Advertisement
muftY

Stack multi

Feb 23rd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. typedef struct data
  6. {
  7.     int a;
  8.     char c[100];
  9.     struct data *next;
  10. } data;
  11.  
  12. data *head=NULL;
  13.  
  14. void push()
  15. {
  16.     data *new_node=(data*)malloc(sizeof(data));
  17.     printf("     Integer: ");
  18.     scanf("%d",&new_node->a);
  19.     printf("\n   Character: ");
  20.  
  21.     scanf(" %[^\n]s",new_node->c);
  22.  
  23.     new_node->next=NULL;
  24.  
  25.     if(head==NULL)
  26.     {
  27.         head=new_node;
  28.         return;
  29.     }
  30.     new_node->next=head;
  31.     head=new_node;
  32.  
  33.     return;
  34.  
  35. }
  36.  
  37. //data *copy=NULL;
  38.  
  39. void pop()
  40. {
  41.    if(head==NULL)
  42.     {
  43.         printf("   Nothing To POP Bro\n");
  44.         return;
  45.     }
  46.  
  47. //    copy->a=head->a;
  48. //
  49. //    strcpy(copy->c,head->c);
  50.  
  51.     data *del=head;
  52.  
  53.     printf("   POPPED: %d %s\n",head->a,head->c);
  54.  
  55.     head=head->next;
  56.  
  57.     free(del);
  58.     return;
  59. }
  60.  
  61. void top()
  62. {
  63.     if(head==NULL)
  64.     {
  65.         printf("    Top is Empty \n");
  66.         return ;
  67.     }
  68.  
  69.     printf("    Top: %d %s \n",head->a, head->c);
  70.  
  71.     return;
  72. }
  73.  
  74.  
  75.  
  76. void reset()
  77. {
  78.  
  79.     head=NULL;
  80.     printf("    Reseted\n");
  81.     return;
  82. }
  83.  
  84. void print()
  85. {
  86.     data *temp=head;
  87.     printf("   Result: ");
  88.     if(head==NULL)
  89.     {
  90.         printf("Stack Empty");
  91.     }
  92.     while(temp!=NULL)
  93.     {
  94.         printf("%d %s",temp->a, temp->c);
  95.         temp=temp->next;
  96.     }
  97.     printf("\n");
  98. }
  99.  
  100. int main()
  101. {
  102.     int n;
  103.  
  104.     while(1)
  105.     {
  106.  
  107.         printf("\n1.PUSH\n");
  108.         printf("2.POP\n");
  109.         printf("3.TOP\n");
  110.  
  111.         printf("4.PRINT\n");
  112.         printf("9.RESET\n");
  113.         printf("0.BREAK\n");
  114.  
  115.         printf("   Command: ");
  116.         scanf("%d",&n);
  117.         printf("\n");
  118.  
  119.  
  120.         if(n==1)
  121.         {
  122.             push();
  123.         }
  124.  
  125.         if(n==2)
  126.         {
  127.             pop();
  128.  
  129.         }
  130.        if(n==3)
  131.         {
  132.             top();
  133.         }
  134.         if(n==4)
  135.         {
  136.             print();
  137.         }
  138.         if(n==9)
  139.         {
  140.             reset();
  141.         }
  142.         if(n==0)
  143.         {
  144.             printf("    Prgrm Clossed\n");
  145.             break;
  146.         }
  147.  
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement