Homework 10
Linked Queue
The purpose of this assignment is to implement a
Circular Doubly Linked Queue,
which is built around a linked lists.
The behavior of a queue is First In
First Out with two main operations, enqueue and dequeue.
This implementation of the queue
will use a dummy head node, which will act as a reference to the front
of the queue and the back of the queue. When the queue is instantiated,
the dummy head node's next and prev will point to itself. The dummy head
node will only be deallocated when the queue object goes out of scope.
When the first element is enqueued, a new node
is added to the back of the queue (head's prev).
For the dequeuing operation, the element at the front of the queue is
removed and the value returned.
Your task is to write the LinkedQueue.h header file and
LinkedQueue.cpp Implementation file. Node.h, Node.cpp, and
main.cpp will be given to you. Details for the queue's operations
are given below.
- LinkedQueue() - the default constructor will allocate memory for a
dummy head node and set head's next and prev to itself.
- ~LinkedQueue() - the destructor will call clear(),
deallocate head, set head to
NULL, and set count to 0.
- void clear() - this function will traverse and deallocate each node
within the queue.
Set head to NULL and count to 0
Set head's next and prev to head, and count to 0.
- Node<T>* createNode(const T&) - this will dynamically
allocate a new Node object, assign the passed parameter to set the
new node's data, and set the new node's next and prev to itself. Return the
new node
- bool isEmpty() const - this function will return true if head's
next is pointing to head, false otherwise
- int getCount() const - this function will return the count of elements
within the queue
- bool enqueue(const T&) - this function will create a new node and
insert the passed element to the back of the queue (head's prev), set the
new node's next to head and new node's prev to head's prev. Then set the
last node's next to the new node, and head's prev to new node,
increment count, and return true. If bad_alloc return false.
- T dequeue() - this function will remove the first element from the
queue, deallocate the Node for the element, decrement count, and return
the element if the queue is not empty. Otherwise return a
type casted (T)0.
- void print() - this function will traverse and print the contents of
the queue, if the queue is not empty
hw10 files