Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,892 questions

51,823 answers

573 users

How to define and use a simple queue class with int array in C++

1 Answer

0 votes
#include <iostream>
#include <deque>

using std::cout;
using std::endl;
using std::deque;

#define SIZE 30

class CQueue {
	int queue[SIZE];           
	int ihead, itail;            
public:
	CQueue();              
	void push(int n);
	int pop();                 
};

CQueue::CQueue() {
	ihead = itail = 0;
}

void CQueue::push(int n) {
	if (itail + 1 == SIZE) {
		cout << "Queue is full" << endl;
		return;
	}
	itail++;
	if (itail == SIZE) itail = 0; 
	queue[itail] = n;
}

int CQueue::pop()
{
	if (ihead == itail) {
		cout << "Queue is empty" << endl;
		return -1;  
	}
	ihead++;
	if (ihead == SIZE) ihead = 0; 
	return queue[ihead];
}

int main()
{
	CQueue q;

	for (int i = 1; i <= 6; i++) {
		q.push(i);
	}

	for (int i = 1; i <= 6; i++) {
		cout << q.pop() << ' ';
	}

	cout << endl;

	return 0;
}


/*
run:

1 2 3 4 5 6

*/

 



answered May 11, 2018 by avibootz

Related questions

1 answer 186 views
186 views asked Jun 28, 2015 by avibootz
1 answer 142 views
1 answer 137 views
...