Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* C program to implement queues using two stacks */
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- };
- void push(struct node** top, int data);
- int pop(struct node** top);
- struct queue
- {
- struct node *stack1;
- struct node *stack2;
- };
- void enqueue(struct queue *q, int x)
- {
- push(&q->stack1, x);
- }
- void dequeue(struct queue *q)
- {
- int x;
- if (q->stack1 == NULL && q->stack2 == NULL) {
- printf("queue is empty");
- return;
- }
- if (q->stack2 == NULL) {
- while (q->stack1 != NULL) {
- x = pop(&q->stack1);
- push(&q->stack2, x);
- }
- }
- x = pop(&q->stack2);
- printf("%d\n", x);
- }
- void push(struct node** top, int data)
- {
- struct node* newnode = (struct node*) malloc(sizeof(struct node));
- if (newnode == NULL) {
- printf("Stack overflow \n");
- return;
- }
- newnode->data = data;
- newnode->next = (*top);
- (*top) = newnode;
- }
- int pop(struct node** top)
- {
- int buff;
- struct node *t;
- if (*top == NULL) {
- printf("Stack underflow \n");
- return;
- }
- else {
- t = *top;
- buff = t->data;
- *top = t->next;
- free(t);
- return buff;
- }
- }
- void display(struct node *top1,struct node *top2)
- {
- while (top1 != NULL) {
- printf("%d\n", top1->data);
- top1 = top1->next;
- }
- while (top2 != NULL) {
- printf("%d\n", top2->data);
- top2 = top2->next;
- }
- }
- int main()
- {
- struct queue *q = (struct queue*)malloc(sizeof(struct queue));
- int f = 0, a;
- char ch = 'y';
- q->stack1 = NULL;
- q->stack2 = NULL;
- while (ch == 'y'||ch == 'Y') {
- printf("enter ur choice\n1.add to queue\n2.remove
- from queue\n3.display\n4.exit\n");
- scanf("%d", &f);
- switch(f) {
- case 1 : printf("enter the element to be added to queue\n");
- scanf("%d", &a);
- enqueue(q, a);
- break;
- case 2 : dequeue(q);
- break;
- case 3 : display(q->stack1, q->stack2);
- break;
- case 4 : exit(1);
- break;
- default : printf("invalid\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement