#include <iostream>
#include <cstdlib>
using namespace std;
struct myQueue {
int data = 0;
myQueue *next = NULL;
} *Head;
int counT() {
int c = 0;
for (myQueue *Current = Head; Current != NULL; Current = Current -> next)
c++;
return c;
}
void display() {
if (Head == NULL)
cout << "Queue is Empty, Nothing to Display.";
else
for (myQueue *Current = Head; Current != NULL; Current = Current -> next)
cout << Current -> data << " ";
cout << endl << endl;
}
void enqueue(int num) {
myQueue *Current = Head;
myQueue *temp = new myQueue;
temp -> data = num;
temp -> next = NULL;
if (Head == NULL)
Head = temp;
else {
while (Current -> next != NULL)
Current = Current -> next;
Current -> next = temp;
}
}
void dequeue() {
if (Head == NULL)
cout << "Queue is Empty, Nothing to Dequeue." << endl;
else {
myQueue *temp = Head;
Head = Head -> next;
delete temp;
}
cout << endl;
}
int main() {
int choice, data;
while (true) {
cout << "Queue Operations" << endl;
cout << "----------------" << endl;
cout << "1. En-Queue" << endl;
cout << "2. De-Queue" << endl;
cout << "3. Display" << endl;
cout << "4. Size" << endl;
cout << "5. Exit" << endl;
cout << " --Choice: ";
cin >> choice;
switch (choice) {
case 1: {
cout << "Enter the Integer Value: ";
cin >> data;
cout << endl;
enqueue(data);
break;
}
case 2: {
dequeue();
break;
}
case 3: {
display();
break;
}
case 4: {
cout << "Size: " << counT() << endl << endl;
break;
}
case 5:
exit(0);
default:
cout << "Invalid Choice, Try Again." << endl << endl;
}
}
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.