Lab 6 - Linked Queues

The purpose of this lab is to implement the enqueue and dequeue operations for a linked queue. This will be similar to last week's lab where we implemented the same functions for an array-based queue.

The file lab6.cpp contains most of the code for the linked queue, including the same menu function used in Lab 5. Note that the linked queue is still a singly linked queue (its node structure only has a pointer to "next"), but it does keep track of the tail node to make enqueue operations constant in time efficiency.

The one special case we have to handle in enqueue and dequeue is transitioning to and from an empty queue. Since we do NOT have a dummy head node, the empty state is indicated by both the head and tail being NULL. When adding the first element to the queue, we have to update both the head and tail to point to that first node. But adding additional elements to the queue only need to update the tail pointer since enqueue is an abstraction of tail insertion.

Likewise, when deleting the very last item, we have to update the head and tail pointer to be NULL again so we can correctly tell that the list is empty. We can detect if there is only one node left in the list when the head and tail pointers both are pointing to the same node. For all other dequeue operations, we just delete the head node and update the head pointer.

The pseudocode for the enqueue and dequeue operations is as follows:

Enqueue:
  try to allocate a new node
  if allocation fails
    print/raise "out of memory" error
    return false
  end-if

  set node->data to the element
  set node->next to NULL
  if queue is empty
    set head and tail to the node
  else
    set tail->next to the node
    set tail to the node
  end-if
  return true

Dequeue:
  if queue is empty
    print/raise "empty queue" error
    return false
  end-if

  set node pointer called ptr to the head
  if head is equal to tail
    set head and tail to NULL
  else
    set head to head->next
  end-if
  delete ptr
  return true
Once you add the implementation for enqueue and dequeue, the menu will function as expected to add and remove items from the queue. You can use the debug report option to print out the "behind the scenes" state of the queue.

Email me your updated lab6.cpp file.