Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- Node* next;
- };
- struct Queue {
- Node* front;
- Node* rear;
- };
- void initializeQueue(Queue& queue) {
- queue.front = nullptr;
- queue.rear = nullptr;
- }
- bool isEmpty(const Queue& queue) {
- return (queue.front == nullptr);
- }
- void enqueue(Queue& queue, int data) {
- Node* newNode = new Node;
- newNode->data = data;
- newNode->next = nullptr;
- if (queue.front == nullptr) {
- queue.front = newNode;
- queue.rear = newNode;
- } else {
- queue.rear->next = newNode;
- queue.rear = newNode;
- }
- }
- void dequeue(Queue& queue) {
- if (isEmpty(queue)) {
- cout << "Queue is empty. Unable to dequeue." << endl;
- } else {
- Node* temp = queue.front;
- queue.front = queue.front->next;
- delete temp;
- if (queue.front == nullptr) {
- queue.rear = nullptr;
- }
- }
- }
- void display(const Queue& queue) {
- if (isEmpty(queue)) {
- cout << "Queue is empty." << endl;
- } else {
- Node* current = queue.front;
- cout << "Queue: ";
- while (current != nullptr) {
- cout << current->data << " ";
- current = current->next;
- }
- cout << endl;
- }
- }
- int main() {
- Queue queue1, queue2;
- initializeQueue(queue1);
- initializeQueue(queue2);
- int choice, item;
- do {
- cout << "\nQueue Operations:" << endl;
- cout << "1. Enqueue" << endl;
- cout << "2. Dequeue" << endl;
- cout << "3. Display" << endl;
- cout << "0. Exit" << endl;
- cout << "Enter your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- cout << "Enter the item to enqueue: ";
- cin >> item;
- if (item % 2 == 0) {
- enqueue(queue1, item);
- } else {
- enqueue(queue2, item);
- }
- break;
- case 2:
- if (!isEmpty(queue1)) {
- dequeue(queue1);
- } else if (!isEmpty(queue2)) {
- dequeue(queue2);
- } else {
- cout << "Both queues are empty. Unable to dequeue." << endl;
- }
- break;
- case 3:
- cout << "Queue 1: ";
- display(queue1);
- cout << "Queue 2: ";
- display(queue2);
- break;
- case 0:
- cout << "Exiting the program. Bye!" << endl;
- break;
- default:
- cout << "Invalid choice. Please enter a valid option." << endl;
- }
- } while (choice != 0);
- return 0;
- }
Advertisement
Advertisement