Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Array implementation of Queue using class in c++

#include<iostream>
using namespace std;
#define n 101

class Queue
{
public:
	Queue()
	{
		rear = -1;
		front = -1;
	}
	bool IsEmpty()
	{
		if (rear == -1 && front == -1)
			return true;
		return false;
	}
	bool IsFull()
	{
		if ((rear + 1) % n == front)
			return true;
		return false;
	}
	void EnQueue(int x)
	{
		if (IsFull())
		{
			cout << "Queue is full!
";
			return;
		}
		else if (IsEmpty())
		{
			rear = front = 0;
		}
		else
		{
			rear = (rear + 1) % n;
		}
		Array[rear] = x;
	}
	void Dequeue()
	{
		if (IsEmpty())
		{
			cout << "Queue is Empty!
";
			return;
		}
		else if (rear == front)
		{
			rear = front = -1;
		}
		else
		{
			front = (front + 1) % n;
		}
	}
	int Front()
	{
		if (front == -1)
		{
			cout << "Queue is Empty!
";
			return -1;
		}
		return Array[front];
	}
	void print()
	{
		int cnt = (rear + n - front) % n + 1;
		cout << "Queue: ";
		for (int i = 0; i < cnt; i++)
		{
			int idx = (front + i) % n;
			cout << Array[idx] << " ";
		}
		cout << "

";
	}
private:
	int Array[n];
	int rear;
	int front;
};

int main()
{
	Queue q;

	//insert
	q.EnQueue(1);
	q.EnQueue(2);
	q.EnQueue(3);
	q.EnQueue(4);
	q.EnQueue(5);

	//print
	q.print();      //should print 1 2 3 4 5 

	//delete
	q.Dequeue();    //should delete 1
	q.Dequeue();    //should delete 2

	//print
	q.print();     //should print 3 4 5

	//get feont 
	cout << q.Front() << "
";   //should print 3

	return 0;
}
Comment

Array implementation of Queue using class in c++

#include<iostream>
using namespace std;
#define n 101

class Queue
{
public:
	Queue()
	{
		rear = -1;
		front = -1;
	}
	bool IsEmpty()
	{
		if (rear == -1 && front == -1)
			return true;
		return false;
	}
	bool IsFull()
	{
		if ((rear + 1) % n == front)
			return true;
		return false;
	}
	void EnQueue(int x)
	{
		if (IsFull())
		{
			cout << "Queue is full!
";
			return;
		}
		else if (IsEmpty())
		{
			rear = front = 0;
		}
		else
		{
			rear = (rear + 1) % n;
		}
		Array[rear] = x;
	}
	void Dequeue()
	{
		if (IsEmpty())
		{
			cout << "Queue is Empty!
";
			return;
		}
		else if (rear == front)
		{
			rear = front = -1;
		}
		else
		{
			front = (front + 1) % n;
		}
	}
	int Front()
	{
		if (front == -1)
		{
			cout << "Queue is Empty!
";
			return -1;
		}
		return Array[front];
	}
	void print()
	{
		int cnt = (rear + n - front) % n + 1;
		cout << "Queue: ";
		for (int i = 0; i < cnt; i++)
		{
			int idx = (front + i) % n;
			cout << Array[idx] << " ";
		}
		cout << "

";
	}
private:
	int Array[n];
	int rear;
	int front;
};

int main()
{
	Queue q;

	//insert
	q.EnQueue(1);
	q.EnQueue(2);
	q.EnQueue(3);
	q.EnQueue(4);
	q.EnQueue(5);

	//print
	q.print();      //should print 1 2 3 4 5 

	//delete
	q.Dequeue();    //should delete 1
	q.Dequeue();    //should delete 2

	//print
	q.print();     //should print 3 4 5

	//get feont 
	cout << q.Front() << "
";   //should print 3

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: remove value from vector c++ 
Cpp :: qt qstring to double 
Cpp :: __lg(x) in c++ 
Cpp :: read string from binary file in c++ 
Cpp :: qt label set text color 
Cpp :: c++ std::copy to cout 
Cpp :: create n threads cpp 
Cpp :: c++ vector add only unique elements 
Cpp :: vector of int to string c++ 
Cpp :: how to know in flutter if signin with user completed in firebase 
Cpp :: c++ print every element in array 
Cpp :: how to initialize 2d vector in c++ 
Cpp :: cannot open include file: 
Cpp :: c++ rule of five 
Cpp :: static_cast c++ 
Cpp :: what is the difference between i++ and ++ i ? 
Cpp :: double to string c++ 
Cpp :: C++ Program to Reverse an Integer 
Cpp :: vector to string c++ 
Cpp :: how to add colored text in c++ 
Cpp :: in c++ the default value of uninitialized array elements is 
Cpp :: string length c++ 
Cpp :: rand c++ 
Cpp :: convert binary string to int c++ 
Cpp :: how to remove an element from a vector by value c++ 
Cpp :: How to pause a c++ program. 
Cpp :: clear the input buffer in cpp 
Cpp :: array max and minimum element c++ 
Cpp :: string.begin() c++ 
Cpp :: c++ string element access 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =