my code stock.com

Opration
- isempty
- isfull
- enqueue
- dequeue
- traverse

Snippet options

Download: Download snippet as queue-using-array.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#include<iostream>
using namespace std;
#define SIZE 6

class queue_list{
private:
	int front;
	int rear;
	int c = 0;
	int queue[SIZE];
public:
	queue_list(){
		front = rear = 0;
	}

	bool isempty();
	bool isfull();
	void enqueue(int);
	int dequeue();
	void traverse();

};

bool queue_list::isempty(){
	if (c == 0)
		return true;
	else
		return false;
}

bool queue_list::isfull(){
	if (c == SIZE - 1)
		return true;
	else
		return false;
}

void queue_list::enqueue(int item){
	if (isfull())
	{
		cout << "is full";
		return;
	}
	else{
		if (c == 0)
			front = rear = 0;
		else if (rear == SIZE - 1)
			rear = 0;
		else
		rear++;
		queue[rear] = item;
		c++;
	}
}

int queue_list::dequeue(){
	if (isfull()){
		cout << "underflow";
		return -1;
	}
	int t = queue[front];
	c--;
	if (c == 0)
		front = rear = -1;
	else if (front == SIZE - 1)
		front = 0;
	else
		front++;
}

void queue_list::traverse(){
	if (isempty())
	{
		cout << "empty";
	}
	int temp = front;
	while (1)
	{
		cout << queue[temp] << "\t";
		if (temp == rear)
			break;
		else if (temp == SIZE - 1)
			temp = 0;
		else
			temp++;

	}
}

void main(){
	int item;
	int size;
	queue_list x;
	cout << "Enter SIZE";
	cin >> size;
	for (int i = 0; i < size; i++){
		cin >> item;
		x.enqueue(item);
	}

	x.traverse();
	x.dequeue();
	cout << endl;
	x.traverse();
	
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.