Queue using Linked List in C and C++
Here in this example you will get the program code to implement the Queue using Linked List in C and C++ using Data structure. This program can be implemented by Queue using Array.
Queue using Linked List in C
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 122 123 124 125 | //queue using linked list in c #include <stdio.h> #include <stdlib.h> // Structure of node struct Node { int data; struct Node* next; }; // Queue structure struct Queue { struct Node* front; struct Node* rear; }; // create new node struct Node* createNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; return node; } // create an empty queue struct Queue* createQueue() { struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue)); queue->front = queue->rear = NULL; return queue; } // check if queue is empty int isEmpty(struct Queue* queue) { return (queue->front == NULL); } // add element void enqueue(struct Queue* queue, int data) { struct Node* newNode = createNode(data); if (isEmpty(queue)) { queue->front = queue->rear = newNode; return; } queue->rear->next = newNode; queue->rear = newNode; } // delete element int dequeue(struct Queue* queue) { if (isEmpty(queue)) { return -1; } int data = queue->front->data; struct Node* temp = queue->front; queue->front = queue->front->next; if (queue->front == NULL) { queue->rear = NULL; } // Free the memory of the dequeued node free(temp); return data; } // display the queue void displayQueue(struct Queue* queue) { if (isEmpty(queue)) { printf("Queue is empty.\n"); return; } struct Node* current = queue->front; printf("Queue: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { struct Queue* queue = createQueue(); int choice, data; while (1) { printf("\nQueue using Linked list\n"); printf("1. Add element\n"); printf("2. Remove element\n"); printf("3. Display elements\n"); printf("4. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter data to add to queue: "); scanf("%d", &data); enqueue(queue, data); break; case 2: data = dequeue(queue); if (data != -1) { printf("Removed element: %d\n", data); } else { printf("Queue is empty.\n"); } break; case 3: displayQueue(queue); break; case 4: printf("Exiting the program.\n"); exit(0); default: printf("Invalid choice.\n"); } } return 0; } |
Queue using Linked List C++
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 122 123 124 125 126 | //queue using linked list in cpp #include <stdio.h> #include<iostream.h> #include<stdlib.h> // Structure of node struct Node { int data; struct Node* next; }; // Queue structure struct Queue { struct Node* front; struct Node* rear; }; // create new node struct Node* createNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; return node; } // create an empty queue struct Queue* createQueue() { struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue)); queue->front = queue->rear = NULL; return queue; } // check if queue is empty int isEmpty(struct Queue* queue) { return (queue->front == NULL); } // add element void enqueue(struct Queue* queue, int data) { struct Node* newNode = createNode(data); if (isEmpty(queue)) { queue->front = queue->rear = newNode; return; } queue->rear->next = newNode; queue->rear = newNode; } // delete element int dequeue(struct Queue* queue) { if (isEmpty(queue)) { return -1; } int data = queue->front->data; struct Node* temp = queue->front; queue->front = queue->front->next; if (queue->front == NULL) { queue->rear = NULL; } // Free the memory of the dequeued node free(temp); return data; } // display the queue void displayQueue(struct Queue* queue) { if (isEmpty(queue)) { cout<<"Queue is empty.\n"; return; } struct Node* current = queue->front; cout<<"Queue: "; while (current != NULL) { printf("%d ", current->data); current = current->next; } cout<<"\n"; } int main() { struct Queue* queue = createQueue(); int choice, data; while (1) { cout<<"\nQueue using Linked list\n"; cout<<"1. Add element\n"; cout<<"2. Remove element\n"; cout<<"3. Display elements\n"; cout<<"4. Exit\n"; cout<<"Enter your choice: "; cin>>choice; switch (choice) { case 1: cout<<"Enter data to add to queue: "; cin>>data; enqueue(queue, data); break; case 2: data = dequeue(queue); if (data != -1) { cout<<"Removed element : \n"<<data; } else { cout<<"Queue is empty.\n"; } break; case 3: displayQueue(queue); break; case 4: cout<<"Exiting the program.\n"; exit(0); default: cout<<"Invalid choice.\n"; } } return 0; } |
Output
Queue using Linked list
1. Add element
2. Remove element
3. Display elements
4. Exit
Enter your choice: 1
Enter data to add to queue: 100
Queue using Linked list
1. Add element
2. Remove element
3. Display elements
4. Exit
Enter your choice: 1
Enter data to add to queue: 200
Queue using Linked list
1. Add element
2. Remove element
3. Display elements
4. Exit
Enter your choice: 1
Enter data to add to queue: 300
Queue using Linked list
1. Add element
2. Remove element
3. Display elements
4. Exit
Enter your choice: 3
Queue: 100 200 300
Queue using Linked list
1. Add element
2. Remove element
3. Display elements
4. Exit
Enter your choice: 2
Removed element: 100
Queue using Linked list
1. Add element
2. Remove element
3. Display elements
4. Exit
Enter your choice: 3
Queue: 200 300
Queue using Linked list
1. Add element
2. Remove element
3. Display elements
4. Exit
Enter your choice: 4
Exiting the program.