280 likes | 409 Views
This guide provides a detailed explanation of how to implement a queue using arrays and circular linked lists. It covers the fundamental concepts, including the use of head and tail pointers that indicate the first empty slot in the array. The document illustrates enqueue and dequeue operations, showing how items are added to the rear and removed from the front of the queue. Additionally, it explores the advantages of using a circular linked list for efficient memory management and continuous operations without wasting space.
E N D
.head .tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they point to the same slot, say 0.
Add object to rear of list 1 .head .tail
Add object to rear of list 1 2 .head .tail
Add object to rear of list 1 2 3 .head .tail
Add object to rear of list 1 2 3 4 .head .tail
Remove from front 2 3 4 .head .tail 1 .object
Remove from front 3 4 .head .tail 2 .object
Add 3 4 5 .head .tail
Remove 4 5 .head .tail 3 .object
Add 4 5 6 .head .tail
Add 4 7 5 6 .head .tail
Add 4 7 8 5 6 .head .tail
Add 4 7 8 5 6 9 .tail .head
Queue using Circularly Linked List
Circularly linked list .tail
Queue: remove from front _object .tail _object = tail->next->item;
Queue: remove from front Temp _temp _object .tail _temp = tail->next;
Queue: remove from front Temp _temp _object .tail _tail->next = tail->next->next;
Queue: remove from front Temp _temp _object .tail _delete temp;
Queue: remove from front Temp _object .tail _return object;
Queue: remove from front Temp .tail _
Queue: insert at rear Temp .tail _
Queue: insert at rear _cell Temp NULL .tail _cell = new Cell(object);
Queue: insert at rear _cell Temp .tail _cell->next = tail->next;
Queue: insert at rear _cell Temp .tail _tail->next = cell;
Queue: insert at rear _cell Temp .tail _tail = tail->next;