Advertisement
HmHimu

2 stack to 1queue

Mar 6th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1.     /* C program to implement queues using two stacks */
  2.  
  3.     #include <stdio.h>
  4.  
  5.     #include <stdlib.h>
  6.  
  7.     struct node
  8.  
  9.     {
  10.  
  11.         int data;
  12.  
  13.         struct node *next;
  14.  
  15.     };
  16.  
  17.     void push(struct node** top, int data);
  18.  
  19.     int pop(struct node** top);
  20.  
  21.     struct queue
  22.  
  23.     {
  24.  
  25.         struct node *stack1;
  26.  
  27.         struct node *stack2;
  28.  
  29.     };
  30.  
  31.     void enqueue(struct queue *q, int x)
  32.  
  33.     {
  34.  
  35.         push(&q->stack1, x);
  36.  
  37.     }
  38.  
  39.     void dequeue(struct queue *q)
  40.  
  41.     {
  42.  
  43.         int x;
  44.  
  45.         if (q->stack1 == NULL && q->stack2 == NULL) {
  46.  
  47.             printf("queue is empty");
  48.  
  49.             return;
  50.  
  51.         }
  52.  
  53.         if (q->stack2 == NULL) {
  54.  
  55.             while (q->stack1 != NULL) {
  56.  
  57.             x = pop(&q->stack1);
  58.  
  59.             push(&q->stack2, x);
  60.  
  61.             }
  62.  
  63.         }
  64.  
  65.         x = pop(&q->stack2);
  66.  
  67.         printf("%d\n", x);
  68.  
  69.     }
  70.  
  71.     void push(struct node** top, int data)
  72.  
  73.     {
  74.  
  75.         struct node* newnode = (struct node*) malloc(sizeof(struct node));
  76.  
  77.             if (newnode == NULL) {
  78.  
  79.                 printf("Stack overflow \n");
  80.  
  81.                 return;
  82.  
  83.             }
  84.  
  85.         newnode->data = data;
  86.  
  87.         newnode->next = (*top);
  88.  
  89.         (*top) = newnode;
  90.  
  91.     }
  92.  
  93.     int pop(struct node** top)
  94.  
  95.     {
  96.  
  97.         int buff;
  98.  
  99.         struct node *t;
  100.  
  101.         if (*top == NULL) {
  102.  
  103.             printf("Stack underflow \n");
  104.  
  105.             return;
  106.  
  107.         }
  108.  
  109.         else {
  110.  
  111.             t = *top;
  112.  
  113.             buff = t->data;
  114.  
  115.             *top = t->next;
  116.  
  117.             free(t);
  118.  
  119.             return buff;
  120.  
  121.         }
  122.  
  123.     }
  124.  
  125.     void display(struct node *top1,struct node *top2)
  126.  
  127.     {
  128.  
  129.         while (top1 != NULL) {
  130.  
  131.             printf("%d\n", top1->data);
  132.  
  133.             top1 = top1->next;
  134.  
  135.         }
  136.  
  137.         while (top2 != NULL) {
  138.  
  139.             printf("%d\n", top2->data);
  140.  
  141.             top2 = top2->next;
  142.  
  143.         }
  144.  
  145.     }
  146.  
  147.     int main()
  148.  
  149.     {
  150.  
  151.         struct queue *q = (struct queue*)malloc(sizeof(struct queue));
  152.  
  153.         int f = 0, a;
  154.  
  155.         char ch = 'y';
  156.  
  157.         q->stack1 = NULL;
  158.  
  159.         q->stack2 = NULL;
  160.  
  161.         while (ch == 'y'||ch == 'Y') {
  162.  
  163.             printf("enter ur choice\n1.add to queue\n2.remove
  164.  
  165.                   from queue\n3.display\n4.exit\n");
  166.  
  167.             scanf("%d", &f);
  168.  
  169.             switch(f) {
  170.  
  171.                 case 1 : printf("enter the element to be added to queue\n");
  172.  
  173.                          scanf("%d", &a);
  174.  
  175.                          enqueue(q, a);
  176.  
  177.                          break;
  178.  
  179.                 case 2 : dequeue(q);
  180.  
  181.                          break;
  182.  
  183.                 case 3 : display(q->stack1, q->stack2);
  184.  
  185.                          break;
  186.  
  187.                 case 4 : exit(1);
  188.  
  189.                          break;
  190.  
  191.                 default : printf("invalid\n");
  192.  
  193.                           break;
  194.  
  195.             }
  196.  
  197.         }
  198.  
  199.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement