Marcus Lee / LinearArray

linearArray.hpp

Committer:
UHSLMarcus
Date:
2017-03-08
Revision:
6:314f180362cb
Parent:
5:0f65cdadb1a4
Child:
7:6e24d8ccecd4

File content as of revision 6:314f180362cb:

template<class type>
LinearArray<type>::LinearArray(int size, bool forced) :
		_elem_count(0), _array_size(size), _front(0), _rear(-1), _forced(forced) {
	_array = new type[size];
}

template<class type>
LinearArray<type>::~LinearArray() {
	delete[] _array;
}

template<class type>
int LinearArray<type>::push(type item) {

	int ret = -1;
	if (elem_count < _array_size) {
		if (_rear == _array_size - 1) _rear = -1;
		_array[++_rear] = item;
		ret = _elem_count++;
	}

	return ret;
}

template<class type>
type& LinearArray<type>::pop() {

	type item = NULL;

	if (elem_count > 0) {
		item = _array[_front++];
		if (_front == _array_size) _front = 0;
		_elem_count--;
	}

	return item;

}

template<class type>
type& LinearArray<type>::peek() {

	type item = NULL;

	if (_front <= _rear) {
		item = _array[_front];
	}

	return item;

}

template<class type>
int LinearArray<type>::size() {
	return _array_size;
}

template<class type>
int LinearArray<type>::count() {
	return _elem_count;
}

template<class type>
bool LinearArray<type>::full() {
	return _elem_count == _array_size;
}