Queue Data Structure Studytonight

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

What is a Queue Data Structure?

Queue is also an abstract data type or a linear data structure, just like
stack data structure, in which the first element is inserted from one
end called the REAR(also called tail), and the removal of existing
element takes place from the other end called as FRONT(also called
head).

This makes queue as FIFO(First in First Out) data structure, which


means that element inserted first will be removed first.

Which is exactly how queue system works in real world. If you go to a


ticket counter to buy movie tickets, and are first in the queue, then you
will be the first one to get the tickets. Right? Same is the case with
Queue data structure. Data inserted first, will leave the queue first.

The process to add an element into queue is called Enqueue and the
process of removal of an element from queue is called Dequeue.

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 1 of 8
Basic features of Queue
1. Like stack, queue is also an ordered list of elements of similar
data types.
2. Queue is a FIFO( First in First Out ) structure.
3. Once a new element is inserted into the Queue, all the elements
inserted before the new element in the queue must be removed,
to remove the new element.
4. peek( ) function is oftenly used to return the value of first
element without dequeuing it.

Applications of Queue
Queue, as the name suggests is used whenever we need to manage
any group of objects in an order in which the first one coming in, also
gets out first while the others wait for their turn, like in the following
scenarios:

1. Serving requests on a single shared resource, like a printer, CPU


task scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to
hold people calling them in an order, until a service representative
is free.
3. Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive i.e First come first
served.

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 2 of 8
Implementation of Queue Data Structure
Queue can be implemented using an Array, Stack or Linked List. The
easiest way of implementing a queue is by using an Array.

Initially the head(FRONT) and the tail(REAR) of the queue points at


the first index of the array (starting the index of array from 0). As we
add elements to the queue, the tail keeps on moving ahead, always
pointing to the position where the next element will be inserted, while
the head remains at the first index.

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 3 of 8
When we remove an element from Queue, we can follow two possible
approaches (mentioned [A] and [B] in above diagram). In [A]
approach, we remove the element at head position, and then one by
one shift all the other elements in forward position.

In approach [B] we remove the element from head position and then
move head to the next position.

In approach [A] there is an overhead of shifting the elements one

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 4 of 8
position forward every time we remove the first element.

In approach [B] there is no such overhead, but whenever we move


head one position ahead, after removal of first element, the size on
Queue is reduced by one space each time.

Algorithm for ENQUEUE operation

1. Check if the queue is full or not.


2. If the queue is full, then print overflow error and exit the program.
3. If the queue is not full, then increment the tail and add the
element.

Algorithm for DEQUEUE operation

1. Check if the queue is empty or not.


2. If the queue is empty, then print underflow error and exit the
program.
3. If the queue is not empty, then print the element at the head and
increment the head.

#include<iostream>

using namespace std;

#define SIZE 10

class Queue

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 5 of 8
{
int a[SIZE];
int rear;
int front;

public:
Queue()
{
rear = front = -1;
}

void enqueue(int x);


int dequeue();
void display();
};

void Queue :: enqueue(int x)


{
if(front == -1) {
front++;
}
if( rear == SIZE-1)
{
cout << "Queue is full";
}
else
{
a[++rear] = x;
}
}

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 6 of 8
int Queue :: dequeue()
{
return a[++front];
}

void Queue :: display()


{
int i;
for( i = front; i <= rear; i++)
{
cout << a[i] << endl;
}
}

int main()
{
Queue q;
q.enqueue(10);
q.enqueue(100);
q.enqueue(1000);
q.enqueue(1001);
q.enqueue(1002);
q.dequeue();
q.enqueue(1003);
q.dequeue();
q.dequeue();
q.enqueue(1004);

q.display();

return 0;
}

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 7 of 8
To implement approach [A], you simply need to change the dequeue
method, and include a for loop which will shift all the remaining
elements by one position.

return a[0];
for (i = 0; i < tail-1; i++)
{
a[i] = a[i+1];
tail--;
}

Complexity Analysis of Queue Operations

Just like Stack, in case of a Queue too, we know exactly, on which


position new element will be added and from where an element will be
removed, hence both these operations requires a single step.

Enqueue: O(1)
Dequeue: O(1)
Size: O(1)

https://www.studytonight.com/data-structures/queue-data-structure 11/11/19, 11:18 AM


Page 8 of 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy