Advertisement
muftY

Stack multi Up(01-03-20)

Mar 1st, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 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.  
  38. data *pop()
  39. {
  40.     data *new_node=(data*)malloc(sizeof(data));
  41.     new_node->a=head->a;
  42.  
  43.     strcpy(new_node->c,head->c); //string copy korar shrtcut, ei jnno library file #include<string.h> use kora hoice.
  44.     //strcpy= string_copy.
  45.  
  46.     data *del=head;
  47.     head=head->next;
  48.     free(del);
  49.     return new_node;
  50. }
  51.  
  52. void top()
  53. {
  54.     if(head==NULL)
  55.     {
  56.         printf("    Top is Empty \n");
  57.         return ;
  58.     }
  59.  
  60.     printf("    Top: %d %s \n",head->a, head->c);
  61.  
  62.     return;
  63. }
  64.  
  65.  
  66.  
  67. void reset()
  68. {
  69.     data *del=head;
  70.     head=NULL;
  71.     printf("    Reseted\n");
  72.     free(del);
  73.     return;
  74. }
  75.  
  76. void print()
  77. {
  78.     data *temp=head;
  79.     printf("   Result: ");
  80.     if(head==NULL)
  81.     {
  82.         printf("Stack Empty");
  83.     }
  84.     while(temp!=NULL)
  85.     {
  86.         printf("%d %s ",temp->a, temp->c);
  87.         temp=temp->next;
  88.     }
  89.     printf("\n");
  90. }
  91.  
  92. int main()
  93. {
  94.     int n;
  95.  
  96.     while(1)
  97.     {
  98.  
  99.         printf("\n1.PUSH\n");
  100.         printf("2.POP\n");
  101.         printf("3.TOP\n");
  102.  
  103.         printf("4.PRINT\n");
  104.         printf("9.RESET\n");
  105.         printf("0.BREAK\n");
  106.  
  107.         printf("   Command: ");
  108.         scanf("%d",&n);
  109.         printf("\n");
  110.         //push
  111.  
  112.         if(n==1)
  113.         {
  114.             push();
  115.         }
  116.  
  117.         //pop
  118.         if(n==2)
  119.         {
  120.             if(head==NULL)
  121.             {
  122.                 printf("   Nothing To POP Bro\n");
  123.                 continue;
  124.             }
  125.  
  126.             data *temporary=pop();
  127.  
  128.             printf("   POPPED: %d %s\n",temporary->a,temporary->c);
  129.  
  130.             free(temporary);
  131.  
  132.         }
  133.  
  134.         //top
  135.  
  136.         if(n==3)
  137.         {
  138.             top();
  139.         }
  140.  
  141.         if(n==4)
  142.         {
  143.             print();
  144.         }
  145.         if(n==9)
  146.         {
  147.             reset();
  148.         }
  149.         if(n==0)
  150.         {
  151.             printf("    Prgrm Clossed\n");
  152.             break;
  153.         }
  154.  
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement