In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.

Watch related videos of your favorite subject.

Connect with students from different parts of the world.

Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE

See Our team

Wondering how we keep quality?

Got unsolved questions? Ask Questions

GATE
GMAT
CBSE
NCERT
Career
Interview
Railway
UPSC
NID
NIFT-UG
NIFT-PG
PHP
AJAX
JavaScript
Node Js
Shell Script
Research

## What is a Queue, how it is different from stack and how is it implemented?

## Interview preparation question on data Structures.This are the main questions which can be used while preparing for interviews on computer science.The is question is about what is a Queue, how it is different from stack and how is it implemented?

Asked On2019-08-17 04:01:30 by:Annaliya

Taged users:

komaleduAlfaqshubhamrdardapranaymasaneAnirudh

Likes:

Annaliya

Dislikes:

Be first to dislike this question

Talk about this Like Dislike

Download question setAnswersLikes:

Be first to like this answer

Dislikes:

Be first to dislike this answer

Talk about this Like Dislike

## You might like this video:Logistic Regression- R-squared and p-value

Watch more here

## Watch more videos from this user Here

Learn how to upload a video over here

## Difference Between Stack and Queue

Stack and Queue both are the non-primitive data structures. The main differences between stack and queue are that stack uses LIFO (last in first out) method to access and add data elements whereas Queue uses FIFO (First in first out) method to access and add data elements.

Stack has only one end open for pushing and popping the data elements on the other hand Queue has both ends open for enqueuing and dequeuing the data elements.

Stack and queue are the data structures used for storing data elements and are actually based on some real world equivalent. For example, the stack is a stack of CD’s where you can take out and put in CD through the top of the stack of CDs. Similarly, The queue is a queue for Theatre tickets where the person standing in the first place, i.e., front of the queue will be served first and the new person arriving will appear in the back of the queue (rear end of the queue).

Comparison Chart

Array implementation Of QueueFor implementing queue, we need to keep track of two indices, front and rear. We enqueue an item at the rear and dequeue an item from front. If we simply increment front and rear indices, then there may be problems, front may reach end of the array. The solution to this problem is to increase front and rear in circular manner.

// CPP program for array implementation of queue

#include <bits/stdc++.h>

using namespace std;

// A structure to represent a queue

class Queue

{

public:

int front, rear, size;

unsigned capacity;

int* array;

};

// function to create a queue of given capacity.

// It initializes size of queue as 0

Queue* createQueue(unsigned capacity)

{

Queue* queue = new Queue();

queue->capacity = capacity;

queue->front = queue->size = 0;

queue->rear = capacity - 1; // This is important, see the enqueue

queue->array = new int[(queue->capacity * sizeof(int))];

return queue;

}

// Queue is full when size

// becomes equal to the capacity

int isFull(Queue* queue)

{ return (queue->size == queue->capacity); }

// Queue is empty when size is 0

int isEmpty(Queue* queue)

{ return (queue->size == 0); }

// Function to add an item to the queue.

// It changes rear and size

void enqueue(Queue* queue, int item)

{

if (isFull(queue))

return;

queue->rear = (queue->rear + 1) % queue->capacity;

queue->array[queue->rear] = item;

queue->size = queue->size + 1;

cout << item << " enqueued to queue\n";

}

// Function to remove an item from queue.

// It changes front and size

int dequeue(Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

int item = queue->array[queue->front];

queue->front = (queue->front + 1) % queue->capacity;

queue->size = queue->size - 1;

return item;

}

// Function to get front of queue

int front(Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->front];

}

// Function to get rear of queue

int rear(Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->rear];

}

// Driver code

int main()

{

Queue* queue = createQueue(1000);

enqueue(queue, 10);

enqueue(queue, 20);

enqueue(queue, 30);

enqueue(queue, 40);

cout<<dequeue(queue)<<" dequeued from queue\n";

cout << "Front item is " << front(queue) << endl;

cout << "Rear item is " << rear(queue) << endl;

return 0;

}

// This code is contributed by rathbhupendra

Output:

10 enqueued to queue

20 enqueued to queue

30 enqueued to queue

