void CTECList<Type>::addAtIndex(int index, const Type& value)
{
	ArrayNode<Type>* newNode = new ArrayNode<Type>(value);
	ArrayNode<Type>* current = head;
	ArrayNode<Type>* previous = current;

	assert(index <= size && index >= 0);

	if (index == 0)
	{
		addFront(value);
	}
	else if(index == size)
	{
		addEnd(value);
	}
	else
	{
		for(int i = 0; i < index; i++)
		{
			previous = current;
			current = current->getNext();
		}
		previous->setNext(newNode);
		newNode->setNext(current);
	}

	calculateSize();
}
Beispiel #2
0
CTECArray<Type>::CTECArray(int size)
{
    this->size = size;
    head = nullptr;

    if(size <= 0)
    {
        cerr << "Impossible!" << endl;
        return;
    }

    for(int index = 0; index < size; index++)
    {
        if(head != nullptr)
        {   //we have more than one arraynode.
            ArrayNode<Type> nextNode;
            nextNode.setNext(head);
            head = &nextNode;
        }
        else
        {
            //this is the first node in the array.
            ArrayNode &temp;
            head = &temp;
        }
    }
}
Type CTECList<Type>::removeEnd()
{

	assert(size > 0);

	ArrayNode<Type>* previous;
	ArrayNode<Type>* toDelete;
	ArrayNode<Type>* current = head;
	Type thing;

	if (current->getNext() == nullptr)
	{
		toDelete = current;
		thing = toDelete->getValue();
		delete toDelete;
		end = nullptr;
	}
	else
	{
		while(current->getNext() != nullptr)
		{
			previous = current;
			current = current->getNext();
		}
		toDelete = current;
		thing = toDelete->getValue();
		delete toDelete;
		previous->setNext() = nullptr;
		end = previous;
	}

	this->calculateSize();
	return thing;
}
void CTECList<Type>::addToFront(const Type& value)
{
	ArrayNode<Type>* newNode = new ArrayNode<Type>(value);

	if (head == nullptr)
	{
		newNode->setNext(nullptr);
		head = newNode;
		end = newNode;
	}
	else
	{
		newNode->setNext(head);
		head = newNode;
	}
	calculateSize();
}
void CTECList<Type>::addToEnd(const Type& value)
{
	ArrayNode<Type>* newNode = new ArrayNode<Type>(value);
	ArrayNode<Type>* current = head;

	if (current->getNext() == nullptr)
	{
		current->setNext(newNode);
		newNode->setNext(nullptr);
		end = newNode;
	}
	else
	{
		while(current->getNext() != nullptr)
		{
			current = current->getNext();
		}
		current->setNext(newNode);
		newNode->setNext(nullptr);
		end = newNode;
	}
	calculateSize();
}
Type CTECList<Type>::removeAtIndex(int index)
{
	ArrayNode<Type>* previous;
	ArrayNode<Type>* toDelete;
	ArrayNode<Type>* newNext;
	ArrayNode<Type>* current = head;
	Type thing;

	assert(this->size > 0);
	assert(index > 0 || index <= size);

	if (index == 0)
	{
		thing = removeFront();
	}
	else if (index == size - 1)
	{
		thing = removeEnd();
	}
	else
	{

		for (int i = 0; i < index; i++)
		{
			if (i == index - 1)
			{
				previous = current;
			}
			else if (i == index)
			{
				toDelete = current;
				thing = toDelete->getValue();
				newNext = current->getNext();
				delete toDelete;
				previous->setNext(newNext);
			}
			current = current->next();
		}
	}

	this->calculateSize();
	return thing;
}