0% found this document useful (0 votes)
3 views25 pages

Lec 09

A queue is a data structure that follows the First In First Out (FIFO) principle, where elements are added at the rear and removed from the front. Key operations include EnQueue for adding elements and DeQueue for removing them, along with auxiliary operations like checking if the queue is empty or getting the front element. Queues are widely used in applications such as job scheduling in operating systems and simulating real-world queues, with implementations possible through arrays, circular arrays, or linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

Lec 09

A queue is a data structure that follows the First In First Out (FIFO) principle, where elements are added at the rear and removed from the front. Key operations include EnQueue for adding elements and DeQueue for removing them, along with auxiliary operations like checking if the queue is empty or getting the front element. Queues are widely used in applications such as job scheduling in operating systems and simulating real-world queues, with implementations possible through arrays, circular arrays, or linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Queues

1
What is a Queue?
• A queue is a data structure used for storing
data.
• In queue, the order in which data arrives is
important.
• In general, a queue is a line of people or things
waiting to be served in sequential order starting
at the beginning of the line or sequence.

2
What is a Queue?
• Definition: A queue is an ordered list in which
insertions are done at one end (rear) and
deletions are done at other end (front). The first
element to be inserted is the first one to be
deleted. Hence, it is called First in First out
(FIFO) list.

3
What is a Queue?
• Similar to Stacks, special names are given to the
two changes that can be made to a queue.
• When an element is inserted in a queue, the
concept is called EnQueue,
• and when an element is removed from the
queue, the concept is called DeQueue.

4
What is a Queue?
• DeQueueing an empty queue is called underflow

• EnQueuing an element in a full queue is called


overflow.

• Generally, we treat them as exceptions.

5
What is a Queue?

6
How are Queues Used?
• Queue ADT
– The following operations make a queue an ADT.
– Insertions and deletions in the queue must follow the
FIFO scheme. For simplicity we assume the elements
are integers.
• Main Queue Operations
– EnQueue(int data): Inserts an element at the end of
the queue
– int DeQueue(): Removes and returns the element at
the front of the queue

7
How are Queues Used?
• Auxiliary Queue Operations
– int Front(): Returns the element at the front without
removing it
– int QueueSize(): Returns the number of elements
stored in the queue
– int IsEmptyQueueQ: Indicates whether no elements
are stored in the queue or not

8
Queue Applications
• Following are some of the applications that use
queues.
– Operating systems schedule jobs (with equal priority) in
the order of arrival (e.g., a print queue).
– Simulation of real-world queues such as lines at a ticket
counter or any other first-come first-served scenario
requires a queue.
– Determining number of cashiers to have at a
supermarket.

9
Queue Implementations
• There are many ways of implementing queue
operations and some of the commonly used
methods are listed below.
– Array based implementation

– Circular array based implementation

– Linked list implementation

10
Why Circular Arrays?
• First, let us see whether we can use simple
arrays for implementing queues as we have
done for stacks.
• We know that, in queues, the insertions are
performed at one end and deletions are
performed at the other end.
• After performing some insertions and deletions
the process becomes easy to understand.

11
Why Circular Arrays?
• In the following example, it can be seen clearly that the
initial slots of the array are getting wasted.
• So, simple array implementation for queue is not
efficient.

12
Why Circular Arrays?
• To solve this problem we assume the arrays as
circular arrays.
• That means, we treat the last element and the
first array elements as contiguous.
• With this representation, if there are any free
slots at the beginning, the rear pointer can easily
go to its next free slot.

13
Simple Circular Array Implementation

14
Simple Circular Array Implementation
• This simple implementation of Queue ADT uses
an array.
• In the array, we add elements circularly and use
two variables to keep track of the start element
and end element.
• Generally, front is used to indicate the start
element and rear is used to indicate the end
element in the queue.

15
Simple Circular Array Implementation
• The array storing the queue elements may
become full.
• An EnQueue operation will then throw a full
queue exception.
• Similarly, if we try deleting an element from an
empty queue it will throw empty queue
exception.

16
Simple Circular Array Implementation
• Note:
– Initially, both front and rear points to -1 which
indicates that the queue is empty.
• Limitations:
– The maximum size of the queue must be
defined as prior and cannot be changed.

17
isFull
bool isFull()
{
if(front == 0 && rear == SIZE - 1)
return true;
else if(front == rear + 1)
return true;
else
return false;
}

18
isEmpty
bool isEmpty()
{
if(front == -1)
return true;
else
return false;
}

19
enQueue
void enQueue(int element)
{
if(isFull())
cout << "Queue is full";
else
{
if(front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
cout << endl << "Inserted " << element << endl;
}
}
20
deQueue
void deQueue()
{
if(isEmpty())
cout << "Queue is empty" << endl;
else if(front == rear) /* Q has only one
element*/
front = rear = -1;
else
front=(front+1) % SIZE;
}
21
peek
int peek()
{
if(isEmpty())
{
cout << "Queue is empty" << endl;
return -1;
}
else
return items[front];
}
22
display
void display() {
if(isEmpty())
cout << endl << "Empty Queue" << endl;
else {
cout << "Front -> " << front;
cout << endl << "Items -> ";
for(int i=front; i!=rear; i=(i+1)%SIZE)
cout << items[i];
cout << items[i];
cout << endl << "Rear -> " << rear;
}
}

23
makeEmpty
void makeEmpty()
{

front = rear = -1;

24
Linked List Implementation
• Another way of implementing queues is by using Linked
lists.
• EnQueue operation is implemented by inserting an
element at the end of the list.
• DeQueue operation is implemented by deleting an
element from the beginning of the list.

25

You might also like