UMBC CS 201, Spring 99
UMBC CMSC 201 Spring '99 CSEE | 201 | 201 S'99 | lectures | news | help

The Queue
Using a linked list implementation

The Code

/************************************************\ * Filename: queue.c * * Author: Sue Bogar * * Date Written: 4/22/98 * * Description: This file contains the functions * * necessary to work with a queue. * * This set of functions provide the operations * * needed including adding an item to the queue, * * enqueue; deleting an item from the queue, * * dequeue; determining if the queue is empty and * * the printing the items in the queue. * * Since the queue is being implemented as a * * linked list, some functions needed for a list * * have been added to this file, although they * * would normally be in a seperate header file, * * the one for linked lists. Those functions * * are CreateNode and GetData. * \************************************************/ #include <stdio.h> #include <stdlib.h> #include "queue.h" /****************** * Enqueue takes two pointers to nodePtrs, and a * nodePtr as arguments. The first argument will * contain the address of head, the second argument * will contain the address of tail, and the third * argument is a pointer to the node to be inserted. * Enqueue will insert the item at the end of the * queue. The address of head and tail are passed * into this function, because this function may * need to change the address held in head, and * always changes the address held in tail. ******************/ void Enqueue (nodePtr* headPtr, nodePtr* tailPtr, nodePtr temp) { if (IsEmpty (*headPtr)) { *headPtr = temp; *tailPtr = temp; } else { (*tailPtr) -> next = temp; *tailPtr = temp; } } /****************** * Dequeue takes two pointers to nodePtr as * its arguments. The first argument will * contain the address of head, the second * argument will contain the address of tail. * This function removes an item from the * queue and returns the data value stored * there. This function may alter the addresses * held in either head or tail. ******************/ int Dequeue (nodePtr *headPtr, nodePtr *tailPtr) { int value; nodePtr temp; if (IsEmpty (*headPtr)) { printf ("Can't delete from an empty queue\n"); } else { temp = *headPtr; value = (*headPtr) -> data; *headPtr = (*headPtr) -> next; if (*headPtr == NULL) { *tailPtr = NULL; } free (temp); return value; } } /****************** * IsEmpty takes a nodePtr as its first * argument, which is a pointer to the list, * known as head. It determines whether the * queue is empty and returns 1 (true) if * the queue is empty and 0 (false) if it * is not empty. ******************/ int IsEmpty (nodePtr head) { /* If the pointer to the list is NULL then there is no list. The list is empty, so we return true. */ if(head == NULL) { return 1; } else { return 0; } } /****************** * PrintQueue takes a nodePtr as an argument * which is initially the head. The queue is * traversed and the value of the data member * of each item is printed. ******************/ void PrintQueue (nodePtr curr) { if (IsEmpty (curr)) { printf ("The queue is empty\n\n"); } else { printf ("The queue contains :\n"); /*Until the end of the list*/ while (curr != NULL) { /* print the current data item */ printf("%d ", curr -> data); /* move to the next node */ curr = curr -> next; } printf ("\n\n"); } } /****************** * CreateNode mallocs the space needed to * hold a struct of type node, initializes * the members, and returns a pointer to * the new node. ******************/ nodePtr CreateNode (void) { nodePtr newNode; /* Get the space needed for the node */ newNode = (nodePtr) malloc (sizeof(node)); if (newNode == NULL) { printf ("Out of Memory - CreateNode\n"); exit (-1); } /* Initialize the members */ newNode -> data = 0; newNode -> next = NULL; return newNode; } /****************** * GetData gets the value of data * from the user and puts it into the * node pointed to by the nodePtr it * receives as an argument. ******************/ void GetData (nodePtr temp) { int value; printf ("\nEnter the value of data : "); scanf ("%d", &value); temp -> data = value; }

The Driver

/* Driver to test queues (linked list implementation) */ /* Bogar, 4/22/98 */ #include <stdio.h> #include <stdlib.h> #include "queue.h" main () { int value; nodePtr head, tail, temp; head = tail = NULL; PrintQueue (head); temp = CreateNode(); GetData (temp); printf("Enqueueing the value, %d\n", temp -> data); Enqueue (&head, &tail, temp); PrintQueue (head); temp = CreateNode(); GetData (temp); printf("Enqueueing the value, %d\n", temp -> data); Enqueue (&head, &tail, temp); PrintQueue (head); temp = CreateNode(); GetData (temp); printf("Enqueueing the value, %d\n", temp -> data); Enqueue (&head, &tail, temp); PrintQueue (head); printf ("Dequeueing :\n"); value = Dequeue(&head, &tail); printf ("The value was %d\n", value); PrintQueue (head); printf ("Dequeueing :\n"); value = Dequeue(&head, &tail); printf ("The value was %d\n", value); PrintQueue (head); printf ("Dequeueing :\n"); value = Dequeue(&head, &tail); printf ("The value was %d\n", value); PrintQueue (head); printf ("Dequeueing :\n"); value = Dequeue(&head, &tail); PrintQueue (head); }

The Output

retriever[102] a.out The queue is empty Enter the value of data : 7 Enqueueing the value, 7 The queue contains : 7 Enter the value of data : 36 Enqueueing the value, 36 The queue contains : 7 36 Enter the value of data : 14 Enqueueing the value, 14 The queue contains : 7 36 14 Dequeueing : The value was 7 The queue contains : 36 14 Dequeueing : The value was 36 The queue contains : 14 Dequeueing : The value was 14 The queue is empty Dequeueing : Can't delete from an empty queue The queue is empty retriever[103]


CSEE | 201 | 201 S'99 | lectures | news | help

Sunday, 02-May-1999 13:29:23 EDT