Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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