Implementation of Queue by Linked List C++:
In this example we have shown how to implement queue by linked list. We have inserted values, then printed the front and back element of the queue. We have removed two elements and then displayed the final queue.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() : front(nullptr), rear(nullptr) {}
// Insert an element at the rear of the queue
void insert(int value) {
Node* newNode = new Node(value);
if (rear == nullptr) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
cout << "Element inserted: " << value << endl;
}
// Delete the element at the front of the queue
void remove() {
if (front == nullptr) {
cout << "Queue is empty. Unable to remove element." << endl;
return;
}
Node* temp = front;
front = front->next;
if (front == nullptr) {
rear = nullptr;
}
cout << "Element removed: " << temp->data << endl;
delete temp;
}
// Display the elements of the queue
void display() {
if (front == nullptr) {
cout << "Queue is empty." << endl;
return;
}
cout << "Queue elements: ";
Node* temp = front;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Get the element at the front of the queue
int getFront() {
if (front == nullptr) {
cout << "Queue is empty." << endl;
return -1; // Return a sentinel value or throw an exception for error handling
}
return front->data;
}
// Get the element at the back of the queue
int getBack() {
if (rear == nullptr) {
cout << "Queue is empty." << endl;
return -1; // Return a sentinel value or throw an exception for error handling
}
return rear->data;
}
};
int main() {
Queue myQueue;
myQueue.insert(10);
myQueue.insert(20);
myQueue.insert(30);
myQueue.display();
cout << "Front element: " << myQueue.getFront() << endl;
cout << "Back element: " << myQueue.getBack() << endl;
myQueue.remove();
myQueue.remove();
myQueue.display();
return 0;
}
Output:
Element inserted: 10
Element inserted: 20
Element inserted: 30
Queue elements: 10 20 30
Front element: 10
Back element: 30
Element removed: 10
Element removed: 20
Queue elements: 30
Here's how the code works:
Here's a breakdown of the code and its functionality:
- The code defines a structure called 'Node' to represent a node in a queue. Each node contains an integer value and a pointer to the next node.
- The code defines a class called 'Queue' to implement a queue data structure using a linked list.
- The 'Queue' class has private member variables 'front' and 'rear', which represent the front and rear pointers of the queue.
- The constructor of the 'Queue' class initializes the 'front' and 'rear' pointers to 'nullptr', indicating an empty queue.
- The 'insert' method of the 'Queue' class inserts a new element at the rear of the queue. It creates a new node with the given value and updates the pointers accordingly.
- The 'remove' method of the 'Queue' class removes the element at the front of the queue. It updates the pointers and deletes the node from memory.
- The 'display' method of the 'Queue' class displays the elements of the queue by traversing the linked list starting from the front pointer.
- The 'getFront' method of the 'Queue' class returns the value of the element at the front of the queue without removing it.
- The 'getBack' method of the 'Queue' class returns the value of the element at the back of the queue without removing it.
- In the 'main' function, an instance of the 'Queue' class called 'myQueue' is created.
- Three elements (10, 20, and 30) are inserted into 'myQueue' using the 'insert' method.
- The 'display' method is called to show the elements of 'myQueue'.
- The 'getFront' and 'getBack' methods are called to retrieve the front and back elements of 'myQueue', respectively.
- Two elements are removed from 'myQueue' using the 'remove' method.
- The 'display' method is called again to show the updated elements of 'myQueue'.
Finally, the program returns 0 to indicate successful execution.
This code implements a basic queue data structure using a linked list and provides methods to insert, remove, display, and access elements in the queue.