LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T> &ListToBeAssigned)
{
	std::cout << "Copy assignement operator called for @ " << this << std::endl;

	if (&ListToBeAssigned != this) {
		// Free The memory held by this
		deleteList();

		ListElement<T> *copy = ListToBeAssigned._head;
		if (copy != nullptr) {
			_head = new ListElement<T>(copy->getValue());
			ListElement<T> *curr = _head;
			copy = copy->getNext();

			// copy List Elements Until we reach the end of the 
			// List to be copied
			while (copy != nullptr) {
				curr->setNext(new ListElement<T>(copy->getValue()));
				copy = copy->getNext();
				curr = curr->getNext();
			}
		}

	}

	return *this;
}
LinkedList<T>::LinkedList(const LinkedList<T> & ListToBeCopied)
{
	std::cout << "Copy constructor called for @ " << this << std::endl;

	ListElement<T> *copy = ListToBeCopied._head;
	_head = new ListElement<T>(copy->getValue());
	copy = copy->getNext();

	ListElement<T> *curr = _head;

	// copy List Elements Until we reach the end of the 
	// List to be copied
	while (copy != nullptr) {
		curr->setNext(new ListElement<T>(copy->getValue()));
		copy = copy->getNext();
		curr = curr->getNext();
	}
}
ListElement<T> * LinkedList<T>::find(const T data)
{
	ListElement<T> *temp = _head;
	if (temp == nullptr) return temp;

	while (temp != nullptr && temp->getValue() != data) {
		temp = temp->getNext();
	}
	return temp;
}
void LinkedList<T>::display()
{
	if (_head == nullptr) return;
	ListElement<T> *temp = _head;
	while (temp != nullptr) {
		std::cout << temp->getValue();
		temp = temp->getNext();
		if (temp != nullptr) {
			std::cout << "->";
		}
	}
	std::cout << std::endl << std::endl;
}
Example #5
0
void List::revertNew()
{
	ListElement *previous = new ListElement(mHead->getValue());
	// Sets new Tail
	mTail = previous;
	ListElement *step = mHead->getNext();
	while (step) {
		ListElement *clone = new ListElement(step->getValue());
		clone->setNext(previous);
		previous = clone;
		step = step->getNext();
	}
	// Head of reverted List now in previous
	while (mHead) {
		step = mHead;
		mHead = mHead->getNext();
		delete step;
	}
	mHead = previous;
}
int SLLT::testLinkedList2()
{
	using namespace std;
	{
		LinkedList<char> LL1(new ListElement<char>('a'));
		LL1.insertAtEnd('b');
		LL1.insertAtEnd('c');
		LL1.insertAtEnd('d');
		LL1.insertAtEnd('e');
		cout << "L1 is empty :: " << LL1.isEmpty() << "L1 size : " << LL1.getSize() << endl;
		LL1.display();
		LL1.insertAtHead('f');
		cout << "After insert 10 at head L1 size : " << LL1.getSize() << endl;
		LL1.display();

		LL1.insertAtEnd('g');
		cout << "After insert at end L1 size : " << LL1.getSize() << endl;
		LL1.display();

		LL1.deleteElement(new ListElement<char>('c'));
		cout << "after delete 6 L1 size : " << LL1.getSize() << endl;
		LL1.display();

		ListElement<char> *le = LL1.find('c');
		if (le) {
			cout << "L1 find :" << le->getValue() << endl;
		}
		else {
			cout << "not found " << endl;
		}

		LL1.recursiveReverseList();
		cout << "L1 after recursive reverse : size:  " << LL1.getSize() << endl;
		LL1.display();

		LL1.reverseList();
		cout << "L1 after  reverse : size:  " << LL1.getSize() << endl;
		LL1.display();

		LinkedList<char> LL2(new ListElement<char>('c'));
		LL2.insertAtEnd('a');
		LL2.insertAtEnd('b');

		cout << "L2 size : " << LL2.getSize() << endl;
		//LinkedList<T><int> LL = LL1.sumLists(LL1, LL2);
		LL2.display();
		LL1 = LL2;

		cout << "are list same: " << LinkedList<char>::compareList(&LL1, &LL2) << endl;
		cout << "L1 size : " << LL1.getSize() << endl;
		cout << "L2 size : " << LL1.getSize() << endl;
		LL1.display();
		LL2.display();

		LinkedList<char> LL3 = LL2;
		LL3.display();
		cout << "are LL2 & LL3 same: " << LinkedList<char>::compareList(&LL2, &LL3) << endl;

		LL3.reverseList();
		cout << "L3 after reverse : size:  " << LL3.getSize() << endl;
		LL3.display();

	}
	getchar();

	return 0;
}