Advertisement
muftY

Stack LAB HW (int & char)

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