DATA STRUCTURE QUEUE
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.
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.
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
QUEUE AS AN ARRAY queue and rear stores the index of the last element in a queue then Number of elements = front - rear + 1
REPRESENTATION OF AN QUEUE Initially Empty 0 1 2 3 4 Front = NULL Rear = NULL
REPRESENTATION OF AN QUEUE A, B and then C inserted: 0 1 2 3 4 Front = 0 Rear = 2 A B C
REPRESENTATION OF AN QUEUE A deleted: 0 1 2 3 4 Front = 1 Rear = 2 B C
REPRESENTATION OF AN QUEUE D and then E inserted: 0 1 2 3 4 Front = 1 Rear = 4 B C D E
REPRESENTATION OF AN QUEUE B and C deleted: 0 1 2 3 4 Front = 3 Rear = 4 D E
REPRESENTATION OF AN QUEUE D deleted: 0 1 2 3 4 Front = 4 Rear = 4 E
REPRESENTATION OF AN QUEUE K deleted: 0 1 2 3 4 Front = NULL Rear = NULL
INSERTION IN AN ARRAY QUEUE Every insertion increases the value of rear by 1.
ALGORITHM If rear = NULL Then { rear = front = 0 Queue [0] = Item } Else if rear = N-1 Then Print " Queue Full, Overflow"
Else { Queue[rear+1] = Item rear = rear +1 } End. ALGORITHM Else { Queue[rear+1] = Item rear = rear +1 } End.
DELETION IN AN ARRAY QUEUE Whenever an element is deleted from the queue, the value of front is increased by 1.
If front = NULL Then { Print "Queue Empty" } Else ALGORITHM If front = NULL Then { Print "Queue Empty" } Else
Item = Queue[front] If front = rear Then { front = rear = NULL } Else ALGORITHM Item = Queue[front] If front = rear Then { front = rear = NULL } Else
ALGORITHM { front = front +1 } End.
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.
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.
ALGORITHM /* Allocate space for Item to be inserted */ NEWPTR = new Node NEWPTR ->Info = Item NEWPTR ->Link = NULL
ALGORITHM /* Insert in the Queue*/ If rear = NULL Then { front = NEWPTR rear = NEWPTR
} Else { rear ->Link = NEWPTR rear = NEWPTR END ALGORITHM } Else { rear ->Link = NEWPTR rear = NEWPTR END
DELETION IN A LINKED QUEUE Deletions in a linked queue take place from the front end. Therefore the front gets modified with every delete.
If rear = NULL Then Print "Queue Empty" Else { Item = front ->Info ALGORITHM If rear = NULL Then Print "Queue Empty" Else { Item = front ->Info
If front = rear Then { front = rear = NULL } Else ALGORITHM If front = rear Then { front = rear = NULL } Else
front = front ->Link } END ALGORITHM front = front ->Link } END
VARIATIONS IN QUEUES Two popular variations in queues are - 1. Circular Queues 2. Dequeues (Double-Ended Queues)
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.
REPRESENTATION OF AN CIRCULAR QUEUE Initially Empty 0 1 2 3 4 Front = NULL Rear = NULL
REPRESENTATION OF AN CIRCULAR QUEUE A, B and then C inserted: 0 1 2 3 4 Front = 0 Rear = 2 A B C
REPRESENTATION OF AN CIRCULAR QUEUE A deleted: 0 1 2 3 4 Front = 1 Rear = 2 B C
REPRESENTATION OF AN CIRCULAR QUEUE D and then E inserted: 0 1 2 3 4 Front = 1 Rear = 4 B C D E
REPRESENTATION OF AN CIRCULAR QUEUE B and C deleted: 0 1 2 3 4 Front = 3 Rear = 4 D E
REPRESENTATION OF AN CIRCULAR QUEUE K inserted: 0 1 2 3 4 Front = 3 Rear = 0 K D E
REPRESENTATION OF AN CIRCULAR QUEUE D deleted: 0 1 2 3 4 Front = 4 Rear = 0 K E
REPRESENTATION OF AN CIRCULAR QUEUE G inserted: 0 1 2 3 4 Front = 4 Rear = 1 K G E
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
INPUT RESTRICTED DEQUE It is a deque which allows insertions at only one end but allows deletions at both ends of the list
OUTPUT RESTRICTED DEQUE It is a deque which allows deletions at only one end but allows insertions at both ends of the list
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