#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5 //Capacity of stack
typedef struct node{
int data;
struct node *next;
}nptr;
int count=0;
nptr *top=NULL;
void push(int x){
nptr *nn;
nn=(nptr *)malloc(sizeof(nptr));
nn->data=x;
nn->next=NULL;
if(top!=NULL)
nn->next=top;
top=nn;
count++;
}
int pop(){
int x=top->data;
nptr *temp=top;
top=top->next;
free(temp);
count--;
return x;
}
int peep(){
return top->data;
}
void display(){
nptr *temp=top;
while(temp!=NULL){
printf(" %d\n",temp->data);
temp=temp->next;
}
}
void main(){
int ch,data;
printf("\nSTACK\n");
while(1){
printf("\nEnter Choice\n1 Push \n2 Pop\n3 Peep\n4 Display Stack\n5 Exit\n\n");
ch=getch();
switch(ch){
case '1':if(count==MAX)
printf("\nStack Overflow\n");
else{
printf("\nEnter data\n");
scanf("%d",&data);
push(data);
printf("\nPushed Value: %d\n",data);
}
break;
case '2':if(!count)
printf("\nStack Underflow\n");
else{
data=pop();
printf("\nPopped Value: %d\n",data);
}
break;
case '3':if(!count)
printf("\nStack Empty\n");
else{
printf("\nTop Value: %d\n",peep());
}
break;
case '4':display();break;
case '5':exit(0);break;
default: printf("\nInvalid Choice\n");
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.