Professional Documents
Culture Documents
Queue
Queue
Lecture 4
Lecture Objectives
In this lecture, you will:
Examples:
Figure
Here front and back are the indices of (a) A naive array-based implementation of a queue;
the front and back items, respectively, (b) rightward drift can cause the queue to appear full
in the queue. Initially, front is 0 and
back is -1. To insert a new item into the The problem with this strategy is rightward
queue, you increment back and place drift – that is, after a sequence of additions
the item in items[back]. To delete an
and removals, the items in the queue will drift
item, you simply increment front.
toward the end of the array, and back could
equal MAX_QUEUE – 1 even when the
The queue is empty whenever back is greater than front. queue contains only a few items. Figure (b)
The queue is full when back equals MAX_QUEUE -1. illustrates this situation.
An Array-Based Implementation
The only difficulty with this scheme involves detecting the queue-empty
and queue-full conditions.
An Array-Based Implementation
It seems reasonable to select as the queue-empty condition
front is one slot ahead of back
since this appears to indicate that front “passes” back when the queue becomes
empty, as Figure (a) depicts. However, it is also possible that this condition signals a
full queue: Because the queue is circular, back might in fact “catch up” with front as
the queue becomes full; Figure (b) illustrates this situation.
Figure (a) front passes back when the queue becomes Figure (b) back catches up to front when the queue
empty becomes full
An Array-Based Implementation
// Queue operations:
private:
QueueItemType items[MAX_QUEUE];
int front;
int back;
int count;
}; // end Queue class
An Array-Based Implementation of the ADT Queue
Function to check
whether queue is
empty or not
An Array-Based Implementation of the ADT Queue
if (isEmpty())
--count;
} // end if
} // end dequeue
An Array-Based Implementation of the ADT Queue
void Queue ::dequeue(QueueItemType& queueFront)
{
if (isEmpty())
cout << "empty queue, cannot dequeue\n" ;
else
Function to
{ // queue is not empty; retrieve and remove front retrieve and
queueFront = items[front]; remove front
front = (front+1) % MAX_QUEUE; item from the
queue
--count;
cout << "Front element of the queue is “;
cout << queueFront << endl;
} // end if
} // end dequeue
An Array-Based Implementation of the ADT Queue
if (isEmpty())
else Function to
{ // queue is not empty; retrieve front retrieve front
item of the
queueFront = items[front]; queue
cout << "Front element of the queue is “;
} // end getFront
An Array-Based Implementation of the ADT Queue
i = front - 1;
Function to
do display all the
items of the
{ i = (i+1)% MAX_QUEUE; queue
} while (i != back)
} // end display
An Array-Based Implementation of the ADT Queue
#include <iostream>
#include <iomanip> MAX_QUEUE = 5, means size of the queue is 5.
const int MAX_QUEUE = 5;
typedef int QueueItemType;
using namespace std;
int main()
{
Sample Run
int a;
Queue aQueue;
aQueue.enqueue(15); Output
aQueue.enqueue(20);
aQueue.enqueue(25); Items in the queue : [ 15 20 25 33 ]
aQueue.enqueue(33);
aQueue.display();
Front element of the queue is 15
aQueue.getFront(a); Items in the queue : [ 25 33 ]
aQueue.dequeue();
aQueue.dequeue(); Front element of the queue is 25
aQueue.display(); Items in the queue : [ 33 ]
aQueue.dequeue(a);
aQueue.display(); Press any key to continue . . .
system ("pause");
return 0;
}
A Pointer-Based Implementation
A pointer-based implementation of a queue could use a linear linked
list with two external pointers, one to the front and one to the back,
as Figure (a) illustrates.
Figure (b) shows that you can actually get by with a single external
pointer – to the back – if you make the linked list circular.
3 3
frontPtr frontPtr
(linked queue)
A Pointer-Based Implementation of the ADT Queue
class Queue
{
public:
// constructors and destructor:
Queue(); // default constructor
Queue(const Queue& Q); // copy constructor
~Queue(); // destructor
// Queue operations:
bool isEmpty() const;
// Determines whether the queue is empty.
// Precondition: None.
// Postcondition: Returns true if the queue is empty, otherwise returns false.
void enqueue(QueueItemType newItem);
// Inserts an item at the back of a queue.
// Precondition: newItem is the item to be inserted.
// Postcondition: If the insertion is successful, newItem is at the back of the queue.
A Pointer-Based Implementation of the ADT Queue
void dequeue();
// Dequeues the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, the item
// that was added to the queue earliest is deleted.
void dequeue(QueueItemType& queueFront);
// Retrieves and deletes the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, queueFront
// contains the item that was added to the queue earliest, and the item is deleted.
void getFront(QueueItemType& queueFront) const;
// Retrieves the item at the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, queueFront
// contains the item that was added to the queue earliest.
void display();
// Displays the elements which are in the queue.
A Pointer-Based Implementation of the ADT Queue
private:
// The queue is implemented as a linked list with one external pointer to the front
// of the queue and a second external pointer to the back of the queue.
struct QueueNode
{ QueueItemType item;
QueueNode *next;
}; // end struct
QueueNode *frontPtr;
QueueNode *backPtr;
QueueNode *curPtr;
}; // end Queue class
A Pointer-Based Implementation of the ADT Queue
Queue ::~Queue()
{
while (!isEmpty())
destructor
dequeue();
// Assertion: frontPtr and backPtr equal NULL
} // end destructor
A Pointer-Based Implementation of the ADT Queue
10 * (10 * 2 + 4) + 7
The following pseudocode performs this conversion in general:
You can see that the first character in the string is at the front of the queue and
the last character in the string is at the top of the stack.
Thus, characters removed from the queue will occur in the order in which they
appear in the string; characters removed from the stack will occur in the
opposite order.
Figure: The results of inserting a string into both a queue and a stack
Knowing this, you can compare the characters at the
front of the queue and the top of the stack. If the
characters are the same, you can delete them. You
repeat this process until either the ADTs become
empty, in which case the original string is a
palindrome, or the two characters are not the same, in
which case the string is not a palindrome.
Figure: The results of inserting a string into both a queue and a stack
The following is a pseudocode version of a nonrecursive
recognition algorithm for the language of palindromes:
// create an empty queue and an empty stack While (aQueue is not empty and
charactersAreEqual)
aQueue.createQueue()
{ aQueue.getFront(queueFront)
aStack.createStack()
aStack.getTop(stackTop)
// insert each character of the string into both
// the queue and the stack if (queueFront equals stackTop)
length = length of str { aQueue.dequeue()
For example,
example in a banking environment, the customers
who arrive first are served first.