Advertisement
aurko96

Stack

Jun 19th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. //#include"stack.h"
  4.  
  5. struct node{
  6.     int data;
  7.     struct node *next;
  8. };
  9.  
  10. struct list{
  11.     struct node *head;
  12. };
  13.  
  14. struct node* createNode()
  15. {
  16.    int a;
  17.    struct node* x=malloc(sizeof(struct node));
  18.    scanf("%d",&a);
  19.    x->data=a;
  20.    x->next=NULL;
  21.    return x;
  22. }
  23. int push(struct list* l,struct node* newnode)
  24. {
  25.     if(l->head==NULL)
  26.     {
  27.         l->head=newnode;
  28.     }
  29.     else
  30.     {
  31.         newnode->next=l->head;
  32.         l->head=newnode;
  33.     }
  34. }
  35. void displayList(struct list* l)
  36. {
  37.     struct node* x = l->head;
  38.     while(x!=NULL)
  39.     {
  40.         printf("\n %d \n",x->data);
  41.         x = x->next;
  42.     }
  43.     getch();
  44. }
  45. struct node* pop(struct list* l)
  46. {
  47.     struct node* x;
  48.     if(l->head==NULL)
  49.     {
  50.         return 0;
  51.     }
  52.     else
  53.     {
  54.         x=l->head;
  55.         l->head=(l->head)->next;
  56.     }
  57.     return x;
  58. }
  59.  
  60. int getChoice(int choice){
  61.     char ch=10;
  62.     while(ch!=13){
  63.         system("cls");
  64.         if(choice==1){
  65.             printf("\n   -->\tP U S H");
  66.         }
  67.         else printf("\n\tPush");
  68.         if(choice == 2)
  69.             printf("\n   -->\tP O P");
  70.         else printf("\n\tPop");
  71.         if(choice == 3)
  72.             printf("\n   -->\tD I S P L A Y");
  73.         else printf("\n\tDisplay");
  74.         if(choice == 4)
  75.             printf("\n   -->\tQ U I T");
  76.         else printf("\n\tQuit");
  77.         printf("\n\n[ Use UP and DOWN arrow to select an option ]");
  78.         ch = getch();
  79.         if(ch == 72)
  80.             choice--;
  81.         else if(ch == 80)
  82.             choice++;
  83.         if(choice<1)
  84.             choice = 1;
  85.         else if(choice>4)
  86.             choice = 4;
  87.     }
  88.     return choice;
  89. }
  90.  
  91. int main()
  92. {
  93.     int choice=1;
  94.     char ch;
  95.     struct node* tempnode;
  96.     struct list l;
  97.     l.head = NULL;
  98.  
  99.     while(choice!=4){
  100.         choice = getChoice(choice);
  101.         switch(choice){
  102.             case 1: tempnode = createNode();
  103.                     push(&l,tempnode);
  104.                     printf("\n\n New node pushed SUCCESSFULLY.");
  105.                     displayList(&l);
  106.                     break;
  107.             case 2: tempnode = pop(&l);
  108.                     if(tempnode!=NULL)
  109.                         printf("\n\n Node popped SUCCESSFULLY\n Deleted value: %d",tempnode->data);
  110.                     displayList(&l);
  111.                     break;
  112.             case 3: displayList(&l);
  113.                     break;
  114.             case 4: printf("\n\n");
  115.         }
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement