Skip to content
Laviyx Learn

Try

DS Lab · Experiment 7

Queue using a linked list (pointers)

CStacks and queuesJNTUH R22 · CS306PC

Aim

To write a menu-driven C program that implements a queue using a singly linked list (pointers), with the operations enqueue (insert), dequeue (delete), peek and display, and reports underflow.

Algorithm

  1. Define a node with data and next. Keep two pointers: front (first node) and rear (last node). Both are NULL when the queue is empty.
  2. Enqueue x: allocate a new node with next = NULL. If the queue is empty, make both front and rear point to it. Otherwise set rear->next = new and rear = new.
  3. Dequeue: if front is NULL, report underflow. Otherwise save front, move front to front->next, print and free the saved node. If front became NULL, set rear = NULL too.
  4. Peek: show front->data if the queue is not empty.
  5. Display: follow next from front to the end and print each node.
  6. Repeat the menu until the user chooses Exit, then free every remaining node.

Program

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

struct node {
    int data;
    struct node *next;
};

struct node *front = NULL, *rear = NULL;    /* both NULL: queue is empty */

void enqueue(int value) {
    struct node *newnode = malloc(sizeof(struct node));
    if (newnode == NULL) {
        printf("Queue overflow (no memory left)\n");
        return;
    }
    newnode->data = value;
    newnode->next = NULL;
    if (rear == NULL) {         /* first node */
        front = newnode;
        rear = newnode;
    } else {
        rear->next = newnode;
        rear = newnode;
    }
    printf("Inserted %d\n", value);
}

void dequeue(void) {
    if (front == NULL) {
        printf("Queue underflow, nothing to delete\n");
        return;
    }
    struct node *temp = front;
    front = front->next;
    if (front == NULL) {        /* queue became empty */
        rear = NULL;
    }
    printf("Deleted %d\n", temp->data);
    free(temp);
}

void peek(void) {
    if (front == NULL) {
        printf("Queue is empty\n");
    } else {
        printf("Front element is %d\n", front->data);
    }
}

void display(void) {
    if (front == NULL) {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue (front to rear): ");
    for (struct node *temp = front; temp != NULL; temp = temp->next) {
        printf("%d ", temp->data);
    }
    printf("\n");
}

void free_queue(void) {
    while (front != NULL) {
        struct node *temp = front;
        front = front->next;
        free(temp);
    }
    rear = NULL;
}

int main(void) {
    int choice, value;

    printf("QUEUE USING LINKED LIST\n");
    printf("1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");

    do {
        printf("\nEnter your choice: ");
        if (scanf("%d", &choice) != 1) {
            printf("Invalid input\n");
            break;
        }
        switch (choice) {
        case 1:
            printf("Enter value to insert: ");
            if (scanf("%d", &value) == 1) {
                enqueue(value);
            }
            break;
        case 2:
            dequeue();
            break;
        case 3:
            peek();
            break;
        case 4:
            display();
            break;
        case 5:
            break;
        default:
            printf("Invalid choice\n");
        }
    } while (choice != 5);

    free_queue();
    printf("All nodes freed. Exiting.\n");
    return 0;
}

Output

Output
QUEUE USING LINKED LIST
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice: 2
Queue underflow, nothing to delete

Enter your choice: 1
Enter value to insert: 10
Inserted 10

Enter your choice: 1
Enter value to insert: 20
Inserted 20

Enter your choice: 1
Enter value to insert: 30
Inserted 30

Enter your choice: 4
Queue (front to rear): 10 20 30 

Enter your choice: 2
Deleted 10

Enter your choice: 3
Front element is 20

Enter your choice: 1
Enter value to insert: 40
Inserted 40

Enter your choice: 4
Queue (front to rear): 20 30 40 

Enter your choice: 5
All nodes freed. Exiting.

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

Keeping a rear pointer makes enqueue O(1): without it, every insertion would have to walk to the end of the list. Dequeue removes the head, which is also O(1).

There is no fixed size, so overflow can only happen when malloc returns NULL. The nodes still in the queue at Exit are freed by free_queue.

Common mistakes

  • Forgetting to set rear = NULL when the last node is dequeued. rear then points to freed memory, and the next enqueue writes into it.

  • Inserting at the front and deleting at the rear. Deleting the last node of a singly linked list needs the node before it, which takes O(n) time.

  • Not setting newnode->next = NULL, so the last node points to garbage and display runs off into random memory.

  • Forgetting to free the nodes that remain when the program ends.

Viva questions

Try to answer each one aloud before you open it.

  • Why do we keep both front and rear pointers?

    front lets us delete in O(1) and rear lets us insert in O(1). With only one pointer, one of the two operations would need a walk through the whole list.

  • How do you know the queue is empty?

    front is NULL. In this program rear is also set to NULL whenever that happens.

  • What are the advantages over the array queue?

    There is no fixed size, and there is no wasted space at the front after deletions, so it never shows false overflow.

  • What is a deque?

    A double-ended queue, where elements can be inserted and deleted at both the front and the rear.

Learn the concepts

All Data Structures Lab experiments →