NB52053

Nadui_LAB7

Dec 3rd, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. // Header File-----------------------------------------------
  2.  
  3. #include <stdio.h>
  4. #ifndef Header_h_
  5. #define Header_h_
  6.  
  7. struct list{
  8.     int data;
  9.     struct list *next;
  10. };
  11.  
  12.  
  13. void push(int value);
  14. void pop();
  15. void enqueue(int value);
  16. void dequeue();
  17. void show();
  18. #endif
  19.  
  20. //----------------------------------------------------
  21.  
  22.  
  23.  
  24. // Source 1------------------------------------------(Start)
  25. #include <stdio.h>
  26. #include "Header.h"
  27. #include <stdlib.h>
  28.  
  29. typedef struct list node;
  30.  
  31. node *temp,*top=NULL,*r,*f=NULL,*tail;
  32.  
  33.  
  34. void push(int value)
  35. {
  36.     if(top == NULL){
  37.     temp = (node*)malloc(sizeof(node));
  38.     temp->data = value;
  39.     top = temp;
  40.     top->next = NULL;
  41.     r = top;
  42.     }
  43.     else
  44.     {
  45.         temp = (node*)malloc(sizeof(node));
  46.         temp->data = value;
  47.         temp -> next = top;
  48.         top = temp;
  49.         r = top;
  50.         while(r -> next != NULL)
  51.             r = r -> next;
  52.     }
  53.      
  54. }
  55. void pop()
  56. {
  57.     if(top == NULL)
  58.         printf("\nStack Underflow!!!\n");
  59.      
  60.     else
  61.     {
  62.         temp = top;
  63.         top = top -> next;
  64.         free(temp);
  65.         r = top;
  66.     }
  67. }
  68. void enqueue(int value)
  69. {
  70.  
  71.     if(top == NULL)
  72.     {
  73.         temp = (node*)malloc(sizeof(node));
  74.         temp -> data = value;
  75.         temp -> next = NULL;
  76.         r = top;
  77.     }
  78.     else{
  79.         temp = (node*)malloc(sizeof(node));
  80.         temp -> data = value;
  81.         temp -> next = NULL;
  82.         r -> next = temp;
  83.         r = temp;
  84.     }
  85. }
  86. void dequeue()
  87. {
  88.     if(top == NULL)
  89.         printf("\nUnderflow!!!\n");
  90.      
  91.     else
  92.     {
  93.         temp = top;
  94.         top = top -> next;
  95.         free(temp);
  96.     }
  97.      
  98. }
  99. void show()
  100. {
  101.     tail = top;
  102.     while(tail != NULL)
  103.     {
  104.         printf("%d ",tail->data);
  105.         tail = tail->next;
  106.     }
  107. }
  108.  
  109. //----------------------------------------------------------(End)
  110.  
  111.  
  112.  
  113.  
  114.  
  115. //Source----------------------------------------------------------(Start)
  116. #include <stdio.h>
  117. #include "Header.h"
  118. #include <stdlib.h>
  119.  
  120. int main()
  121. {
  122.     int value,choice;
  123.     while(1)
  124.     {      
  125.             printf("------------Stack and Queue Implementation--------\n");
  126.             printf("1) Push\n");
  127.             printf("2) Pop\n");
  128.             printf("3) Enqueue\n");
  129.             printf("4) Dequeue\n");
  130.             printf("5) Display all\n");
  131.             printf("6) Exit\n");
  132.         printf("Enter your choice: \n");
  133.         scanf("%d",&choice);
  134.         printf("\n");
  135.         switch(choice)
  136.         {
  137.         case 1:
  138.             printf("Enter the value: \n");
  139.             scanf("%d",&value);
  140.             push(value);
  141.             break;
  142.         case 2:
  143.             pop();
  144.             break;
  145.         case 3:
  146.             printf("Enter the value: \n");
  147.             scanf("%d",&value);
  148.             enqueue(value);
  149.             break;
  150.         case 4:
  151.             dequeue();
  152.             break;
  153.         case 5:
  154.             show();
  155.             break;
  156.         case 6:
  157.             exit(0);
  158.         default:
  159.             printf("\n\nWrong input!!!\n\n");
  160.         }
  161.     }
  162.     return 0;
  163. }
  164.  
  165. //----------------------------------------------------------(End)
Add Comment
Please, Sign In to add comment