0% found this document useful (0 votes)
54 views4 pages

Module 9 Lab Codes

The document contains C code to implement a stack data structure with functions to push elements onto the stack, display the stack, and find the maximum, minimum, or sort the elements of the stack. It defines a struct for stack nodes with a data element and next pointer. Functions implemented include push to add elements, display to print the stack, max/min to find the maximum/minimum value, and sort to sort the elements of the stack in ascending order. The code is tested in main by pushing random elements onto each stack and calling the corresponding function.

Uploaded by

Vijayendra Goud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

Module 9 Lab Codes

The document contains C code to implement a stack data structure with functions to push elements onto the stack, display the stack, and find the maximum, minimum, or sort the elements of the stack. It defines a struct for stack nodes with a data element and next pointer. Functions implemented include push to add elements, display to print the stack, max/min to find the maximum/minimum value, and sort to sort the elements of the stack in ascending order. The code is tested in main by pushing random elements onto each stack and calling the corresponding function.

Uploaded by

Vijayendra Goud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

------------------------------------Maximum value-----------------------------

// You are using GCC


#include<stdio.h>
#include<stdlib.h>
struct stack{
int data;
struct stack *next;
};
struct stack *top = NULL;
struct stack *push(struct stack*, int);
struct stack *display(struct stack *);
int max(struct stack*);

int main(){
int n;
scanf("%d",&n);
while(n>0){
top=push(top,n);
scanf("%d",&n);
}
printf("Stack elements are:");
display(top);
printf("\nMax value in the stack is:\n %d",max(top));
return 0;

}
struct stack *push(struct stack *top,int val){
struct stack *ptr;
ptr=(struct stack*)malloc(sizeof(struct stack));

if(top== NULL)
{
ptr->data = val;
ptr->next = NULL;
top = ptr;
}
else{
ptr->data = val;
ptr->next = top;
top = ptr;
}
return top;

struct stack *display(struct stack *top){


struct stack *ptr;
ptr=top;
while(ptr!=NULL){
printf("%d",ptr->data);
ptr=ptr->next;
}
return top;
}
int max(struct stack* top){
if(top==NULL){
printf("Stack is empty.\n");
exit(1);
}
int maximum = top->data;
struct stack* ptr = top;
while(ptr != NULL){
if(ptr->data > maximum){
maximum = ptr->data;
}
ptr= ptr->next;
}
return maximum;
}

---------------------Minimum Value-------------------------
// You are using GCC
#include<stdio.h>
#include<stdlib.h>
struct stack{
int data;
struct stack *next;
};
struct stack *top = NULL;
struct stack *push(struct stack*,int);
struct stack *display(struct stack*);
int min(struct stack*);

int main(){
int n;
scanf("%d",&n);
while(n>0){
top=push(top,n);
scanf("%d",&n);
}
printf("Stack elements are:");
display(top);
printf("\nMin value in the stack is:\n %d",min(top));
return 0;
}
struct stack *push(struct stack *top,int val){
struct stack *ptr;
ptr=(struct stack*)malloc(sizeof(struct stack));
if(top==NULL){
ptr->data = val;
ptr->next = NULL;
top = ptr;
}
else{
ptr->data = val;
ptr->next = top;
top = ptr;
}
return top;
}
struct stack *display(struct stack *top){
struct stack *ptr;
ptr=top;
while(ptr!=NULL){
printf("%d",ptr->data);
ptr=ptr->next;
}
return top;
}

int min(struct stack *top){


if(top==NULL){
printf("Stack is empty.\n");
exit(1);
}
int minimum = top->data;
struct stack* ptr = top;
while(ptr != NULL){
if(ptr->data < minimum){
minimum = ptr->data;
}
ptr = ptr->next;
}
return minimum;
}

------------------------sort-------------------------------------
// You are using GCC
#include<stdio.h>
#include<stdlib.h>
struct stack{
int data;
struct stack *next;
};
struct stack *top = NULL;
struct stack *push(struct stack*,int);
struct stack *sort(struct stack*);
struct stack *display(struct stack*);

int main(){
int n;
scanf("%d",&n);
while(n>0){
top=push(top,n);
scanf("%d",&n);
}
printf("Stack elements are:\n");
display(top);
top = sort(top);
printf("\nAfter sorted:\n");
display(top);

return 0;
}
struct stack *push(struct stack *top,int val){
struct stack *ptr;
ptr=(struct stack*)malloc(sizeof(struct stack));
if(top==NULL){
ptr->data = val;
ptr->next = NULL;
top = ptr;
}
else{
ptr->data = val;
ptr->next = top;
top = ptr;
}
return top;
}
struct stack *display(struct stack *top){
struct stack *ptr;
ptr=top;
while(ptr!=NULL){
printf("%d",ptr->data);
ptr=ptr->next;
}
return top;
}

struct stack* sort(struct stack* top){


if(top == NULL || top->next ==NULL){
return top;
}
struct stack* new_top = NULL;
while(top != NULL){
int temp=top->data;
top = top->next;
while(new_top != NULL && new_top->data > temp){
struct stack* new_top_next = new_top->next;
new_top->next = top;
top = new_top;
new_top = new_top_next;
}
struct stack* new_node = (struct stack*)malloc(sizeof(struct stack));
new_node->data = temp;
new_node->next = new_top;
new_top = new_node;
}
return new_top;
}

You might also like