Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Implementation of Queues using Stack in C

Queue using Stack

How to implement Queue using Stack?

Implementation of Queues using Stack in C is a process of creating a queue using Stacks. In this article, we will be using a single stack for the purpose. When a single stack is used for implementing queues recursive stack call used. This article contains in detail steps and algorithm for all the functions of queue i.e to insert data,
to delete data and to print the data.

Steps for implementing queue using stack

enqueue(data)

  • In this enqueue function recursively call push() function.
  • In this push function check if the stack is full or not
  • If the stack is we cannot add more elements.
  • Else add the element at top of stack and increase the value of top by 1.

dequeue()

  • In this function first we will check if the queue has some element or not.
  • If our queue is empty then we cannot delete any element from it.
  • Else if there is only one element in the queue we will pop an element from the top of stack.
  • Else we will pop all the data from the stack until top reaches to zero and will push back the removed data after poping the element at top=0.

print()

  • Initialize a for loop from zero till the top.
  • Print the element at every successful iteration.
  • In this way we can display our queue.
Implementation of Queues using Stack in C

Algorithm for implementing queue using stack

enqueue(int x)

  • PUSH(X)

push(x)

  • VOID PUSH(INT X) 
  • IF(TOP == N-1)
  • RETURN STACK IS FULL
  • ELSETOP++
  •  S[TOP] = X 

dequeue()

  • IF(TOP == -1)
  • RETURN QUEUE IS EMPTY
  • ELSE IF(TOP == 0)
  • RETURN POP()
  • DATA = POP()   
  • RES =DEQUEUE()
  • PUSH(DATA)
  • RETURN RES

pop()

  • RETURN S[TOP–]

print()

  • FOR I=0 TO TOP
  • RETURN S[I]

Code for implementing queue using stack in C

#include<stdio.h>
#include<stdlib.h>
# define N 20 //defining the size of queue
int s[N],top = -1;
int pop()//function to remove an element from stack
{
	return s[top--];
}
void push(int x)//function to insert an element into stack
{
	if(top == N-1)
		printf("Stack is Full");
	else
	{
		top++;
		s[top] = x;	
	}
}
void enqueue(int x)//function to insert element in the queue using recursive stack call
{
	push(x);
}
void print()//function to print elements of a queue
{
	int i;
	for(i=0;i<=top;i++)
		printf("\n%d",s[i]);
}

int dequeue()//function to dequeue element from a queue using recursive stack call
{	
	int data,res;
	if(top == -1)
		printf("Queue is Empty");
	else if(top == 0)
		return pop();
	data = pop();
	res = dequeue();
	push(data);
	return res;
 	
}
int main()
{
	int opt,n,i,data,t;
	printf("Enter Your Choice:-");
	
	do{
		printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for Delete the data from the Queue\n0 for Exit\n");
		scanf("%d",&opt);
		switch(opt){
			case 1:
				printf("\nEnter the number of elements:");
				scanf("%d",&n);
				printf("\nEnter your data\n");
				i=0;
				while(i<n){
					scanf("%d",&data);
					enqueue(data);
					i++;
				}
				break;
			case 2:
				print();
				break;
			case 3:
				t = dequeue();
				printf("Dequeued element:%d",t);
				
				break;
			case 0:
				break;
			default:
				printf("\nIncorrect Choice");
			
		}
	}while(opt!=0);
	
}
Output:
Enter Your Choice:-

1 for Insert the Data in Queue
2 for show the Data in Queue 
3 for Delete the data from the Queue
0 for Exit
1

Enter the number of elements:3

Enter your data
1
2
3


1 for Insert the Data in Queue
2 for show the Data in Queue 
3 for Delete the data from the Queue
0 for Exit
3
Dequeued element:1

1 for Insert the Data in Queue
2 for show the Data in Queue 
3 for Delete the data from the Queue
0 for Exit
2

2
3

1 for Insert the Data in Queue
2 for show the Data in Queue 
3 for Delete the data from the Queue
0 for Exit
0

Queue using Array

Here we have already learned implementation of Queues using Stack in C , click the button below to learn the implementation of queue using array