Advertisement
TAHMID37

abonty_stack

Aug 18th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. struct stack{
  6.   int data;
  7.   struct stack*next;
  8. };
  9.  
  10. struct stack* top=NULL;//GLOBAL VARIABLE
  11.  
  12.  
  13. bool is_empty()
  14. {
  15.     return (top==NULL);
  16. }
  17.  
  18. void push(int data)
  19. {
  20.  
  21.     struct stack *temp=(struct stack*)malloc(sizeof(struct stack ));
  22.     temp->data=data;
  23.     if(is_empty())
  24.     {
  25.         top=temp;
  26.         temp->next=NULL;
  27.     }
  28.     else
  29.     {
  30.         temp->next=top;
  31.         top=temp;
  32.     }
  33.  
  34. }
  35.  
  36.  
  37. void pop()
  38. {
  39.  
  40.     if(is_empty())
  41.     {
  42.         printf("The stack is empty\n");
  43.         return;
  44.     }
  45.  
  46.     struct stack *temp=top;
  47.  
  48.     top=temp->next;
  49.  
  50.     temp->next=NULL;
  51.  
  52.  
  53. }
  54.  
  55. void traverse()
  56. {
  57.     struct stack *ptr=top;
  58.     if(is_empty())
  59.     {
  60.         printf("The stack is empty.\n\n");
  61.         return;
  62.     }
  63.  
  64.  
  65.     while(ptr!=NULL)
  66.     {
  67.         printf("%d",ptr->data);
  68.         printf(" ");
  69.         ptr=ptr->next;
  70.     }
  71.     printf("\n");
  72. }
  73.  
  74. int size()
  75. {
  76.     int length=0;
  77.     struct stack* temp=top;
  78.     while(temp!=NULL)
  79.     {
  80.         length++;
  81.         temp=temp->next;
  82.     }
  83.  
  84.   return length;
  85. }
  86.  
  87.  int top_element()
  88. {
  89.     int x=top->data;
  90.     return x;
  91. }
  92.  
  93. void print()
  94. {
  95.     printf("Stack Operations.\n");
  96.     printf("1. Insert into stack (Push operation).\n");
  97.     printf("2. Delete from stack (Pop operation).\n");
  98.     printf("3. Print top element of stack.\n");
  99.     printf("4. Check if stack is empty.\n");
  100.     printf("5. Traverse stack.\n");
  101.     printf("6. Size.\n");
  102.     printf("7. Exit.\n");
  103.     printf("Enter your choice.\n");
  104. }
  105.  
  106. int main()
  107. {
  108.     int choice;
  109.     while(1)
  110.     {
  111.         print();
  112.         scanf("%d",&choice);
  113.         if(choice==1)
  114.         {
  115.              printf("Enter the data you want to insert:");
  116.              int data;
  117.              scanf("%d",&data);
  118.              push(data);
  119.         }
  120.         else if(choice==2)
  121.         {
  122.             pop();
  123.         }
  124.         else if(choice==3)
  125.         {
  126.             if(is_empty())
  127.             {
  128.                 printf("The stack is empty\n");
  129.  
  130.             }
  131.             else
  132.             {
  133.                 int topelm=top_element();
  134.                 printf("%d\n",topelm);
  135.             }
  136.         }
  137.         else if(choice==4)
  138.         {
  139.             if(is_empty())
  140.             {
  141.                 printf("The stack is empty\n");
  142.             }
  143.             else
  144.             {
  145.                 printf("The stack isn't empty\n");
  146.             }
  147.         }
  148.         else if(choice==5)
  149.         {
  150.             traverse();
  151.         }
  152.         else if(choice==6)
  153.         {
  154.            int siz=size();
  155.             printf("size is %d\n",siz);
  156.         }
  157.         else if(choice==7)
  158.         {
  159.             break;
  160.  
  161.         }
  162.  
  163.     }
  164. }
  165.  
  166.  
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement