#include #include #include #include struct node { int info; struct node *next; }*tos,*fresh,*ptr,*temp; struct node *create() { ptr=(struct node *)malloc(sizeof(struct node)); printf("\n Enter value"); scanf("%d",&ptr->info); ptr->next=NULL; return(ptr); } void push() { char c; if(tos==NULL) { printf("\n Empty Stack"); printf("\n Do you want to create the first node(Y/N)"); if(toupper(getche())=='Y'); { tos=create(); } } else { fresh=create(); fresh->next=tos; tos=fresh; } } void pop() { if(tos==NULL) printf("\n Nothing to delete! Stack does not exist"); else if(tos->next==NULL) { temp=tos; printf("\n Deleted element is %d",temp->info); tos=NULL; free(temp); } else { temp=tos; printf("\n Deleted value is %d",temp->info); tos=tos->next; free(temp); } } void display() { ptr=tos; if(tos==NULL) printf("\n Nothing to display"); else { while(ptr!=NULL) { printf("\n %d",ptr->info); ptr=ptr->next; } } } void main() { int ch; char choice='Y'; tos=NULL; clrscr(); printf("1. PUSH"); printf("\n2. POP"); printf("\n3. DISPLAY"); printf("\n4. Exit"); while(choice=='Y') { printf("\n Enter your choice(1/2/3/4)"); scanf("%d",&ch); switch(ch) { case 1: push(); display(); break; case 2: pop(); display(); break; case 3: display(); break; case 4: exit(1); default: printf("\n Invalid choice"); } printf("\n Enter your choice to continue"); choice=toupper(getche()); } getch(); }