mhrabbi

Stack

Nov 4th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<limits.h>
  4. struct stack
  5. {
  6.     int data;
  7.     struct stack *next;
  8.  
  9. }*top;
  10. int size=0;
  11. void push(int element);
  12. int pop();
  13. void display();
  14. void maxmin();
  15. void find();
  16. int main()
  17. {
  18.     int choice, data;
  19.     while(1)
  20.    {
  21.  
  22.     printf("--------------------------------------------\n");
  23.     printf("choose what do you want:\n");
  24.     printf("1. Push\n");
  25.     printf("2. Pop\n");
  26.     printf("3. display\n");
  27.     printf("4. size\n");
  28.     printf("5. Maximum & minimum\n");
  29.     printf("6. Find Item\n");
  30.     printf("Enter your choice: ");
  31.     scanf("%d",&choice);
  32.     switch(choice)
  33.     {
  34.     case 1:
  35.         printf("Input data to push into stack: ");
  36.         scanf("%d",&data);
  37.         push(data);
  38.         break;
  39.     case 2:
  40.         data=pop();
  41.         if(data!=INT_MIN)
  42.             printf("Popped data is : %d",data);
  43.  
  44.         break;
  45.     case 3:
  46.         display();
  47.         break;
  48.     case 4:
  49.         printf("stack size is : %d",size);
  50.         break;
  51.     case 5:
  52.         maxmin();
  53.         break;
  54.     case 6:
  55.         find();
  56.         break;
  57.  
  58.     }
  59.     printf("\n\n");
  60.    }
  61. return 0;
  62. }
  63. void push(int element)
  64. {
  65.     struct stack *temp;
  66.     temp=(struct stack*)malloc(sizeof(struct stack));
  67.     temp->data=element;
  68.     temp->next=top;
  69.     top=temp;
  70.     printf("Pushed data is: %d\n",temp->data);
  71.     size++;
  72. }
  73. int pop()
  74. {
  75.     int data=0;
  76.     struct stack *temp;
  77.     if(size<= 0 || !top)
  78.     {
  79.         printf("Stack is Empty");
  80.         return INT_MIN;
  81.     }
  82.  
  83.     temp=top;
  84.     data=top->data;
  85.     top=top->next;
  86.     free(temp);
  87.     size--;
  88.     return data;
  89.  
  90. }
  91. void display()
  92. {
  93.     struct stack *temp;
  94.     temp=top;
  95.     printf("Stack data are:\n");
  96.     while(temp!=NULL)
  97.     {
  98.         printf("%d\n",temp->data);
  99.         temp=temp->next;
  100.     }
  101. }
  102. void maxmin()
  103. {
  104.     struct stack *temp;
  105.     temp=top;
  106.     int max,min;
  107.     max=temp->data;
  108.     min=temp->data;
  109.  
  110.     while(temp!=NULL)
  111.     {
  112.         if(temp->data>max)
  113.         {
  114.             max=temp->data;
  115.         }
  116.         if(temp->data<min)
  117.         {
  118.             min=temp->data;
  119.         }
  120.         temp=temp->next;
  121.     }
  122.     printf("Maximum element is : %d\n",max);
  123.     printf("Minimum element is : %d\n",min);
  124. }
  125. void find()
  126. {
  127.     struct stack *temp;
  128.     int num,i,Item;
  129.     i=0;
  130.     temp=top;
  131.     printf("Enter a number to find: ");
  132.     scanf("%d",&num);
  133.     while(temp!=NULL)
  134.     {
  135.         if(temp->data==num)
  136.            {
  137.             i++;
  138.             Item=temp->data;
  139.            }
  140.  
  141.         temp=temp->next;
  142.     }
  143.    printf("Item Found %d times: %d\n",i,Item);
  144. }
Add Comment
Please, Sign In to add comment