Download presentation
Presentation is loading. Please wait.
1
DATA STRUCTURE QUEUE
2
DEFINITION A Queue is a linear list of elements in which deletion can take place only at one end, called the front, and insertions can take place only at the other end, called the rear.
3
QUEUE STRUCTURE A Queue is a FIFO structure, since the first element in a queue will be the first element out of the queue. In other words, the order in which elements enter a queue is the order in which they out/leave and physically it can be implemented either as Array or as a Linked List.
4
QUEUE AS AN ARRAY When a queue is created as an array, its number of elements is declared before processing. The beginning of the array becomes its front end and the end of the array becomes its rear end. Thus, front stores the index of the first element in a
5
QUEUE AS AN ARRAY queue and rear stores the index of the last element in a queue then Number of elements = front - rear + 1
6
REPRESENTATION OF AN QUEUE
Initially Empty Front = NULL Rear = NULL
7
REPRESENTATION OF AN QUEUE
A, B and then C inserted: Front = 0 Rear = 2 A B C
8
REPRESENTATION OF AN QUEUE
A deleted: Front = 1 Rear = 2 B C
9
REPRESENTATION OF AN QUEUE
D and then E inserted: Front = 1 Rear = 4 B C D E
10
REPRESENTATION OF AN QUEUE
B and C deleted: Front = 3 Rear = 4 D E
11
REPRESENTATION OF AN QUEUE
D deleted: Front = 4 Rear = 4 E
12
REPRESENTATION OF AN QUEUE
K deleted: Front = NULL Rear = NULL
13
INSERTION IN AN ARRAY QUEUE
Every insertion increases the value of rear by 1.
14
ALGORITHM If rear = NULL Then { rear = front = 0 Queue [0] = Item }
Else if rear = N-1 Then Print " Queue Full, Overflow"
15
Else { Queue[rear+1] = Item rear = rear +1 } End.
ALGORITHM Else { Queue[rear+1] = Item rear = rear +1 } End.
16
DELETION IN AN ARRAY QUEUE
Whenever an element is deleted from the queue, the value of front is increased by 1.
17
If front = NULL Then { Print "Queue Empty" } Else
ALGORITHM If front = NULL Then { Print "Queue Empty" } Else
18
Item = Queue[front] If front = rear Then { front = rear = NULL } Else
ALGORITHM Item = Queue[front] If front = rear Then { front = rear = NULL } Else
19
ALGORITHM { front = front +1 } End.
20
LINKED QUEUE Linked queues are the queues having links among its elements. Two pointers are maintained to store the front position and the rear position.
21
INSERTION IN A LINKED QUEUE
Insertion in a linked queue also takes place only at the rear end. So rear gets modified with every insert.
22
ALGORITHM /* Allocate space for Item to be inserted */
NEWPTR = new Node NEWPTR ->Info = Item NEWPTR ->Link = NULL
23
ALGORITHM /* Insert in the Queue*/ If rear = NULL Then {
front = NEWPTR rear = NEWPTR
24
} Else { rear ->Link = NEWPTR rear = NEWPTR END
ALGORITHM } Else { rear ->Link = NEWPTR rear = NEWPTR END
25
DELETION IN A LINKED QUEUE
Deletions in a linked queue take place from the front end. Therefore the front gets modified with every delete.
26
If rear = NULL Then Print "Queue Empty" Else { Item = front ->Info
ALGORITHM If rear = NULL Then Print "Queue Empty" Else { Item = front ->Info
27
If front = rear Then { front = rear = NULL } Else
ALGORITHM If front = rear Then { front = rear = NULL } Else
28
front = front ->Link } END
ALGORITHM front = front ->Link } END
29
VARIATIONS IN QUEUES Two popular variations in queues are -
1. Circular Queues 2. Dequeues (Double-Ended Queues)
30
CIRCULAR QUEUE Circular Queues are the queues implemented in circular form rather than a straight line. Circular queues overcome the problem of unutilized space in linear queues implemented as arrays.
31
REPRESENTATION OF AN CIRCULAR QUEUE
Initially Empty Front = NULL Rear = NULL
32
REPRESENTATION OF AN CIRCULAR QUEUE
A, B and then C inserted: Front = 0 Rear = 2 A B C
33
REPRESENTATION OF AN CIRCULAR QUEUE
A deleted: Front = 1 Rear = 2 B C
34
REPRESENTATION OF AN CIRCULAR QUEUE
D and then E inserted: Front = 1 Rear = 4 B C D E
35
REPRESENTATION OF AN CIRCULAR QUEUE
B and C deleted: Front = 3 Rear = 4 D E
36
REPRESENTATION OF AN CIRCULAR QUEUE
K inserted: Front = 3 Rear = 0 K D E
37
REPRESENTATION OF AN CIRCULAR QUEUE
D deleted: Front = 4 Rear = 0 K E
38
REPRESENTATION OF AN CIRCULAR QUEUE
G inserted: Front = 4 Rear = 1 K G E
39
DEQUE (DOUBLE-ENDED QUEUES)
Deques are the queues in which elements can be added or removed at either end but not in the middle. There are two variation of a deque – 1. Input Restricted Deque 2. Output Restricted Deque
40
INPUT RESTRICTED DEQUE
It is a deque which allows insertions at only one end but allows deletions at both ends of the list
41
OUTPUT RESTRICTED DEQUE
It is a deque which allows deletions at only one end but allows insertions at both ends of the list
42
ALOK GUPTA PGT (COMPUTER SCIENCE) KV, SECTOR VIII R K PURAM, NEW DELHI
THANK YOU ALOK GUPTA PGT (COMPUTER SCIENCE) KV, SECTOR VIII R K PURAM, NEW DELHI
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.