Sunday, May 9, 2021

Queue using Linked List in C Program

 We will create an example on queue using linked list in C program. 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.

Queue using Linked List in C Program


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.


Queue using C Program

A queue can defined by the following structure using pointer in C language:

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, we will perform several operations on queue, such as, adding an element, deleting an element, displaying current elements in queue and exit when you don’t want to perform any operation.

/* 
 * File:   Queue.c
 */

#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");
        }
    }
}


Testing the Program

By executing the above C program you will get the following 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



No comments:

Post a Comment