Review of Queue ADT 

 

Objectives of this lecture

q       Review the Queue ADT & Its Implementation

 

Review

q       Recall that Queue is another data structure which has two ends, front and rear, where data is added only at the rear and is removed only at the front.  Thus the first item to be inserted is always the first to be removed (FIFO)

q       It has so many applications, perhaps even more than stack since first-come first serve (FCFS) is the natural way handling competing clients.  For example, access to network resources such as printing, mail-server, web-server, etc are mostly based on FCFS.

q       Its basic operations are: CreateQueue, ClearQueue, FullQueue, EmptyQueue, AppendQueue (enqueue)  and Serve (dequeue or remove).

q       It can also be implemented using both array (static approach)and linked list (Dynamic approach).

q       Recall that the array implementation is particularly difficult because it was necessary to treat the array as circular and the extreme cases of full queue and empty queue caused some difficulties which had to be resolved.

Exercise: Study the implementation of queue using array in chapter four (section 3) of your book.  Identify the drawbacks of this approach.

 

Dynamic Implementation of Stack.

q        We recall that in this implementation, a queue consist of a linked list with two pointers, front (pointing to the front node) and rear (pointing to the rear node)

q       It is important to ensure that the changing implementation approach does not affect the users of your ADT.  Thus, unlike what is in the book, we shall do the implementation such that our functions are compatible to those in the array implementation.

q       To add a new node, we simply create a node, point the next of the current rear node to it and update the rear pointer to points to this new node.

q       To remove a node, we set the next of the current front node to be the front pointer and free the current front node.

q       The following figure illustrates how stack is just a pointer pointing to the top node. 

q        

q        

User interface:

/*  file name: queue.h  */

#include “common.h”

typedef …. QueueEntry;  //datatype for the items in the queue

typedef struct queuenode {    QueueEntry info;

              struct queuenode *next;

        } QueueNode;

typedef struct queue {    QueueNode *front;

                                            QueueNode *rear;

         } Queue;

void    CreateQueue(Queue *q);

void    ClearQueue(Queue *q);

void    Append(QueueEntry newitem, Queue *q);

void    Serve(QueueEntry *frontitem, Queue *q);

Boolean QueueEmpty(Queue *q);

Boolean QueueFull(Queue *q);

 

Implementation Details:

//file name:  queue.c

#include “queue.h”

QueueNode *MakeNode(QueueEntry item);

 

/* CreateQueue:  create the queue.

Pre:   None.

Post: The queue q has been initialized to be empty.

 */

void CreateQueue(Queue *q)

{    q->front = q->rear = NULL;

}

/* AppendNode:  append an entry to the queue.

Pre:   newitem is a valid queue entry

Post: new node is created, the new item has been placed in the

 new node and rear points to the new node.  If this is the first

 node, front also points to it

Uses: QueueEmpty, MakeNode, Error. */

void    Append(QueueEntry newitem, Queue *q)

{   QueueNode *p = MakeNode(newitem);

    if (p != NULL)

        Error("Attempt to append a nonexistent node to the queue.");

    else if (QueueEmpty(q)) /* Set both front and rear to p.          */

        q->front = q->rear = p;

    else {    q->rear->next = p;

                  q->rear = p;

            }

}

 

/* ServeNode:  remove the first entry in the queue.

Pre:   The linked queue q has been created and is not empty.

Post: The first node in the queue has been removed and parameter

          p points to this node.

Uses: QueueEmpty, Error.

 */

void  Serve(QueueEntry *frontitem, Queue *q)

{  QueueNode *p;

    if (QueueEmpty(q))

        Error("Attempt to delete a node from an empty queue.");

   else {  p = q->front;   

               q->front = q->front->next;

              if (QueueEmpty(q))      /* Is the queue now empty? */

                     q->rear = NULL;

             *frontitem =p->info;

              free(p);

        }

}

 

/* MakeNode: make a new node and insert item.

Pre:   None.

Post: Create a new node and insert item in it.

 */

QueueNode *MakeNode(QueueEntry item)

{   QueueNode *nodepointer;

 

    nodepointer = (QueueNode *) malloc(sizeof(QueueNode));

    if (nodepointer == NULL)

        Error("Exhausted memory.");

    else

    {  nodepointer->info = item;

        nodepointer->next = NULL;

    }

   return nodepointer;

}

 

Exercises:

q       Implements the remaining functions: (E2: page 164)

ClearQueue, QueueEmpty, QueueFull

q       Try E3, E4 of page 164