A queue like a stack is another special type of ordered list. In queue insertion operations are permitted at one end of the list and deletion operations are performed at the other end of the list. The end where insertion operations are performed is called rear and the end where deletion operations are performed is called front. A queue is often called Fisrt In Fisrt Out(FIFO) list because the first element removed is the first element added to the queue.

Here we will see the operations on stack using linked list because the stack is never full as long as the system has enough space for dynamic memory allocation.

A queue can defined by following structure:

typedef struct element {
    int item;
    struct element *next;
} element;

typedef struct queue {
    element *front;
    element *rear;
} queue;

If queue variable is q then q->front points to the front extreme end and q->rear points to the rear extreme end of the queue.

The complete source code is given below

/* 
 * File:   Queue.c
 * Author: https://www.roytuts.com
 */

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

typedef struct element {
    int item;
    struct element *next;
} element;

typedef struct queue {
    element *front;
    element *rear;
} queue;

void add(int item, queue *q) {
    element *e;
    e = (element*) malloc(sizeof (element));
    e->item = item;
    e->next = NULL;
    if (q->front == NULL) {
        q->front = e;
        q->rear = e;
        return;
    }
    q->rear->next = e;
    q->rear = e;
}

int delete(queue *q) {
    element *e;
    if (q->front == NULL) {
        printf("\nQueue is empty\n");
        return 0;
    }
    e = q->front;
    q->front = q->front->next;
    if (q->rear == e) {
        q->rear = NULL;
    }
    free(e);
    return 1;
}

void display(queue *q) {
    element *t;
    if (q->front == NULL) {
        printf("\nQueue is Empty\n");
    } else {
        printf("\n");
        t = q->front;
        while (t->next != NULL) {
            printf("%d->", t->item);
            t = t->next;
        }
        printf("%d->NULL\n", t->item);
    }
}

int main() {
    int choice, value;
    queue *q = (queue*) malloc(sizeof (queue));
    q->front = q->rear = NULL;
    printf("\n:: Queue using Linked List ::\n");
    while (1) {
        printf("\nChoose from below Menu\n");
        printf("1. Add\n2. Delete\n3. Display\n4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: printf("Enter the value to be added: ");
                scanf("%d", &value);
                add(value, q);
                break;
            case 2: delete(q);
                break;
            case 3: display(q);
                break;
            case 4: exit(0);
            default: printf("\nWrong selection!!! Please try again!!!\n");
        }
    }
}

The output

:: Queue using Linked List ::

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be added: 1

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

1->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be added: 2

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

1->2->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be added: 3

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

1->2->3->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be added: 4

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

1->2->3->4->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 2

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

2->3->4->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be added: 5

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

2->3->4->5->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 2

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

3->4->5->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be added: 6

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 3

3->4->5->6->NULL

Choose from below Menu
1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 4

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *