-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp12QueueLinkedList.cpp
More file actions
112 lines (104 loc) · 2.75 KB
/
p12QueueLinkedList.cpp
File metadata and controls
112 lines (104 loc) · 2.75 KB
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
/*
12). Design, Develop and Implement a menu driven Program for the following operations on QUEUE (Linked Implementation)
a. Check Overflow and Underflow situations on QUEUE
b. Insert an Element on to QUEUE
c. Delete an Element from QUEUE
d. Display the elements of QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
*/
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* front = nullptr;
Node* rear = nullptr;
void insert(int value) {
Node* newNode = new Node();
if (newNode == nullptr) {
cout << "Overflow! Cannot insert.\n";
return;
}
newNode->data = value;
newNode->next = nullptr;
if (rear == nullptr) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
cout << value << " inserted.\n";
}
void deleteElement() {
if (front == nullptr) {
cout << "Underflow! Queue is empty.\n";
return;
}
Node* temp = front;
cout << temp->data << " deleted.\n";
front = front->next;
if (front == nullptr) {
rear = nullptr;
}
delete temp;
}
void display() {
if (front == nullptr) {
cout << "Queue is empty.\n";
return;
}
Node* temp = front;
cout << "Queue: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
cout<<"ABHISHEK SINGH 2315272/2435222";
char choice;
int value;
do {
cout << "\n--- Queue Operations Menu ---\n";
cout << "a. Insert an element into Queue\n";
cout << "b. Delete an element from Queue\n";
cout << "c. Display elements of Queue\n";
cout << "d. Check Overflow/Underflow\n";
cout << "e. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 'a':
case 'A':
cout << "Enter value: ";
cin >> value;
insert(value);
break;
case 'b':
case 'B':
deleteElement();
break;
case 'c':
case 'C':
display();
break;
case 'd':
case 'D':
if (front == nullptr)
cout << "Queue Underflow (Empty Queue).\n";
else
cout << "Queue has elements (No Overflow or Underflow).\n";
break;
case 'e':
case 'E':
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 'e' && choice != 'E');
return 0;
}