0% found this document useful (0 votes)
6 views35 pages

Lect#4 Data Structure (Queue)

The document provides an overview of queues, a linear data structure where elements are added at the rear and removed from the front, following the First In First Out (FIFO) principle. It discusses various operations such as enqueue, dequeue, and methods of implementing queues using linked lists and arrays, including circular arrays. Additionally, it covers priority queues, which process elements based on assigned priorities, and outlines algorithms for insertion and deletion in circular queues.
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)
6 views35 pages

Lect#4 Data Structure (Queue)

The document provides an overview of queues, a linear data structure where elements are added at the rear and removed from the front, following the First In First Out (FIFO) principle. It discusses various operations such as enqueue, dequeue, and methods of implementing queues using linked lists and arrays, including circular arrays. Additionally, it covers priority queues, which process elements based on assigned priorities, and outlines algorithms for insertion and deletion in circular queues.
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/ 35

Data Structure

BCS - III

By
Islam Zada
Lecturer in Department of
Software Engineering

1 BSSE FOC&IT-IIU Islamabad


Queues

2
QUEUE

● A linear data structure into which items can


only be inserted at one end called rear and
removed from the other called front.
● Queue is very useful in computer science.
● We define a queue to be a list in which all
additions to the list is made at the one end &
all deletions from the list is made at other end.
● Queue are also called First In First Out list of
FIFO for sort.
3
QUEUE
● We may draw queue in any one of the forms as given below

4
Queue Operations

Enqueue(X) – insert X at the rear of the


queue.
Dequeue() -- remove the front element from
queue.
Front() -- return front element.
IsEmpty() -- return TRUE if queue is empty,
FALSE otherwise

5
IMPLEMENTING QUEUE

● There are mainly two types of queue,


– Priority queue.
– Circular queue.
● Queue can be implemented using
either
– Linked list or
– Array

6
Implementing Queue

▪ Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

7
Implementing Queue

▪ Using linked List:


front rear front rear

1 7 5 2 1 7 5 2

dequeue()

front rear front rear

7 5 2 1 7 5 2

8
Implementing Queue

▪ Using linked List:


front rear front rear

1 7 5 2 1 7 5 2

enqueue(9)

front rear front rear

7 5 2 9 7 5 2 9

9
Queue using Array

▪ If we use an array to hold queue elements,


both insertions and removal at the front (start)
of the array are expensive.
▪ This is because we may have to shift up to
“n” elements.
▪ For the stack, we needed only one end; for
queue we need both.
▪ To get around this, we will not shift upon
removal of an element.
10
Queue using Array

front rear
1 7 5 2
1 7 5 2
0 1 2 3 4 5 6 7
front rear
0 3

11
Queue using Array

enqueue(6)

front rear
1 7 5 2 6
1 7 5 2 6
0 1 2 3 4 5 6 7
front rear
0 4

12
Queue using Array

enqueue(8)

front rear
1 7 5 2 6 8
1 7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
0 5

13
Queue using Array

dequeue()

front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
1 5

14
Queue using Array

dequeue()

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
front rear
2 5

15
Queue using Array

enqueue(9)
enqueue(12)

front rear
5 2 6 8 9 12
5 2 6 8 9 12
0 1 2 3 4 5 6 7
front rear
enqueue(21) ?? 2 7

16
Queue using Array

▪ We have inserts and removal running in


constant time but we created a new
problem.
▪ Cannot insert new elements even though
there are two places available at the start of
the array.
▪ Solution: allow the queue to “wrap around”.

17
Queue using Array

▪ Basic idea is to picture the array as a circular


array.

0 1
front
front rear
7 2 2
12 5
5 2 6 8 9 12
6 9 2
8 6 3 rear
7
5 4

18
Queue using Array

enqueue(21)

0 1
front rear 21 front size
7 2 2 8
12 5
5 2 6 8 9 12 21
6 9 2
8 6 3 rear No Elements
0 7
5 4

19
Queue using Array

enqueue(7)

0 1
front rear 21 7 front size
7 2 2 8
12 5
5 2 6 8 9 12 21 7
6 9 2
8 6 3 rear noElements
1 8
5 4

20
Queue using Array

dequeue()

0 1

front rear 21 7 front size


7 2 4 8
12
6 8 9 12 21 7
6 9
8 6 3 rear noElements
1 6
5 4

21
Circular Array

22
Circular Array

23
Circular Array

24
Circular Array

25
Circular Array

26
Circular Array

27
Circular Array

28
Circular Array

29
Circular Array

30
INSERTION TO A QUEUE
Algorithm: ENQUEUE(QUEUE, MAXSIZE, FRONT, REAR,COUNT, ITEM)
This algorithm inserts an element ITEM into a circular queue.
1. [QUEUE already filled?]
If COUNT = MAXSIZE then: [ COUNT is number of values in the QUEUE]
Write: OVERFLOW, and Return.
2. [Find new value of REAR.]
If COUNT= 0, then: [Queue initially empty.]
Set FRONT= 0 and REAR = 0
Else: if REAR = MAXSIZE - 1, then:
Set REAR = 0
Else:
Set REAR = REAR+1.
[End of If Structure.]
3. Set QUEUE[REAR] = ITEM. [This insert new element.]
4. COUNT=COUNT+1 [ Increment to Counter. ]
5. Return.
31
DELETION FROM A QUEUE
Algorithm: DEQUEUE(QUEUE, MAXSIZE, FRONT, REAR,COUNT, ITEM)
This procedure deletes an element from a queue and assigns it to the variable
ITEM.
1. [QUEUE already empty?]
If COUNT= 0, then: Write: UNDERFLOW, and Return.
2. Set ITEM = QUEUE[FRONT].
3. Set COUNT = COUNT -1
4. [Find new value of FRONT.]
If COUNT = 0, then: [There was one element and has been deleted ]
Set FRONT= -1, and REAR = -1.
Else if FRONT= MAXSIZE, then: [Circular, so set Front = 0 ]
Set FRONT = 0
Else:
Set FRONT:=FRONT+1.
[End of If structure.]
5. Return ITEM
32
DEQUEUE

● DEQUEUE---🡪 Double Ended Queue


● Elements can be added or removed at either end but
not in the middle
● A DEQUEUE is maintained by a circular array with
pointers LEFT and RIGHT which points to the two ends
of dequeue
● Variations of dequeue
– Input restricted dequeue
– Output restricted dequeue

33
PRIORITY QUEUE

● A collection of elements such that each


element has been assigned a priority and such
that the order in which elements are deleted
and processed comes from the following rules
– An element of higher priority is processed before
any element of lower priority.
– Two elements with the same priority are processed
according to the order in which they were added to
the queue

34
PRIORITY QUEUE

● Priority queue can be maintained in computer


memory either
– As a one way list
● Each node in the list contains three items of information
– Information field INFO
– Priority number PRN
– Link number LINK

– Array representations of priority queues


● Multiple queues are maintain for each level of priority
● Each such queue appears in its own circular array
● A two-dimensional array can be used instead of multiple arrays
where every row index corresponds to a priority number.
35

You might also like