Write a Menu Driven Program Using C for Dynamic implementation of Stack. The Menu Includes -Push -Pop -Display -Exit
#include#include #include struct node { int data; struct node *link; }*top; void push(); void pop(); void display(); void main() { int ch; clrscr(); top=NULL; while(1) { printf("\n 1.Push"); printf("\n 2.pop"); printf("\n 3.Display"); printf("\n 4.Exit"); printf("\n Enter Your Choice: "); scanf("%d",&ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:display(); break; case 4:exit(0); default: printf("\n Invalid Choice"); } } } void push() { int ele,i,n; struct node *newnode,*prev; printf("\n Enter How Many Element You Want To Push: "); scanf("%d",&n); top=NULL; for(i=1;i<=n;i++) { newnode=(struct node*)malloc(sizeof(struct node)); if(newnode==NULL) { printf("\n Insufficient Memory"); exit(0); } printf("\n Enter Element %d : ",i); scanf("%d",&ele); newnode->data=ele; newnode->link=NULL; if(top==NULL) { top=newnode; prev=newnode; } else { prev->link=newnode; prev=newnode; } } } void pop() { struct node *temp,*curr; temp=top; if(top==NULL) printf("\n Stack is Empty."); else if(top->link==NULL) { printf("Popped Element is : %d ",top->data); free(top); top=NULL; } else { temp=top; while(temp->link->link!=NULL) { temp=temp->link; } curr=temp->link; printf("\n Popped Element is: %d",curr->data); temp->link=NULL; free(curr); } } void display() { struct node *temp; temp=top; if(top==NULL) { printf("\n Stack is Empty."); return; } printf("\n Element in Stack are:"); while(temp!=NULL) { printf("\t%d",temp->data); temp=temp->link; } } /*OUTPUT 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 1 Enter How Many Element You Want To Push: 5 Enter Element 1 : 2 Enter Element 2 : 1 Enter Element 3 : 3 Enter Element 4 : 4 Enter Element 5 : 5 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 3 Element in Stack are: 2 1 3 4 5 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 2 Popped Element is: 5 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 3 Element in Stack are: 2 1 3 4 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 2 Popped Element is: 4 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 3 Element in Stack are: 2 1 3 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 2 Popped Element is: 3 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 3 Element in Stack are: 2 1 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 2 Popped Element is: 1 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 3 Element in Stack are: 2 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 2 Popped Element is : 2 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 3 Stack is Empty. 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 2 Stack is Empty. 1.Push 2.pop 3.Display 4.Exit Enter Your Choice: 4 */
Write a Menu Driven Program Using C for Dynamic implementation of Stack. The Menu Includes -Push -Pop -Display -Exit
Reviewed by
on
November 17, 2013
Rating: