 The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we help you to solve your academic and programming questions fast.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
See Our team
Wondering how we keep quality?
Got unsolved questions?

Data-StructuresDS-10CS35-->View question

## 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?

Taged users:
komaleduAlfaqshubhamrdardapranaymasaneAnirudh

Likes:
Annaliya

Dislikes:
Be first to dislike this question

# Difference Between Stack and Queue

## BASIS FOR COMPARISONSTACKQUEUEWorking principleLIFO (Last in First out)FIFO (First in First out)StructureSame end is used to insert and delete elements.One end is used for insertion, i.e., rear end and another end is used for deletion of elements, i.e., front end.Number of pointers usedOneTwo (In simple queue case)Operations performedPush and PopEnqueue and dequeueExamination of empty conditionTop == -1Front == -1 || Front == Rear + 1Examination of full conditionTop == Max - 1Rear == Max - 1VariantsIt does not have variants.It has variants like circular queue, priority queue, doubly ended queue.ImplementationSimplerComparatively complexArray implementation Of Queue For 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 queue20 enqueued to queue30 enqueued to queue40 enqueued to queue10 dequeued from queueFront item is 20 Rear item is 40Time 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

### Watch more videos from this user Here

Learn how to upload a video over here

# Difference Between Stack and Queue

## BASIS FOR COMPARISONSTACKQUEUEWorking principleLIFO (Last in First out)FIFO (First in First out)StructureSame end is used to insert and delete elements.One end is used for insertion, i.e., rear end and another end is used for deletion of elements, i.e., front end.Number of pointers usedOneTwo (In simple queue case)Operations performedPush and PopEnqueue and dequeueExamination of empty conditionTop == -1Front == -1 || Front == Rear + 1Examination of full conditionTop == Max - 1Rear == Max - 1VariantsIt does not have variants.It has variants like circular queue, priority queue, doubly ended queue.ImplementationSimplerComparatively complexArray implementation Of Queue For 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 queue20 enqueued to queue30 enqueued to queue40 enqueued to queue10 dequeued from queueFront item is 20 Rear item is 40Time 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

### Watch more videos from this user Here

Learn how to upload a video over here

Lets together make the web is a better place

We 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.