0% found this document useful (0 votes)
49 views

03 Lec3 Linear and Circular Queues Using Arrays

Uploaded by

fmlomat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

03 Lec3 Linear and Circular Queues Using Arrays

Uploaded by

fmlomat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture 3: Queues & Circular Queues

Dr.\ Ahmed Ibrahim


Dr.\ Lamiaa Hassaan
What is a queue?
 A queue is a non-primitive linear data structure.
 It is open at both ends and the operations are
performed in First In First Out (FIFO) order.
 We define a queue to be a list in which all insertions to
the list are made at one end (tail or rear), and all
deletions from the list are made at the other end (head
or front).
FIFO Principle of Queue
 A Queue is like a line waiting to purchase tickets, where
the first person in line is the first person served.
 Position of the entry in a queue ready to be served, that
is, the first entry that will be removed from the queue,
is called the front of the queue (sometimes, head of
the queue), similarly, the position of the last entry in
the queue, that is, the one most recently added, is
called the rear (or the tail) of the queue.
C++ enqueue function
bool isFull(int tail)
{
if ( tail== maxsize-1)
return true;
else return false;
}
void enqueue(int item)
{
if(isFull(tail))
{ cout<<"\nQueue is full"; return;}
else
{
if ( head == -1 ) head = 0;
tail = tail + 1;
queue[tail] = item;
}
}
C++ dequeue function
bool isEmpty(int head)
{
if (head== -1|| head>tail) return true;
else return false;
}
int dequeue()
{ int element;
if (isEmpty(head))
{
cout<<"Queue Underflow ";
return -1;
}
else {
element= queue[head] ;
head++;
}

return element;
}
Linear Implementation
0 1 2 3 4 5 6 7

Front Rear

6
Insert
0 1 2 3 4 5 6 7

Front Rear

7
Insert
0 1 2 3 4 5 6 7

Front Rear

8
Delete
0 1 2 3 4 5 6 7

Front Rear
This comes off the
queue

9
Delete
0 1 2 3 4 5 6 7

Front Rear

This comes off the


queue

10
Insert
0 1 2 3 4 5 6 7

Front Rear

11
Insert
0 1 2 3 4 5 6 7

Front Rear

NO SPACE
HERE
12
Insert
0 1 2 3 4 5 6 7

Front Rear
SPACE
HERE

13
Problem of linear queue

When a linear queue is full, it's not possible to insert more


elements. Even when an element is dequeued, the front of
the queue moves forward, thereby reducing the overall size of
the queue. New elements cannot be enqueued because
the tail pointer is still at the end of the queue.
Circular Queue
• A circular queue is the extended version of a regular
queue where the last element is connected to the
first element. Thus forming a circle-like structure.
• The circular queue solves the major limitation of
the linear queue. In a linear queue, after a bit of
insertion and deletion, there will be non-usable
empty space.
Operations of Circular Queue
The circular queue work as follows:
• Two pointers head and tail, head tracks the first
element of the queue and tail tracks the last elements
of the queue
• Initially, the head and the tail pointers will be
pointing to the same location (-1), this would mean
that the queue is empty.
Operations of Circular Queue
Enqueue Operation
 Check if the queue is full with two additional cases:
• Case 1: head = 0 && tail == SIZE – 1
• Case 2: tail == (head – 1) % MAXSIZE – 1: happens when
tail value is just 1 less than head, the queue is full.
 For the first element, set value of head to 0
 Circularly increase the tail index by 1 (i.e. if the tail reaches the
end, then set the tail to position 0.
 Add the new element in the position pointed to by tail
Operations of Circular Queue
Dequeue Operation
• Check if the queue is empty
• Return the value pointed by head
• Circularly increase the head index by 1
• for the last element, reset the values of head and tail to -1
Operations of Circular Queue
When the Dequeue operation is performed on both the
queues: Consider the first 2 elements that are deleted
from both the queues. In both the queues the front
points at element 72 and the rear points at
element 24 as illustrated below:
Operations of Circular Queue
Now again enqueue operation is performed: Consider an
element with a value of 100 is inserted in both the queues.
The insertion of element 100 is not possible in Linear
Queue but in the Circular Queue, the element with a value
of 100 is possible as illustrated below:
C++ functions of circular queue: enqueue( )
bool isFull () {
if ((front == 0 && rear == MAX_SIZE - 1) || (rear == (front - 1) % (MAX_SIZE - 1)))
{ return true; }
return false;
}
void enQueue (int value)
{ if (isFull ()) { cout << "Queue is full." << endl; }
else
{ if (front == -1)
{ front = 0; }
rear = (rear + 1) % MAX_SIZE;
arr[rear] = value;
cout << "Enqueued element: " << value << endl;
}
}
C++ functions of circular queue: dequeue( )
bool isEmpty ()
{ if (front == -1)
{ return true; }
return false;
}
int deQueue ()
{ int element;
if (isEmpty ())
{ cout << "Queue is empty." << endl; return -1; }
else
{ element = arr[front];
if (front == rear)
{ front = -1; rear = -1; }
else { front = (front + 1) % MAX_SIZE; }
cout << "Dequeued element: " << element << endl;
return element;
}
}

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