Improving a queue

Just added a few things that adds a lot more functionality. If anyone sees anything wrong or would like to optimize further, please post below.

// main.cpp
// Queue
//
// Originally coded by Mike Kim
// Modified by Jesse Santos on 13-10-12
// Added clear()
// Added insert()
// Added removeAt()
// Added _size and size()
// Optimized the destructor
#include<iostream>
using namespace std;

class Queue;

class QNode{
	int _data;
	QNode* _next;
	QNode(int data, QNode* next){
		_data = data;
		_next = next;
	}
	friend class Queue;
};

class Queue{
	QNode* _head;
	QNode* _tail;
	unsigned int _size;
public:
	Queue();
	virtual ~Queue();
	void add(int data);
	unsigned int size();
	void clear();
	void insert(int data, int index);
	int removeAt(int index);
	int remove();
	bool isEmpty();
};

Queue::Queue(){
	_head = 0;
	_tail = 0;
	_size = 0;
}

Queue::~Queue(){
	clear();
}

void Queue::clear(){
    QNode* toDel;
    while(_head){
        toDel = _head;
        _head = _head->_next;
        delete toDel;
    }
    //Resets all values
    _head = (QNode*)0;
    _tail = (QNode*)0;
    _size = 0;
}

unsigned int Queue::size(){
	return _size;
}

//Adds data to the end of the queue
void Queue::add(int data){
	QNode* temp = new QNode(data, 0);
	if(isEmpty()){
		_head = temp;
		_tail = temp;
	}else{
		_tail->_next = temp;
		_tail = temp;
	}
	_size++;
}

//Inserts data anywhere in the queue (useful for adding things in reverse)
void Queue::insert(int data, int index){
	if(index > _size){
		add(data);
		return;
	}
	if(isEmpty()){
		add(data);
		return;
	}

	QNode* temp = _head;
	int curIndex;
	for(curIndex = 0; curIndex < index-1; curIndex++)
		temp = temp->_next;

	if(curIndex){
		QNode* ins = new QNode(data, temp->_next);
		temp->_next = ins;
	}else{
		QNode* ins = new QNode(data, _head);
		_head = ins;
	}
	_size++;
}

//Removes an element from the beginning of the queue
int Queue::remove(){
	QNode* temp = _head->_next;
	int ret = _head->_data;
	delete _head;
	_head = temp;
	_size--;
	return ret;
}

//Removes any element for anywhere in the queue
int Queue::removeAt(int index){
	if(isEmpty()){
		return 0;
	}
	if(!index){
		remove();
		return 0;
	}

	QNode* temp = _head;
	int curIndex;
	for(curIndex = 0; curIndex < index-1; curIndex++)
		temp = temp->_next;

	_size--;
	if(curIndex){
		QNode* previous = temp;
		temp = temp->_next;
		QNode* next = temp->_next;
		int ret = temp->_data;
		delete temp;
		previous->_next = next;
		return ret;		
	}else{
		_head = temp;
		temp = temp->_next;
		QNode* next = temp->_next;
		int ret = temp->_data;
		delete temp;
		_head->_next = next;
		return ret;
	}
}

bool Queue::isEmpty(){
	return(!_head);
}

int main(){
	Queue Q;
	for(int i=0; i<100; i+=10){
		Q.insert(i,0);
	}
	cout<<"Size after insert: "<<Q.size()<<endl;
	for(int i=0; i<100; i+=10){
		Q.add(i);
	}
	cout<<"Size after adding: "<<Q.size()<<endl;
	Q.insert(1234, 0);
	Q.insert(5678, 6);
	Q.insert(1564, 15);
	cout<<"Size: "<<Q.size()<<endl;
	Q.removeAt(9);
	Q.removeAt(1);
	Q.removeAt(0);
	cout<<"Size: "<<Q.size()<<endl;
	while(!Q.isEmpty()){
		cout<<Q.remove()<<" ";
	}
	cout<<endl<<"Size: "<<Q.size()<<endl;
} 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s