Stack Using Two Queue
Stack Using Two Queue
AIM:
Write a program to implement stack using two queues and performs following program:-
(a) Write a program to insert an element in stack.
(b)Write a program to delete an element from stack.
(c)Write a program to display the elements of stack.
(d)Exit the program
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct queue
{
struct node *rear;
struct node *front;
};
tmp->data=n;
tmp->next=NULL;
if(q->front==NULL)
{
q->rear=tmp;
q->front=tmp;
return;
}
q->rear->next=tmp;
q->rear=tmp;
}
//itm=q->front->data;
tmp=q->front;
itm=tmp->data;
q->front=tmp->next;
free(tmp);
return itm;
while(((&q1)->front)!=NULL)
{
j=qdel(&q1);
qadd(&q2,j);
}
(*tmp)=q1;
q1=q2;
q2=(*tmp);
void pop()
{
printf("\n element deleted is %d",qdel(&q1));
}
int main()
{
printf("press\n1. insert in stack\n2. delete from stack\n3.display the stack
element\n4.exit the program\n");
int n=0,val;
initial(&q1);
initial(&q2);
while(1){
printf("enter choice:--");
scanf("%d",&n);
switch(n){
case 1:
printf("enter element to be inserted:--");
scanf("&d",&val);
push(val);
break;
case 2:
pop();
display(&q1);
break;
case 3:
display(&q1);
break;
case 4:
exit(1);
break;
default:
printf("invalid input\n");
break;
}
}
return 0;
}
Output:
Conclusion: The program written above was successfully executed in the dev c++ software.