Professional Documents
Culture Documents
Module 9 Lab Codes
Module 9 Lab Codes
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;
---------------------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;
}
------------------------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;
}