Advertisement
muftY

Stack LAB HW (int & char) 2

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