Sie sind auf Seite 1von 24

Data Structure & Algorithms

Chapter 5:
Queue
Imran Ali Memon
Lecturer (IT Department)

Rear Front
QUEUE
Contents
5.1 - Introduction
5.2 – Basic Operations
5.3 – Types of representation of queue in memory
5.4 – enqueue( ) Algorithm
5.5 – dequeue( ) Algorithm
5.6 – Types of queues

Imran Ali Memon (Lecturer IT Department) 2


Introduction
Queue: (FIFO)
• Queue is a non-primitive linear data structure that
permits insertion of an element at one end and
deletion at the other end.
• The end at which insertion of a new element can
take place is called rear.
• The end at which the deletion of an element take
place is called front of an element at the other end.

Imran Ali Memon (Lecturer IT Department) 3


Queue (FIFO)
• The first element that gets added into the queue is
the first one to get removed from the list.
• Hence, Queue is also referred to as First-In-
First-Out (FIFO) list.
• In CS queue occurs in a timesharing queue, printer
queue, keystroke queue etc.

Imran Ali Memon (Lecturer IT Department) 4


Queue

Imran Ali Memon (Lecturer IT Department) 5


Basic Operations
Primary queue operations:
• Insertion (Enqueue): insert an element at the rear
of the queue
• Deletion (Dequeue): remove an element from the
front of the queue

Imran Ali Memon (Lecturer IT Department) 6


Queue Operations

Imran Ali Memon (Lecturer IT Department) 7


There are two ways to represent
Queue in memory.
1. Array (Static)
2. Linked List (Dynamic)

Imran Ali Memon (Lecturer IT Department) 8


Linked List Representation of Queue
(Dynamic)
Head
1001

AAA 1050 BBB 2000 CCC 3901 DDD


1001 1050 2000 3901

Front = 3901
Rear = 1001

Imran Ali Memon (Lecturer IT Department) 9


Linked List Representation of Queue(Dynamic)
Head
1001

AAA 1050 BBB 2000 CCC 3901 DDD


1001 1050 2000 3901

Front = 3901
Rear = 1001
Head
1111
enqueue()
AAA 1050 BBB 2000 CCC 3901 DDD
1001 1050 2000 3901

Front = 3901
New 1001
1111 Imran Ali Memon (Lecturer IT Department)
Rear = 1111 10
Linked List Representation of Queue(Dynamic)
Head
1001

AAA 1050 BBB 2000 CCC 3901 DDD


1001 1050 2000 3901

Front = 3901
Rear = 1001
Head
1001
dequeue()
AAA 1050 BBB 2000 CCC
1001 1050 2000

Front = 2000
Imran Ali Memon (Lecturer IT Department)
Rear = 1001 11
Array Representation of Queue
• Usually the queues are represented in the computer
by a linear array.
• Each of our queues will be maintained by a linear
array QUEUE and two pointer variables FRONT and
REAR.
• FRONT containing the location of the front element
of the queue.
• REAR containing the location of the rear element of
the queue .
• FRONT = NULL indicates the queue is empty.
Imran Ali Memon (Lecturer IT Department) 12
Array Representation of Queue
Next slide shows:
• The way the array will be stored in memory using an array
QUEUE with N elements.
• Way the elements will be deleted from queue.
• Way new elements will be added to the queue.
• Whenever an element is deleted from the queue, the value
of FRONT is increased by 1.
• FRONT = FRONT + 1;
• Whenever an element is added to the queue, the value of
REAR is increased by 1.
• REAR = REAR + 1; Imran Ali Memon (Lecturer IT Department) 13
Array representation of the queue
F R
0 1 2 3 4 5 6 7 8 9 10 11
FRONT = 0
REAR= 3 A B C D

0 1 2 3 4 5 6 7 8 9 10 11
FRONT= 1
REAR = 3 B C D

0 1 2 3 4 5 6 7 8 9 10 11
FRONT = 1
REAR= 4 B C D E

FRONT=2 0 1 2 3 4 5 6 7 8 9 10 11
REAR = 5 C D E F
Imran Ali Memon (Lecturer IT Department) 14
Array representation of the queue
0 1 2 3 4 5 6 7 8 9 10 11

REAR = FRONT = NULL


Empty Queue

0 1 2 3 4 5 6 7 8 9 10 11
FRONT = 0
REAR= 0
A

Imran Ali Memon (Lecturer IT Department) 15


0 1 2 3 4 5 6 7 8 9 10 11
FRONT = 0
REAR= 2
A B C
MAXSIZE = 12.

Imran Ali Memon (Lecturer IT Department) 16


0 1 2 3 4 5 6 7 8 9 10 11
FRONT = 0
REAR= 2
A B C
MAXSIZE = 12.

Imran Ali Memon (Lecturer IT Department) 17


Analyze ??

Imran Ali Memon (Lecturer IT Department) 18


Types of Queue (Priority Queue)
• A priority queue is a collection of elements such that
each element has been assigned a priority.
• The order in which elements are deleted and
processed comes form the following rules.
1. An element of higher priority is processed before any
element of lower priority.
2. Two elements with the same priority are processed
according to the order in which they were added to
the queue.
• Prototype of a priority queue is a timesharing system.

Imran Ali Memon (Lecturer IT Department) 19


Representation of Priority Queues in Memory
• There are mainly two ways of maintaining a priority
queue in memory.

1. Using one way list


2. Using multiple queues

Imran Ali Memon (Lecturer IT Department) 20


One-way List Representation of a Priority Queue :

INFO PRN LINK

Node

Imran Ali Memon (Lecturer IT Department) 21


One-way List Representation of a Priority Queue :
• Each node in the list will contain three items of information;
an information field INFO, a priority number PRN and a link
number LINK.
• A node X precedes a node Y in the list
• (I) When X has higher priority then Y and
• (II) When both have the same priority but X is added to the
list before Y.
• This means that the order in the one-way list corresponds to
the order of the priority queue.
• Priority queues will operate in the usual way : the lower the
priority number, the higher the priority.

Imran Ali Memon (Lecturer IT Department) 22


Multiple Queues Representation of a
Priority Queue :

Imran Ali Memon (Lecturer IT Department) 23


Types of Queue (DEQUE)
DEQUE:
• A deque (deck or dequeue) is a linear list in which
elements can be added or removed at either end
but not in the middle.
• The term deque is a contraction of the name
double-ended-queue.

Imran Ali Memon (Lecturer IT Department) 24

Das könnte Ihnen auch gefallen