Mark As Completed Discussion

In the previous section, we discussed how to implement a queue using a linked list. Now, let's dive into the various operations that we can perform on a queue.

  1. Enqueue: The enqueue operation adds an element to the rear of the queue. In simple terms, it puts an item at the end of the line. To enqueue an element, we create a new node with the given data value and add it to the rear of the queue. If the queue is empty, we update both the front and rear pointers to the new node.
TEXT/X-C++SRC
1void enqueue(int item) {
2    Node* newNode = new Node;
3    newNode->data = item;
4    newNode->next = nullptr;
5
6    if (isEmpty()) {
7        front = rear = newNode;
8    }
9    else {
10        rear->next = newNode;
11        rear = newNode;
12    }
13}
  1. Dequeue: The dequeue operation removes an element from the front of the queue. In simple terms, it takes an item off from the front of the line. To dequeue an element, we remove the front node from the queue and update the front pointer to the next node. If the queue becomes empty after the dequeue operation, we update the rear pointer to null as well.
TEXT/X-C++SRC
1void dequeue() {
2    if (isEmpty()) {
3        cout << "Queue is empty" << endl;
4    }
5    else {
6        Node* temp = front;
7        cout << "Dequeued: " << front->data << endl;
8        front = front->next;
9
10        delete temp;
11    }
12}
  1. Get Front: The getFront operation returns the data value of the front node without removing it from the queue. If the queue is empty, we return -1 to indicate an error.
TEXT/X-C++SRC
1int getFront() {
2    if (isEmpty()) {
3        return -1;
4    }
5    else {
6        return front->data;
7    }
8}

Now that we have seen the basic queue operations, let's put them to use in an example:

TEXT/X-C++SRC
1#include <iostream>
2using namespace std;
3
4// Node class
5class Node {
6public:
7    int data;
8    Node* next;
9};
10
11// Queue class
12class Queue {
13private:
14    Node* front;
15    Node* rear;
16
17public:
18    // Constructor
19    Queue() {
20        front = nullptr;
21        rear = nullptr;
22    }
23
24    // Check if queue is empty
25    bool isEmpty() {
26        return (front == nullptr);
27    }
28
29    // Enqueue an element
30    void enqueue(int item) {
31        Node* newNode = new Node;
32        newNode->data = item;
33        newNode->next = nullptr;
34
35        if (isEmpty()) {
36            front = rear = newNode;
37        }
38        else {
39            rear->next = newNode;
40            rear = newNode;
41        }
42    }
43
44    // Dequeue an element
45    void dequeue() {
46        if (isEmpty()) {
47            cout << "Queue is empty" << endl;
48        }
49        else {
50            Node* temp = front;
51            cout << "Dequeued: " << front->data << endl;
52            front = front->next;
53
54            delete temp;
55        }
56    }
57
58    // Get the front element
59    int getFront() {
60        if (isEmpty()) {
61            return -1;
62        }
63        else {
64            return front->data;
65        }
66    }
67};
68
69int main() {
70    // Create a queue object
71    Queue q;
72
73    // Enqueue elements
74    q.enqueue(10);
75    q.enqueue(20);
76    q.enqueue(30);
77
78    // Dequeue elements
79    q.dequeue();
80    q.dequeue();
81
82    // Get the front element
83    int front = q.getFront();
84    cout << "Front element: " << front << endl;
85
86    return 0;
87}

This is a simple example that demonstrates the enqueue, dequeue, and getFront operations.

In the next section, we will explore the applications of queues in various real-world scenarios.

CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment