A Queue implemented as a linked structure in C.


Header file for a queue
//---------------------------------------------------------------
// File: Code136_Queue.h
// Purpose: Header file for a demonstration of a queue implemented 
//        as a linked structure.  Data type: Character
// Programming Language: C
// Author: Dr. Rick Coleman
//---------------------------------------------------------------
#ifndef CODE136_QUEUE_H
#define CODE136_QUEUE_H

#include <stdio.h>
#include <stdlib.h>

typedef struct QNodeType
{
    char    ch;
    struct QNodeType *next;
}QNode;


// List Function Prototypes
void InitQueue();                // Initialize the queue
void ClearQueue();               // Remove all items from the queue
int Enqueue(char ch);            // Enter an item in the queue
char Dequeue();                  // Remove an item from the queue
int isEmpty();                   // Return true if queue is empty
int isFull();                    // Return true if queue is full

// Define TRUE and FALSE if they have not already been defined
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif

#endif // End of queue header

Implementation (.c) file for a queue
//---------------------------------------------------------------
// File: Code136_Queue.c
// Purpose: Implementation file for a demonstration of a queue  
//        implemented as a linked structure.    Data type: Character
// Programming Language: C
// Author: Dr. Rick Coleman
// Date: February 11, 2002
//---------------------------------------------------------------
#include "Code136_Queue.h"

// Declare these as static so no code outside of this source
// can access them.
static QNode *head, *tail;    // Declare global pointers to head and tail of queue

//--------------------------------------------
// Function: InitQueue()
// Purpose: Initialize queue to empty.
// Returns: void
//--------------------------------------------
void InitQueue()
{
    head = tail = NULL;
}

//--------------------------------------------
// Function: ClearQueue()
// Purpose: Remove all items from the queue
// Returns: void
//--------------------------------------------
void ClearQueue()
{
    QNode *temp;
    while(head != NULL)
    {
        temp = head;
        head = head->next;
        free(temp);
    }

    head = tail = NULL; // Reset indices to start over
}

//--------------------------------------------
// Function: Enqueue()
// Purpose: Enqueue an item into the queue.
// Returns: TRUE if enqueue was successful
//        or FALSE if the enqueue failed.
//--------------------------------------------
int Enqueue(char ch)
{
    QNode *temp;

    // Check to see if the Queue is full
    if(isFull()) return FALSE;

    // Create new node for the queue
    temp = (QNode *)malloc(sizeof(QNode));
    temp->ch = ch;
    temp->next = NULL;

    // Check for inserting first in the queue
    if(head == NULL)
        head = tail = temp;
    else
    {
        tail->next = temp; // Insert into the queue
        tail = temp;       // Set tail to new last node
    }

    return TRUE;
}

//--------------------------------------------
// Function: Dequeue()
// Purpose: Dequeue an item from the Queue.
// Returns: TRUE if dequeue was successful
//        or FALSE if the dequeue failed.
//--------------------------------------------
char Dequeue()
{
    char ch;
    QNode *temp;

    // Check for empty Queue
    if(isEmpty()) return '\0';  // Return null character if queue is empty
    else
    {
        ch = head->ch;        // Get character to return
        temp = head;
        head = head->next;    // Advance head pointer
        free(temp);            // Free old head
        // Check to see if queue is empty
        if(isEmpty())
        {
            head = tail = NULL;        // Reset everything to empty queue
        }
    }
    return ch;                // Return popped character
}

//--------------------------------------------
// Function: isEmpty()
// Purpose: Return true if the queue is empty
// Returns: TRUE if empty, otherwise FALSE
// Note: C has no boolean data type so we use
//    the defined int values for TRUE and FALSE
//    instead.
//--------------------------------------------
int isEmpty()
{
    return (head == NULL);
}

//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the queue is full.
// Returns: TRUE if full, otherwise FALSE
//--------------------------------------------
int isFull()
{
    return FALSE;
}

Main file used to test the queue
//---------------------------------------------------------------
// File: QueueMain.c
// Purpose: Main file with tests for a demonstration of a queue 
//        implemented as as a linked structure.
// Programming Language: C
// Author: Dr. Rick Coleman
// Date: February 11, 2002
//---------------------------------------------------------------
#include "Code136_Queue.h"
#include 

int main(int argc, char **argv)
{
    char    testString[27];
    int        i;
    char    ch;

    printf("Simple Queue Demonstration\n");
    printf("(Queue implemented as a linked structure - Queue data type is character.)\n\n");
    printf("Creating a queue\n");

    InitQueue();
    printf("Queue created...\n");

    // Test enqueuing and dequing item on a queue
    printf("Testing enqueue and dequeue of single item.\n");
    Enqueue('A');
    printf("Enqueued: %c\n", Dequeue());
    printf("...done\n\n");

    // Test queue by enqueing letters in a string
    strcpy(testString, "abcdefghijklmnopqrasuvwxyz"); 

    // Try to Enqueue all letters in the string
    i = 0;
    printf("Testing enqueuing of string: %s\n", testString);
    while(testString[i] != '\0')
    {
        if(!Enqueue(testString[i]))
        {
            printf("Queue is full. Unable to enqueue %c\n", testString[i]);
        }
        i++;
    }

    printf("\n\nDone testing enqueue.\n\nNow testing dequeue.\n");
    // Dequeue letters and print them
    printf("Dequeued letters are...\n");
    while((ch = Dequeue()) != '\0') // Dequeue returns null terminator 
        printf("%c", ch);            // when queue is empty
    
    printf("\nEnd of queue encountered...\n");

    printf("\n\n...done.\n");
    return 0;
}