0% found this document useful (0 votes)
63 views

Stack Using Two Queue

The document describes an experiment to implement a stack using two queues. It includes the aim, apparatus, code, and conclusion. The code defines node and queue structures, functions to initialize and add/delete from queues, and functions to push/pop from the stack-like queues. It uses two queues - one to simulate the top of the stack and the other for overflow, swapping them when pushing to or popping from the "stack". The program was compiled and run successfully, allowing the user to insert, delete and display stack elements.

Uploaded by

Sudip
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Stack Using Two Queue

The document describes an experiment to implement a stack using two queues. It includes the aim, apparatus, code, and conclusion. The code defines node and queue structures, functions to initialize and add/delete from queues, and functions to push/pop from the stack-like queues. It uses two queues - one to simulate the top of the stack and the other for overflow, swapping them when pushing to or popping from the "stack". The program was compiled and run successfully, allowing the user to insert, delete and display stack elements.

Uploaded by

Sudip
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 6

 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

 Apparatus Required : dev c++ Software.

 Code:

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node * next;
};

struct queue
{
struct node *rear;
struct node *front;
};

void initial(struct queue *);


void qadd(struct queue *,int);
int qdel(struct queue *);
void display(struct queue *);
void push(int);
void pop();

struct queue q1,q2;

void initial(struct queue *q)


{
q->front=NULL;
q->rear=NULL;
}
void qadd(struct queue *q,int n)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));

tmp->data=n;
tmp->next=NULL;

if(q->front==NULL)
{
q->rear=tmp;
q->front=tmp;
return;
}

q->rear->next=tmp;
q->rear=tmp;
}

int qdel(struct queue *q)


{
struct node *tmp;
int itm;
if(q->front==NULL)
{
printf("\nqueue is empty");
return NULL;
}

//itm=q->front->data;
tmp=q->front;
itm=tmp->data;
q->front=tmp->next;
free(tmp);
return itm;

void display(struct queue *q)


{
struct node *tmp;
tmp=q->front;
while((tmp)!=NULL)
{
printf("\n%d",(tmp->data));
tmp=tmp->next;
}
printf("\n");
}

void push(int val)


{
struct queue *tmp;
int j;
qadd(&q2,val);

while(((&q1)->front)!=NULL)
{
j=qdel(&q1);
qadd(&q2,j);
}

(*tmp)=q1;
q1=q2;
q2=(*tmp);

printf("\nelements after pushing are:\n");


display(&q1);

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.

You might also like