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

Data Structures: Mahesh Goyani

The document discusses queues as a data structure. Some key points: 1) A queue is a first-in, first-out (FIFO) linear data structure that allows insertion at one end (the rear) and deletion at the other (the front). 2) Queues can be implemented using arrays or linked lists. Array implementation allows efficient insertion but costly removal, while linked lists allow efficient insertion and removal. 3) Common queue operations include enqueue (insert at rear), dequeue (remove at front), check for overflow and underflow. Pseudocode for these operations is provided. 4) A circular queue is also discussed, which uses array indices cyclically to avoid overflow.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Data Structures: Mahesh Goyani

The document discusses queues as a data structure. Some key points: 1) A queue is a first-in, first-out (FIFO) linear data structure that allows insertion at one end (the rear) and deletion at the other (the front). 2) Queues can be implemented using arrays or linked lists. Array implementation allows efficient insertion but costly removal, while linked lists allow efficient insertion and removal. 3) Common queue operations include enqueue (insert at rear), dequeue (remove at front), check for overflow and underflow. Pseudocode for these operations is provided. 4) A circular queue is also discussed, which uses array indices cyclically to avoid overflow.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 30

DATA

STRUCTURES

MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
[email protected]

(C) GOYANI MAHESH 1


QUEUE

(C) GOYANI MAHESH 2


TERMINOLOGY

 QUEUE in real life: Queue for tickets, Train

 Non – Primitive, Linear Data Structure.

 Last In Last Out (LIFO)

 Operation can be performed at Both the end

 Insertion can be done at REAR end

 Push : rear++

 Deletion can be done at FRONT end

 Pop : front++

 Queue Full + Push() = Overflow

 Queue Empty + Pop() = Underflow

 Total Elements in Queue: REAR – FRONT + 1

(C) GOYANI MAHESH 3


ALGORITHM INSERT (STACK, FONT, REAR, X)

1. [Check for overflow]


If (REAR >= SIZE -1) then
“QUEUE overflow”
Return.

2. [Increment RAER pointer]


REAR = REAR + 1

3. [Insert Element]
QUEUE[REAR] = X

4. [Exit]

(C) GOYANI MAHESH 4


ALGORITHM REMOVE (STACK, FONT, REAR, X)

1. [Check for Underflow]


If (FRONT > REAR ||
FRONT = -1 ) then
“QUEUE Underflow”
Return.

2. [Read Element]
X = QUEUE [FRONT]

3. [Increment FRONT pointer]


FRONT = FRONT + 1

4. [Exit]

(C) GOYANI MAHESH 5


QUEUE IMPLEMENTATION

QUEUE implementation can be done in TWO ways;

(I) Static Implementation (Using Array)


(II) Dynamic Implementation (Using Pointers / Linked List)

(C) GOYANI MAHESH 6


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.

Front = -1
Rear = -1

(C) GOYANI MAHESH 7


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

(C) GOYANI MAHESH 8


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

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

enqueue(6) front rear


0 4

(C) GOYANI MAHESH 9


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

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

enqueue(6) front rear


0 4

front rear
1 7 5 2 6 8
1 7 5 2 6 8
0 1 2 3 4 5 6 7
enqueue(8) front rear
0 5
(C) GOYANI MAHESH 10
front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
dequeue()
front rear
1 5

(C) GOYANI MAHESH 11


front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
dequeue()
front rear
1 5

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
dequeue()
front rear
2 5

(C) GOYANI MAHESH 12


front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
dequeue()
front rear
1 5

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
dequeue()
front rear
2 5

front rear
5 2 6 8 9 12
5 2 6 8 9 12
0 1 2 3 4 5 6 7
enqueue(9)
front rear
enqueue(12)
enqueue(21) ?? 2 7
(C) GOYANI MAHESH 13
QUEUE – Using LINKED LIST

front rear front rear

1 7 5 2 1 7 5 2

(C) GOYANI MAHESH 14


front rear front rear

1 7 5 2 1 7 5 2

front rear front rear

7 5 2 1 7 5 2

dequeue()

(C) GOYANI MAHESH 15


front rear front rear

1 7 5 2 1 7 5 2

front rear front rear

7 5 2 1 7 5 2

dequeue()

front rear front rear

7 5 2 9 7 5 2 9

enqueue(9)
(C) GOYANI MAHESH 16
CIRCULAR QUEUE

6 3 2 QUEUE Full

(C) GOYANI MAHESH 17


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

(C) GOYANI MAHESH 18


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

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 noElements
enqueue(21) 0 7
5 4

(C) GOYANI MAHESH 19


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

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 noElements
enqueue(21) 0 7
5 4

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
enqueue(7)
1 8
5
(C) GOYANI MAHESH 4 20
0 1

front rear 21 7 front size


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

(C) GOYANI MAHESH 21


0 1

front rear 21 7 front size


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

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
dequeue() 1 6
5 4

(C) GOYANI MAHESH 22


CQ Operation void enqueue(int x)
{
rear = (rear+1)%size;
QUEUE[rear] = x;
noElements = noElements+1;
}

int dequeue()
{
int x = QUEUE[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}

int isFull()
{
return noElements == size;
}

int isEmpty()
{
return noElements == 0;
}

(C) GOYANI MAHESH 23


ALGORITHM INSERT(STACK, REAR, FRONT, DATA)

1. Initialize FRONT = -1, REAR = -1

2. REAR = (REAR + 1) % SIZE

3. If (FRONT == REAR && FRONT != -1)

a). “Queue Over Flow”

b). Exit.

4. If (FRONT == -1) then

a). FRONT = 0

b). REAR = 0

5. QUEUE [REAR] = DATA

6. Exit.

(C) GOYANI MAHESH 24


ALGORITHM REMOVE(STACK, REAR, FRONT, DATA)

1. If (FRONT == -1 || FRONT >= REAR)

a). “QUEUE Under Flow”

b). Exit.

2. DATA = QUEUE [FRONT]

3. if (FRONT == REAR)

REAR = FRONT = -1

Else

FRONT = (FRONT + 1) % SIZE

(C) GOYANI MAHESH 25


DEQUEUE

Insertion Insertion

14 69 44 33 34

Deletion Deletion

 Elements can be added and deleted from both the ends.

 Input Restricted Queue :


 Insertion can be performed at REAR end only.
 Deletion can be performed at both the ends.

 Output Restricted Queue:


 Insertion can be performed at both the ends
 Deletion can be performed at FRONT end only

(C) GOYANI MAHESH 26


APPLICATION

 Simulation

 Round Robin Technique

 Printer Server Routine

 All Kind of Customer Service

(C) GOYANI MAHESH 27


PRIORITY QUEUE

 Priority queue is a queue where each element is assigned a priority.

 An element with higher priority is processed before any other element.


 Two elements with same priority are processed according to the order in
which they were inserted to the queue.

NODE
DATA 14 10 12 60 13
12 7
9 11 23 56 15
Priority
DATA Priority

REAR

(C) GOYANI MAHESH 28


CREATION
12 17

10 9 12 17

Push (DATA = 10, PRIORITY = 9)

10 9 12 17 60 30

Push (DATA = 60, PRIORITY = 30)

10 9 12 17 60 30 13 46

Push (DATA = 13, PRIORITY = 46)

10 9 14 10 12 17 60 30 13 46

Push (DATA = 13, PRIORITY = 46)

(C) GOYANI MAHESH 29


10 9 14 10 12 17 60 30 13 46

14 10 12 17 60 30 13 46

X=pop (i.e. 10)

12 17 60 30 13 46

X=pop (i.e. 14)

(C) GOYANI MAHESH 30

You might also like