Posts

Showing posts from December, 2018

Queue

Image
Previously, we have seen how a stack works. In this blog, I want to give you some insights on another elementary data structure, queue. Queue works on First-In-First-Out (FIFO) principle. Queue can be modeled in real life by a line of customers waiting to pay the bill. The new customers are added at the end of the queue and the customer at the front of the queue comes out after paying the bill. Similar to the push and pop operation of stack, queue has enqueue and dequeue operation. Enqueue adds the element to the end of the queue while dequeue removes the first element from the queue. (a) Queue Q has 5 elements at location Q[7...11]. (b) Queue Q after ENQUEUE(Q, 17), ENQUEUE(Q, 3), and ENQUEUE(Q, 5). (c) Queue Q after DEQUEUE(Q). [Source: Introduction to Algorithms] Here, we implement a queue using an array Q [1…n]. Queue has its attribute, Q.head which points to the head of the queue. Also, Q.tail points to the next location at which the new element will be inserted. When Q...