C Program To Implement Queue using Linked List Data Structure
Learn How To Implement Queue using Linked List in C Programming Language. We have explained and demonstrated the code for most of the operations in a Queue Data Structure such as Insertion, Deletion, Displaying all the Elements and the Peek Function.
To understand this C Program To Implement Queue using Linked Lists, you should be well versed with Linked Lists concept and Queue implementation using Static Arrays.
Must Read: C Program To Implement Queue using Array
What is a Queue?
A Queue is one of the several data structures which stores elements in it. The Queues are based on First In First Out (FIFO) principle. As the name suggests, the program that element that comes first will be stored in the Queue first. In a Queue, an element is inserted from the Rear end whereas the element is deleted from the Front end.
The Queues are highly implemented in Scheduling Algorithms such as Priority Scheduling, FCFS CPU Scheduling, Shortest Job First Scheduling and many more. Here, we focus on implementation of Queue using Linked List Data Structure in C Programming.
Application of Queue in Real World
- Queue of Data Packets in Communication Technology
- Printer Spooling
- Scheduling Algorithms in CPU
- Computer Simulation Programs
Must Read: Linked List Implementation in C Programming
C Program For Queue using Linked List with Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *link; }*front = NULL, *rear = NULL; int isEmpty() { if(front == NULL) { return 1; } else { return 0; } } void insert(int item) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); if(temp == NULL) { printf("Queue is not Allocated\n"); return; } temp->data = item; temp->link = NULL; if(front == NULL) { front = temp; } else { rear->link = temp; } rear = temp; } int delete_node() { struct node *temp; int item; if(isEmpty()) { printf("Queue is Empty\n"); exit(1); } temp = front; item = temp->data; front = front->link; free(temp); return item; } int peek() { if(isEmpty()) { printf("Queue is Empty\n"); exit(1); } return front->data; } void display() { struct node *ptr; if(isEmpty()) { printf("Queue is Empty\n"); return; } printf("Queue Elements (or Nodes):\n"); for(ptr = front; ptr != NULL; ptr = ptr->link) { printf("%d ", ptr->data); } printf("\n\n"); } int main() { int option, item; while(1) { printf("1. Insert an Element (Node) in the Queue\n"); printf("2. Delete an Element from the Queue\n"); printf("3. Display Element at the Front position\n"); printf("4. Display All Elements of the queue\n"); printf("5. Exit\n"); printf("Enter your option:\t"); scanf("%d", &option); switch(option) { case 1: printf("Enter the Element to Add in Queue:\t"); scanf("%d", &item); insert(item); break; case 2: printf("The Deleted Element fromt the Queue:\t%d\n", delete_node()); break; case 3: printf("Element at the Front:\t%d\n", peek()); break; case 4: display(); break; case 5: exit(1); default: printf("Wrong option\n"); } } return 0; } |
Must Read: C Program To Implement Round Robin CPU Scheduling Algorithm
Output

In case you get any Compilation Errors or any doubts in this C Program for Queue using Linked List Data Structure in C Programming, let us know about it in the Comment Section below. Find more information about Queue Data Structures here.
Excellent code for Queue in C programming. This is just so simple.
cool