HmHimu

stack

Jan 31st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct Node
  5. {
  6.     int data;
  7.     struct Node *next;
  8. }*temp;
  9.  
  10. struct Head
  11. {
  12.     int count;
  13.     struct Node *top;
  14. }*head;
  15.  
  16.  
  17.  
  18. void PushNode(int data)
  19. {
  20.     if(head->top==NULL)
  21.     {
  22.         temp=(struct Node*)malloc(sizeof(struct Node));
  23.         temp->data=data;
  24.         temp->next=NULL;
  25.         head->top=temp;
  26.     }
  27.     else{
  28.         temp=(struct Node*)malloc(sizeof(struct Node));
  29.         temp->data=data;
  30.         temp->next=head->top;
  31.         head->top=temp;
  32.     }
  33.     head->count++;
  34.  
  35. }
  36.  
  37.  
  38. void PopNode()
  39. {
  40.      temp=head->top;
  41.  
  42.     if(temp==NULL)
  43.     {
  44.         printf("ERROR!!!\n Empty stack");
  45.         return;
  46.     }
  47.     else{
  48.  
  49.         head->top=temp->next;
  50.         printf("delated value is %d",temp->data);
  51.         free(temp);
  52.         head->count--;
  53.     }
  54.  
  55. }
  56.  
  57. void createStack()
  58. {
  59.     head=(struct Head*)malloc(sizeof(struct Head));
  60.     head->count=0;
  61.     head->top=NULL;
  62.  
  63. }
  64.  
  65. void checkTop()
  66. {
  67.  
  68.   temp=head->top;
  69.  
  70.     if(temp==NULL)
  71.     {
  72.         printf("Empty Stack");
  73.  
  74.     }
  75.     else
  76.     {
  77.         printf("\ntop of the stack is %d",temp->data);
  78.     }
  79. }
  80.  
  81.  
  82. void show()
  83. {
  84.     int i;
  85.     temp=head->top;
  86.     for(i=1;i<=head->count;i++)
  87.     {
  88.         printf("\n%d",temp->data);
  89.         temp=temp->next;
  90.     }
  91. }
  92.  
  93.  
  94.  
  95. int main()
  96. {
  97.     int choice,data;
  98. /*    createStack();
  99.     PushNode(9);
  100.     PushNode(11);
  101.     PushNode(16);
  102.     PushNode(19);
  103.     show();
  104.     PopNode();
  105.     show();
  106.     checkTop();
  107. */
  108. createStack();
  109. while(1)
  110. {
  111.  
  112. printf("\npress 1 for push node\n");
  113. printf("press 2 for pop node\n");
  114. printf("press 3 for stack top\n");
  115. printf("press 4 for check stack\n");
  116. scanf("%d",&choice);
  117. switch(choice)
  118. {
  119.     case 1:
  120.             printf("enter data\n");
  121.             scanf("%d",&data);
  122.              PushNode(data);
  123.              break;
  124.      case 2:
  125.  
  126.              PopNode();
  127.              break;
  128.  
  129.      case 3:
  130.             checkTop();
  131.             break;
  132.      case 4:
  133.             show();
  134.      default:
  135.          printf("nothing to show");
  136.          break;
  137.  
  138.  
  139. }
  140.  
  141.  
  142. }
  143.  
  144. }
Add Comment
Please, Sign In to add comment