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

Lecture 03.2

The document discusses queue data structures. It defines a queue as a first-in, first-out (FIFO) container with two ends - a front and rear. Common queue operations like enqueue, dequeue, isEmpty and isFull are described. Pseudocode is provided to simulate queue operations on an array-based implementation, including adding elements to the rear, removing elements from the front, and checking for empty and full conditions. Examples show the state of the queue and how the front and rear pointers change during operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Lecture 03.2

The document discusses queue data structures. It defines a queue as a first-in, first-out (FIFO) container with two ends - a front and rear. Common queue operations like enqueue, dequeue, isEmpty and isFull are described. Pseudocode is provided to simulate queue operations on an array-based implementation, including adding elements to the rear, removing elements from the front, and checking for empty and full conditions. Examples show the state of the queue and how the front and rear pointers change during operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Queue

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 3.2 Week No: 3 Semester: Fall 2020-2021


Lecturer: MAHFUJUR RAHMAN, [email protected]
Lecture Outline

1. Queue
i. Definition
ii. Queue in Computer Language
iii. Simulation of Operations
iv. Pseudo code of Queue Implementation
2. Books
3. References
Queue
Definition

 A queue is a waiting line – seen in daily life.

 A line of people waiting for a bank teller.

 A line of cars at a toll both.

 Queue data structure is like a container with both end open.


 An end called rear
 Another end is front

 This mechanism is called First-In-First-Out (FIFO).

 Some of the applications are :


 Device queue, printer queue, keystroke queue, etc.
Queue in Computer Language

 A queue is a sequence of data elements front


 In the sequence 1 4 3
 Items can be added only at the rear
 Items can be removed only at the other end, front
rear
 Basic operations
 Check IsEmpty
 Check IsFull
 EnQueue (add element to back i.e. at the rear)
 DeQueue (remove element from the front)
 FrontValue (retrieve value of element from front)
 ShowQueue (print all the values of queue from front to rear)
Simulation of Operations

front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue
Initialize( )  front=rear=-1
 Check IsEmpty
 Check IsFull rear

 EnQueue (add element


to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue(3)  front=rear=0
 Check IsFull rear

 EnQueue (add element


to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear++
 Check IsFull rear

 EnQueue (add element


to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear++
 EnQueue (add element
to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear = 2
 EnQueue (add element EnQueue( 5 )  rear++
to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear = 2
 EnQueue (add element EnQueue( 5 )  rear = 3
to back i.e. at the rear) EnQueue( 9 )  Queue Full, if (rear==(MaxSize-1))
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear = 2
 EnQueue (add element EnQueue( 5 )  rear = 3
to back i.e. at the rear) EnQueue( 9 )  Queue Full, if (rear==(MaxSize-1))
DeQueue()  front++
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear = 2
 EnQueue (add element EnQueue( 5 )  rear = 3
to back i.e. at the rear) EnQueue( 9 )  Queue Full, if (rear==(MaxSize-1))
DeQueue()  front = 1
 DeQueue (remove DeQueue()  front++
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear = 2
 EnQueue (add element EnQueue( 5 )  rear = 3
to back i.e. at the rear) EnQueue( 9 )  Queue Full, if (rear==(MaxSize-1))
DeQueue()  front = 1
 DeQueue (remove DeQueue()  front = 2
element from the front) DeQueue()  front++
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear = 2
 EnQueue (add element EnQueue( 5 )  rear = 3
to back i.e. at the rear) EnQueue( 9 )  Queue Full, if (rear==(MaxSize-1))
DeQueue()  front = 1
 DeQueue (remove DeQueue()  front = 2
element from the front) DeQueue()  front = 3
DeQueue()  front=rear=-1, if ((front!=-1) && (front==rear))
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0
EnQueue( 6 )  rear = 1
 Check IsFull rear
EnQueue( 2 )  rear = 2
 EnQueue (add element EnQueue( 5 )  rear = 3
to back i.e. at the rear) EnQueue( 9 )  Queue Full, if (rear==(MaxSize-1))
DeQueue()  front = 1
 DeQueue (remove DeQueue()  front = 2
element from the front) DeQueue()  front = 3
 FrontValue (retrieve DeQueue()  front=rear=-1, if ((front!=-1) && (front==rear))
DeQueue()  Queue Empty, if ((front==-1) && (rear==-1))
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Pseudo code of Queue Implementation

Initialize queue[maxSize]; front = rear = -1;

isEmpty(){
if ((front==-1) and (rear==-1)):
then return true;
}

isFull(){
if rear==(maxSize-1):
then return true;
}

enqueue(x){
if(queue full): {error: “queue full!”;}
otherwise if(queue is empty): {front=rear=0; insert x in queue[rear]}
otherwise: rear++; insert x in queue[rear];
}
Pseudo code of Queue Implementation

dequeue(){
if(queue empty): {error: “queue is empty! dequeue not possible”}
otherwise if (front and rear are equal): {front=rear=-1;}
otherwise: {front++;}
}

frontElement(){
return queue[front];
}

showQueue(){
if (queue empty)
error: “cannot show queue because it is empty”;
otherwise:
for: i=front; i<=rear; i++
output: queue[i];
}
Books

 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard


 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,
References

 https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
 https://www.cs.usfca.edu/~galles/visualization/QueueArray.html (This is a great site
for visualizing stack operations)
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008.
[Chapter 1: 1.2]

You might also like