View Single Post
Old 04-15-2003, 06:11 PM   #7 (permalink)
gqchynaboy
Registered User
 
Join Date: Apr 2003
Posts: 4
gqchynaboy is on a distinguished road
Code:
template <class T>
bool List<T> ::Method1 (const T & value)
{
	if (used == capacity)
			if(Method5 (capacity + CHUNK) ==false)
				return false;
	if (first == -1)
			first = last = 0;
	else if (first == 0)
		first = capacity -1;
	else
		first --;
	data[first] = value;
	used++;
	return true;
}

template <class T>
bool List<T> ::Method2 (const T & value)
{
	if (used == capacity)
			if(Method5 (capacity + CHUNK) ==false)
				return false;
	if (last == -1)
			first = last = 0;
	else 
		last = (last +1) % capacity;
	data[first] = value;
	used++;
	return true;
}

template <class T>
bool List<T> ::Method3 ( T & value)
{
		if (used ==0)
			return false;
		value = data[first];
		if(capacity - used >= 1.5 * CHUNK)
				if(Method5 (capacity + CHUNK) ==false)
					return false;
		used--;
		if (used ==0)
			first = last = -1;
		else
			first = (first +1) % capacity;
			return true;
}

template <class T>
bool List<T> ::Method4 ( T & value)
{
	if (used ==0)
			return false;
		value = data[first];
		if(capacity - used >= 1.5 * CHUNK)
				if(Method5 (capacity + CHUNK) ==false)
					return false;
	used--;
		if (used ==0)
			first = last = -1;
		else if (last ==0)
			last = capacity -1;
		else 
			last --;

		return true;
}



template <class T>
bool List<T> ::Method5 (const size_t & new_capacity)
{
	T * temp = data;
	data = new T[new_capacity];
	if(data == NULL)
	{
		data = temp;
		return false;
	}
	if (used >0)
	{
			int j =first;
			int k = (new_capacity - used) /2;
			first =k;
			last = first + used -1;
			for (int i =0; i <used ; i++)
			{
				data[k] = temp[j];
				j = (j+1) % capacity;
				k++;
			}
	}
	capacity = new_capacity;
	delete []temp;
	return true;
}
gqchynaboy is offline   Reply With Quote