You are on page 1of 28

University of Computer Studies

Linear Data Structure(Queue)

Daw Su Su Win

Lecturer

Faculty of Computer Science

University of Computer Studies, FCS

1
Lecture Objectives FCS

 To learn about queues

 To examine various queue operations

 To learn how to implement a queue as an array &


Linked List

 To know about types of queue

University of Computer Studies, FCS 2


What is a Queue? FCS

Definition
-queue is a linear data structure type which is used to organized the data.
- It is used for temporary storage of the data values.
- A new element is added at one end called rear.
- The existing element deleted from the other end is called front.
- First-in-First-Out(FIFO) property

Working of Queue
Queue operations work as follows:
 two pointers FRONT and REAR
 FRONT track the first element of the queue
 REAR track the last element of the queue
 initially, set value of FRONT and REAR to -1

University of Computer Studies, FCS


Queue FCS

Definition Methods of Queue


A queue is a data structure used to model a • Enqueue: places an item at the back of
First-In-First-Out (FIFO) strategy. Conceptually, the queue;
we add to the end of a queue and take away • Dequeue: retrieves/remove the item at
elements from its front. the front of the queue, and removes it
from the queue;
 IsEmpty: Check if the queue is empty
Types of Queue
 IsFull: Check if the queue is full
• A simple queue
 Peek: Get the value of the front of the
• Circular queue
queue without removing it
• Priority Queue
 Size: Return the number of elements in
• Double Ended Queue
the queue
Queue
Representation of Queue FCS
There are two ways of implementing the Queue:
Implementation using array: sequential allocation in a Queue can be implemented using an array.
Implementation using Linked list: linked list allocation in a Queue can be implemented using a
linked list

Array representation of Queue


• We can easily represent queue by using linear arrays. There are two variables i.e. front and rear, that are
implemented in the case of every queue. Front and rear variables point to the position from where insertions
and deletions are performed in a queue. Initially, the value of front and rear is -1 which represents an empty
queue. Array representation of a queue containing 5 elements along with the respective values of front and
rear, is shown in the following figure:

University of Computer Studies, FCS 6


Linked List representation of Queue FCS

• In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Each
element of the queue points to its immediate next element in the memory.
• In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear
pointer. The front pointer contains the address of the starting element of the queue while the rear
pointer contains the address of the last element of the queue.

University of Computer Studies, FCS 7


Types of Queues FCS
Queue can be of four types:
o Simple Queue
o Circular Queue(Ring Buffer)
o Priority Queue
o Deque ( Double Ended Queue)

1.Simple Queue-Simple queue defines the simple operation of queue in which insertion occurs at the rear of the list
and deletion occurs at the front of the list.

Dequeue/delete Enqueue/insert
Front Rear

7 2 6 9 1

University of Computer Studies, FCS 8


2.Circular Queue(Ring Buffer) FCS

- a circular queue, the elements act like circular rings. The working of a circular queue is similar to a
simple queue but in a circular queue, the element in the last position is connected to the element in the
first position. The main advantage of a circular queue is that the memory will be utilized in a better way.

University of Computer Studies, FCS 9


3.Priority Queues FCS
• a priority queue is a collection of a finite number of prioritized elements.
• Priority queues are those in which the data can insert or delete elements from any position based on some
fundamental ordering of the elements.
• Elements can be inserted in any order in a priority queue, but when an element is removed from the priority
queue, it is always the one with the highest priority.
• a priority queue is to use a heap data structure (Heap tree).
Priority Queues(Cont’d) FCS
There are two types of priority queue:
Ascending order priority queue: In ascending order priority queue, a lower priority number is given as a higher
priority in a priority.
Descending order priority queue: In descending order priority queue, a higher priority number is given as a
higher priority in a priority.

Fig: (a) ascending order priority queue Fig: (b)descending order priority queue
4.Double-Ended Queue(Deque) FCS

• A double-ended queue is commonly known as a “Deque”


• Deque defines a data structure where elements can be added or deleted at either the front end or
the rear end, but no changes can be made elsewhere in the list
Double-Ended Queue(Cont’d) FCS
Types of deque
There are two types of deque –
 Input restricted queue
 Output restricted queue

Input restricted Queue


In input restricted queue, insertion operation can be performed
at only one end, while deletion can be performed from both
ends.

Output restricted Queue


In output restricted queue, deletion operation can be
performed at only one end, while insertion can be performed
from both ends.
Major Drawback of Linear Queue FCS

• The major drawback of using a linear Queue is that insertion is done only from the rear end.
If the first three elements are deleted from the Queue, we cannot insert more elements even
though the space is available in a Linear Queue. In this case, the linear Queue shows the
overflow condition as the rear is pointing to the last element of the Queue.
Resolving Drawback
• The drawback that occurs in a linear queue is overcome by using the circular queue. If the
empty space is available in a circular queue, the new element can be added in an empty
space by simply incrementing the value of rear. The main advantage of using the circular
queue is better memory utilization.

University of Computer Studies, FCS

