0% found this document useful (0 votes)
11 views3 pages

Document 1

Uploaded by

tanweerjamal833
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Document 1

Uploaded by

tanweerjamal833
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

QUEUE (IMPLEMENTATION USING LINKED LIST)

#include <iostream>
using namespace std;

// Node structure for the linked list


struct Node {
int data;
Node* next;
};

// Global front and rear pointers for the queue


Node* front = nullptr;
Node* rear = nullptr;

// Function to enqueue an element


void enqueue(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr;

if (front == nullptr) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
cout << value << " enqueued to queue" << endl;
}

// Function to dequeue an element


void dequeue() {
if (front == nullptr) {
cout << "Queue Underflow" << endl;
return;
}
Node* temp = front;
int value = temp->data;
front = front->next;
if (front == nullptr) {
rear = nullptr; // Reset rear if queue becomes empty
}
delete temp;
cout << value << " dequeued from queue" << endl;
}

// Function to get the front element


int frontElement() {
if (front == nullptr) {
cout << "Queue is empty" << endl;
return -1; // Indicate an empty queue with a special value
}
return front->data;
}

// Function to display the queue elements


void display() {
if (front == nullptr) {
cout << "Queue is empty" << endl;
return;
}
Node* temp = front;
cout << "Queue elements: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

// Function to check if the queue is empty


bool checkEmpty() {
return front == nullptr;
}

// Function to count the number of elements in the queue


int countElements() {
int count = 0;
Node* temp = front;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}

int main() {
int choice, value;
while (true) {
cout << "\nQueue Operations:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Front\n";
cout << "4. Display\n";
cout << "5. Check Empty\n";
cout << "6. Count Elements\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
value = frontElement();
if (value != -1) {
cout << "Front element: " << value << endl;
}
break;
case 4:
display();
break;
case 5:
if (checkEmpty()) {
cout << "Queue is Empty" << endl;
} else {
cout << "Queue is not Empty" << endl;
}
break;
case 6:
cout << "Number of elements in queue: " << countElements() << endl;
break;
case 7:
cout << "Exiting program..." << endl;
return 0;
default:
cout << "Invalid choice" << endl;
}
}
return 0;
}

You might also like