-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueusingLinkedlist.cpp
121 lines (97 loc) · 2.66 KB
/
QueueusingLinkedlist.cpp
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 <iostream>
using namespace std;
// Define the structure for each node in the linked list
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* frontPtr; // Pointer to the front element of the queue
Node* rearPtr; // Pointer to the rear element of the queue
int size; // Current number of elements in the queue
public:
// Constructor to initialize the queue
Queue() : frontPtr(nullptr), rearPtr(nullptr), size(0) {}
// Destructor to free the dynamically allocated memory
~Queue() {
while (!isEmpty()) {
dequeue();
}
}
// Function to add an element to the rear of the queue (enqueue)
void enqueue(int item) {
Node* newNode = new Node();
newNode->data = item;
newNode->next = nullptr;
if (isEmpty()) {
frontPtr = newNode;
} else {
rearPtr->next = newNode;
}
rearPtr = newNode;
size++;
}
// Function to remove an element from the front of the queue (dequeue)
int dequeue() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
int frontElement = frontPtr->data;
Node* temp = frontPtr;
frontPtr = frontPtr->next;
if (frontPtr == nullptr) {
rearPtr = nullptr;
}
delete temp;
size--;
return frontElement;
}
// Function to get the front element of the queue
int front() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
return frontPtr->data;
}
// Function to check if the queue is empty
bool isEmpty() {
return frontPtr == nullptr;
}
// Function to get the current size of the queue
int getSize() {
return size;
}
// Function to display the elements of the queue
void display() {
if (isEmpty()) {
cout << "Queue is empty\n";
return;
}
Node* current = frontPtr;
cout << "Queue: ";
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
Queue queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.display();
cout << "Front element: " << queue.front() << endl;
cout << "Queue size: " << queue.getSize() << endl;
queue.dequeue();
cout << "Front element after dequeue: " << queue.front() << endl;
cout << "Queue size after dequeue: " << queue.getSize() << endl;
queue.display();
return 0;
}