0% found this document useful (0 votes)
47 views11 pages

Untitled Document

The document describes a C program that implements a queue using two stacks. It defines functions to push and pop elements onto the queue/stacks. The main function displays a menu to enqueue, dequeue, display the queue, or exit. When run, it allows the user to insert elements, remove elements, and view the current queue until exiting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views11 pages

Untitled Document

The document describes a C program that implements a queue using two stacks. It defines functions to push and pop elements onto the queue/stacks. The main function displays a menu to enqueue, dequeue, display the queue, or exit. When run, it allows the user to insert elements, remove elements, and view the current queue until exiting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

● Program:-

#include <stdio.h>
#define MAX 100
int stack1[MAX], stack2[MAX],top1=-1,top2= -1;

void push1(int k)
{
if (top1==(MAX-1))
{
printf("stack overflow\n");
return;
}
else
{
top1+=1;
stack1[top1]=k;
}
}

void push2(int k)
{
if (top2==(MAX-1))
{
printf("stack overflow\n");
return;
}
else
{
top2+=1;
stack2[top2]=k;
}
}

int pop1()
{
int val;
if(top1==-1)
{
printf("stack underflow\n");
return -1;
}
else
{
val= stack1[top1];
top1= top1-1;
return val;
}
}

int pop2()
{
int val;
if(top2==-1)
{
printf("stack underflow\n");
return -1;
}
else
{
val= stack2[top2];
top2= top2-1;
return val;
}
}

void EnQueue(int k)
{
if (top1==MAX-1)
{
printf("Queue is full\n");
return;
}
else
{
push1(k);
}
}
int DeQueue()
{
if (top1==-1)
{
printf("Queue is empty\n");
}
else
{
while (top1!=-1)
{
int val1=pop1();
push2(val1);
}
int retval=stack2[top2];
top2--;
while (top2!=-1)
{
int val2=pop2();
push1(val2);
}
return retval;
}
}

void display()
{
int i;
printf("ITEM:\n");
for(i=0;i<=top1;i++)
{
printf("%d ",stack1[i]);
}
printf("\n");
}

int main()
{
while(1)
{
printf("\nMenu\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("Enter your choice(1-4):\n");
int ch,ele;
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element you want to insert:\n");
scanf("%d",&ele);
EnQueue(ele);
break;
case 2:
printf("Deleted item is: %d\n",DeQueue());
break;
case 3:
display();
break;
case 4:
return 0;
break;
default:
printf("Invalid Input\n");
break;
}
}
return 0;
}

● Output:-

Menu
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice(1-4):
1
Enter the element you want to insert:
3

Menu
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice(1-4):
1
Enter the element you want to insert:
4

Menu
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice(1-4):
3
ITEM:
34

Menu
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice(1-4):
2
Deleted item is: 3

Menu
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice(1-4):
4

--------------------------------
Process exited after 21.35 seconds with return value 0
Press any key to continue . . .
● Program:-

#include <stdio.h>
#define MAX 100
int q1[MAX], q2[MAX], front1=-1,front2=-1, rear1=-1, rear2=-1;

void EnQueue1(int n)
{
if(rear1 ==(MAX-1))
{
printf("Queue1 is full\n");
}
else
{
if(front1== -1)
{
front1=0;
}
rear1++;
q1[rear1]=n;
}
}

void EnQueue2(int n)
{
if(rear2 ==(MAX-1))
{
printf("Queue2 is full\n");
}
else
{
if(front2== -1)
{
front2=0;
}
rear2++;
q2[rear2]=n;
}
}
int DeQueue1()
{
int retval;
if(front1==-1)
{
printf("Queue1 is empty\n");
return 0;
}
else
{
retval= q1[front1];
front1++;
if(front1>rear1)
{
front1= -1;
rear1= -1;
}
return retval;
}
}

int DeQueue2()
{
int retval;
if(front2==-1)
{
printf("Queue2 is empty\n");
return 0;
}
else
{
retval= q2[front2];
front2++;
if(front2>rear2)
{
front2= -1;
rear2= -1;
}
return retval;
}
}
void push(int k)
{
if(rear1== MAX-1)
{
printf("Stack Overflow\n");
}
else
{
EnQueue1(k);
}
}

int pop()
{
int val;
if (rear1== -1)
{
printf("Stack Underflow");
}
else
{
while(rear1!=front1)
{
EnQueue2(DeQueue1());
}
val= DeQueue1();
while(rear2!=-1)
{
EnQueue1(DeQueue2());
}
return val;
}
}

void display()
{
int i;
printf("The elements are:\n");
for(i=rear1;i>=front1;i--)
{
printf("%d",q1[i]);
printf("\n");
}
}

int main()
{
while(1)
{
printf("\nMenu\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("Enter your choice(1-4):\n");
int ch,ele;
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element you want to insert:\n");
scanf("%d",&ele);
push(ele);
break;
case 2:
printf("The popped element is: %d\n",pop());
break;
case 3:
display();
break;
case 4:
return 0;
break;
default:
printf("Invalid Input\n");
break;
}
}
return 0;
}
● Output:-

Menu
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):
1
Enter the element you want to insert:
3

Menu
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):
1
Enter the element you want to insert:
4

Menu
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):
3
The elements are:
4
3

Menu
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):
2
The popped element is: 4

Menu
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):
4

--------------------------------
Process exited after 68.36 seconds with return value 0
Press any key to continue . . .

You might also like