0% found this document useful (0 votes)
34 views4 pages

Queue Data Structure

This C++ program defines a Node class to represent nodes in a linked list, with data and pointer to next node. It also defines a Queue class using the linked list to implement a queue, with methods to enqueue, dequeue, display front node, get length, and display the queue. The main function tests the queue class by enqueueing and dequeueing nodes of different values and calling the methods.

Uploaded by

Hamza Sajid
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
34 views4 pages

Queue Data Structure

This C++ program defines a Node class to represent nodes in a linked list, with data and pointer to next node. It also defines a Queue class using the linked list to implement a queue, with methods to enqueue, dequeue, display front node, get length, and display the queue. The main function tests the queue class by enqueueing and dequeueing nodes of different values and calling the methods.

Uploaded by

Hamza Sajid
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 4

#include<iostream>

using namespace std;

class Node {
private:
int object;
Node* nextNode;
public:
void setData(int o)
{
object = o;
}
int getData()
{
return object;
}
void setNext(Node* n)
{
nextNode = n;
}
Node* getNext()
{
return nextNode;
}
};
class Queue {
private:
Node* front;
Node* rear;
int size;
public:
Queue();
void enqueue(int addObject);
void dequeue();
void frontNode();
int length();
void display();
};
Queue::Queue()
{
front = NULL;
rear = NULL;
size = 0;
}
void Queue::enqueue(int addObject)
{
Node* newNode = new Node();
newNode->setData(addObject);

if (front == NULL)
{
newNode->setNext(NULL);
front = newNode;
rear = newNode;
}
else
{
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
}
size++;
}
void Queue::dequeue()
{
if (front == NULL)
{
cout << "Queue is empty, however you can't perform this operation!" <<
endl;
}
else
{
int ret = front->getData();
cout << "This node has been deleted: " << ret << endl;

Node* temp = front;


front = front->getNext();

delete temp;

size--;
}
}
void Queue::frontNode()
{
if (front == NULL)
{
cout << "Queue is empty, however, you can't perform this operation!" <<
endl;
}
else
{
cout << "The front Node in the Queue is: " << front->getData() << endl;
}
}
int Queue::length()
{
cout << "The length of a Queue is: ";
return size;
}
void Queue::display()
{
if (front == NULL)
{
cout<<"Queue is empty, however you can't perform this
operation!"<<endl;
}
else
{
Node* temp = front;
cout << "The elements in the Queue are: ";
while (temp)
{
cout << temp->getData();
cout << " ";
temp = temp->getNext();
}
}
}
int main()
{
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

q.display();

cout << endl;

q.frontNode();

q.dequeue();

q.dequeue();

q.frontNode();

q.enqueue(50);

q.enqueue(100);

q.display();

cout << endl;

cout << q.length();

cout << endl;

q.dequeue();

q.display();

cout<<endl;

q.dequeue();

q.display();

cout << endl;

q.dequeue();

q.display();

q.frontNode();

q.enqueue(1000);
q.enqueue(2000);
q.enqueue(5000);

q.display();

cout << endl;

q.frontNode();
cout << q.length();

getchar();

You might also like