Lecture5 Csc0015 Module5 Week5
Lecture5 Csc0015 Module5 Week5
Queues
Module 5A
bool IntQueue::isEmpty()
{
bool status;
if (numItems)
status = false;
else
status = true;
return status;
}
Contents of IntQueue.cpp
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull()
{
bool status;
return status;
}
Contents of IntQueue.cpp
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void IntQueue::clear()
{
front = queueSize - 1;
rear = queueSize - 1;
numItems = 0;
}
Contents of IntQueue.cpp
Main Program
// This program demonstrates the IntQeue class
#include <iostream>
#include "intqueue.h“
using namespace std;
int main()
{
IntQueue iQueue(5);
Program Output
Enqueuing 5 items...
Now attempting to enqueue again...
The queue is full.
The values in the queue were:
0
• A dynamic queue starts as an empty linked list.
• With the first enqueue operation, a node is added,
which is pointed to by front and rear pointers.
• As each new item is added to the queue, a new node
is added to the rear of the list, and the rear pointer is
updated to point to the new node.
• As each item is dequeued, the node pointed to by the
front pointer is deleted, and front is made to point
to the next node in the list.
• The figure below shows the structure of a dynamic
queue.
Contents of DynIntQueue.cpp
#include <iostream>
#include "dynintqueue.h“ //************************
using namespace std; // Destructor *
//************************
//************************
DynIntQueue::~DynIntQueue()
// Constructor *
{
//************************
clear();
}
DynIntQueue::DynIntQueue(void)
{
front = NULL;
rear = NULL;
numItems = 0;
}
Contents of DynIntQueue.cpp
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
if (isEmpty())
cout << "The queue is empty.\n";
else
{
num = front->value;
temp = front->next;
delete front;
front = temp;
numItems--;
}
}
Contents of DynIntQueue.cpp
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool DynIntQueue::isEmpty()
{
bool status;
if (numItems)
status = false;
else
status = true;
return status;
}
Contents of DynIntQueue.cpp
//********************************************
// Function clear dequeues all the elements *
// in the queue. *
//********************************************
void DynIntQueue::clear()
{
int value; // Dummy variable for dequeue
while(!isEmpty())
dequeue(value);
}
Main Program
// This program demonstrates the DynIntQeue class
#include <iostream>
#include "dynintqueue.h“
using namespace std;
int main()
{
DynIntQueue iQueue;
Queue STL
• Know the existing C++ STL
containers for queues: the queue
and dequeue
• The STL dequeue Container
• The STL queue Container
• A deque (pronounced "deck" or "deek") is a
double-ended queue. It similar to a vector, but
allows efficient access to values at both the front
and the rear.
• The queue ADT is like the the stack ADT: it is
actually a container adapter.
• Programs that use the deque ADT must include the
deque header file.
Sample Program 1
// This program demonstrates the STL deque
// container.
#include <iostream>
#include <deque>
using namespace std;
int main()
{
int x;
deque<int> iDeque;