14
Enqueue Operation / Insertion FCS
Queue Operations and Specifications

Enqueue - adds an element to the rear of a queue

• Step 1 − Check if the queue is full.

• Step 2 − If the queue is full, produce overflow error and exit.

• Step 3 − If the queue is not full, increment rear pointer to


Figure 1: Operations in Queue
point the next empty space.

• Step 4 − Add data element to the queue location, where the

rear is pointing.

• Step 5 − return success.


University of Computer Studies, FCS 15
Enqueue Operation FCS

Rear Front
D

C B A Before

Rear Front

D C B A After

Figure 2: Operation in Enqueue

University of Computer Studies, FCS 16


Algorithm Of Enqueue FCS
procedure enqueue(data)
if queue is full
return overflow
rear ← rear + 1
queue[rear] ← data //insert data
return true
end procedure

int enqueue (int data)


if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;/insert data
return 1;
end procedure
University of Computer Studies, FCS 17
Dequeue Operation/deletion FCS

• Step 1 − Check if the queue is empty.

• Step 2 − If the queue is empty, produce underflow error and exit.

• Step 3 − If the queue is not empty, access the data where front is pointing.

• Step 4 −Increment front pointer to point to the next available data element.

• Step 5 − Return success.

University of Computer Studies, FCS 18


Dequeue Operation FCS

Rear Front

Before D C B A

Rear Front

After D C B dequeue

Queue
A
Figure 2: Operation in Dequeue

University of Computer Studies, FCS 19


Algorithm Of Dequeue FCS
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
int dequeue()
{
if(isempty()) return 0;
int data = queue[front] ;
front = front + 1;
return data;
}
University of Computer Studies, FCS 20
Simple Queue Vs. Circular Queue FCS

Simple Queue Circular Queue

 A Linear data structure that stores data as a A linear data structure in which the last item
sequence of element similar to a real world connects back to the first item forming a circle.
queue. Possible to enter and removes element from any
 Possible to enter new items from the rear end position.
and remove the items from the front.  Requires less memory.
 Requires more memory.  More efficient.
 Less efficient.

University of Computer Studies, FCS 21


Application of Queues FCS

Some of the commonly used applications are:


 CPU scheduling in operating systems
 Network packet scheduling
 Printer job scheduling
 Breadth-First Search (BFS) algorithm
 Simulation modeling
 Traffic management in communication networks
 Call center management
 Web server request handling
 Bank teller management
 Producer-consumer problem
University of Computer Studies, FCS 22
Lecture Summary FCS

• Firt Ins First Out (FIFO) data structure Implemented as array or linked listLinked

lists: queue

• Types of queue

• Add or Store in Queue called “Enqueue”

• Remove or delete in Queue called “Dequeue”

• Application in Queue

University of Computer Studies, FCS 23


Learning Outcomes FCS

After completing this course satisfactorily, a student will be able to:

1. A list structure with two access points called the front and rear.

2. All insertions (Enqueue) occur at the rear .

3. Varying length (dynamic).

4. Homogeneous components

5. Has a First-In, First-Out characteristic (FIFO)

University of Computer Studies, FCS 24


Queue Example Problem FCS
1.Consider the following queue of numbers, where queue is allocated five memory cell.
Describe the queue as the following operations take place.
front=rear=-1 size=5
a. Add(11)
b. Add(12)
c. Add(13)
d. Delete()
e. Add(14)
f. Delete()
g. Delete()
h. Delete()
i. Delete()
j. Add(15)
k. Add(16)

University of Computer Studies, FCS 25


Queue Example Problem FCS
2.Consider the following queue of characters, where queue is allocated five memory cell.
Describe the queue as the following operations take place.
front=rear=-1 size=5
a. A,B and then C inserted
b. A deleted
c. D and then E inserted
d. B and C deleted
e. F inserted
f. D deleted
g. G and then H inserted
h. E deleted
i. F deleted
j. K inserted
k. G and H deleted
l. K deleted

University of Computer Studies, FCS 26


Queue Example Problem FCS
3.Consider the following queue of characters, where queue is a circular queue which is allocated six memory cell.
Describe the queue as the following operations take place.
front=1, rear=3, QUEUE:___, A , C, D, ___, ____
a. F is added to the queue
b. Two letters are deleted
c. K, L and M are added to the queue
d. Two letters are deleted
e. R is added to the queue
f. Two letters are deleted
g. S is added to the queue
h. Two letters are deleted
i. One letters are deleted
j. One letters are deleted

University of Computer Studies, FCS 27


Queue Example Problem FCS
4.Consider the following deque of characters, where DEQUE is a circular array which is allocated six memory cell.
Describe the queue as the following operations take place.
LEFT=1, RIGHT=3, DEQUE:___, A , C, D, ___, ____
a. F is added to the right of the deque
b. Two letters on the right are deleted
c. K, L and M are added to the left of the deque
d. One letter on the left is deleted
e. R is added to the left of the queue
f. F is added to the right of the deque
g. T is added to the right of the deque

University of Computer Studies, FCS


28

You might also like