Introduction
This post will give a general introduction of Queue, and some of its basic operations.
Definition
Similar to its meaning in ordinary English, a queue is defined as a waiting line, like a line of people waiting to purchase tickets, where the first person in line is the first person served.For computer applications, we similarly define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. Queues are also called first-in first-out lists, or FIFO.
The entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue. Similarly, the last entry in the queue, that is, the one most recently append, is called the rear of the queue.
Implementation
Now we use C++ to implement a queue. Because I use a static array to store the entries of a queue, so it would be more efficient to make it circular in order to make full use of each storage space.
First I will show you the definition of the stack.(CircularQueue.hpp)
1 |
|
Here we use count to show how many entries are in the queue. And we use two int variables front, rear to mark the first entry and last entry in the queue.
CircularQueue()
precondition: None.
postcondition: The Queue has been created and is initialized to be empty.
1 | template <class Queue_entry> |
empty()
precondition: None.
postcondition: Return true if the Queue is empty, otherwise return false
1 | template <class Queue_entry> |
serve()
precondition: None.
postcondition:If the Queue is not empty, the front of the Queue has been removed, and return Success. Otherwise an Error_code of UnderFlow is returned.
1 | template <class Queue_entry> |
append(const Queue_entry &item)
precondition: None.
postcondition: If there is space, x is added to the Queue as its rear, and return Success. Otherwise an Error_code of OvewrFlow is returned.
1 | template <class Queue_entry> |
retrieve(Queue_entry &item)
precondition: None.
postcondition: If the Queue is not empty, the front of the Queue has been recorded as item, and return Success. Otherwise an Error_code of UnderFlow is returned.
1 | template <class Queue_entry> |
Hereto, I’ve introduce the Queue structure.A more useful extended queue is introduced in another post, just search ExtendtedQueue in this website. Hope this post can be helpful to you. Thank you!