40 enqueued to queue

10 dequeued from queue

Front item is 20

Rear item is 40

Time Complexity:Time complexity of all operations like enqueue(), dequeue(), isFull(), isEmpty(), front() and rear() is O(1). There is no loop in any of the operations.Likes:

Be first to like this answer

Dislikes:

Be first to dislike this answer

Talk about this Like Dislike

## You might like this video:Network and IP Address

Watch more here

## Watch more videos from this user Here

Learn how to upload a video over here

## Difference Between Stack and Queue

Stack and Queue both are the non-primitive data structures. The main differences between stack and queue are that stack uses LIFO (last in first out) method to access and add data elements whereas Queue uses FIFO (First in first out) method to access and add data elements.

Stack has only one end open for pushing and popping the data elements on the other hand Queue has both ends open for enqueuing and dequeuing the data elements.

Stack and queue are the data structures used for storing data elements and are actually based on some real world equivalent. For example, the stack is a stack of CD’s where you can take out and put in CD through the top of the stack of CDs. Similarly, The queue is a queue for Theatre tickets where the person standing in the first place, i.e., front of the queue will be served first and the new person arriving will appear in the back of the queue (rear end of the queue).

Comparison Chart

Array implementation Of QueueFor implementing queue, we need to keep track of two indices, front and rear. We enqueue an item at the rear and dequeue an item from front. If we simply increment front and rear indices, then there may be problems, front may reach end of the array. The solution to this problem is to increase front and rear in circular manner.

// CPP program for array implementation of queue

#include <bits/stdc++.h>

using namespace std;

// A structure to represent a queue

class Queue

{

public:

int front, rear, size;

unsigned capacity;

int* array;

};

// function to create a queue of given capacity.

// It initializes size of queue as 0

Queue* createQueue(unsigned capacity)

{

Queue* queue = new Queue();

queue->capacity = capacity;

queue->front = queue->size = 0;

queue->rear = capacity - 1; // This is important, see the enqueue

queue->array = new int[(queue->capacity * sizeof(int))];

return queue;

}

// Queue is full when size

// becomes equal to the capacity

int isFull(Queue* queue)

{ return (queue->size == queue->capacity); }

// Queue is empty when size is 0

int isEmpty(Queue* queue)

{ return (queue->size == 0); }

// Function to add an item to the queue.

// It changes rear and size

void enqueue(Queue* queue, int item)

{

if (isFull(queue))

return;

queue->rear = (queue->rear + 1) % queue->capacity;

queue->array[queue->rear] = item;

queue->size = queue->size + 1;

cout << item << " enqueued to queue\n";

}

// Function to remove an item from queue.

// It changes front and size

int dequeue(Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

int item = queue->array[queue->front];

queue->front = (queue->front + 1) % queue->capacity;

queue->size = queue->size - 1;

return item;

}

// Function to get front of queue

int front(Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->front];

}

// Function to get rear of queue

int rear(Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->rear];

}

// Driver code

int main()

{

Queue* queue = createQueue(1000);

enqueue(queue, 10);

enqueue(queue, 20);

enqueue(queue, 30);

enqueue(queue, 40);

cout<<dequeue(queue)<<" dequeued from queue\n";

cout << "Front item is " << front(queue) << endl;

cout << "Rear item is " << rear(queue) << endl;

return 0;

}

// This code is contributed by rathbhupendra

Output:

10 enqueued to queue

20 enqueued to queue

30 enqueued to queue

40 enqueued to queue

10 dequeued from queue

Front item is 20

Rear item is 40

Time Complexity:Time complexity of all operations like enqueue(), dequeue(), isFull(), isEmpty(), front() and rear() is O(1). There is no loop in any of the operations.Likes:

Be first to like this answer

Dislikes:

Be first to dislike this answer

Talk about this Like Dislike

## You might like this video:Network and IP Address

Watch more here

## Watch more videos from this user Here

Learn how to upload a video over here

Lets together make the web is a better placeWe made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams"with these.Answer a question:You can answer the questions not yet answered in eduladder.How to answer a questionCareer:Work or do your internship with us.Work with usCreate a video:You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder