예제 #1
0
std::set<const LLNode*> LinkedList::GetAllNodes()
{
	std::set<const LLNode*> ret;
	for(LLNode *cur = mHead;cur;cur = cur->GetNext())
	{
		ret.insert(cur);
	}
	return ret;
}
예제 #2
0
	LLNode* dequeue(){
		LLNode* temp_front = front;

		front = front->getNext();

		if(front == back)
			back = back->getNext();

		return temp_front;
	}
예제 #3
0
	void display(){
		LLNode* temp = front;

		while(temp){
			std::cout << temp->getData() << " ";
			temp = temp->getNext();
		}
		
		std::cout << std::endl;
	}
예제 #4
0
int main() {
	LLNode n1(3);
	n1.next = new LLNode(1);
	n1.next->next = new LLNode(5);

	LLNode n2(8);
	n2.next = new LLNode(9);
	n2.next->next=new LLNode(4);

	LLNode *res = ll_add(&n1,&n2,0);
	res->traverse_from();
}
		void testRemove(ostream & os){
			printSubheader("REMOVE",os);
			LinkedList<string> ll;
			ll.Insert("Muh!",NULL);
			ll.Insert("Buh!",NULL);
			ll.Insert("Juh!",NULL);
			
			LLNode<string>* node = ll.Find("Muh!",NULL);
			ll.Remove(node);
			node = ll.GetFirst();
			TESTM(node->GetValue() == "Juh!","Expected: Juh!\nActual: " + node->GetValue());
			TESTM(node->GetNext()->GetValue() == "Buh!","Expected: Buh!\nActual: " + node->GetNext()->GetValue());
		}
예제 #6
0
	void enqueue(int d){
		LLNode* entry = new LLNode(d);

		if(front == NULL)
			front = entry;
		else
			back->setNext(entry);

		back = entry;
	}
int main(int argc, char const *argv[])
{
    LinkedList<int> ll;
    for (int i = 0; i < 20; i++) {
        ll.insert(i);
    }
    ll.printAll();
    ll._delete(19);
    ll._delete(0);
    ll._delete(12);
    ll.printAll();
    LLNode<int>* temp = ll.search(12);
    if (temp) {
        cout << temp->getKey() << endl;
    } else {
        cerr << "This linked list doesn't have 12" << endl;
    }
    ll.inverse();
    ll.printAll();
    return 0;
}
		void testNodes(ostream & os){
			printSubheader("NODES",os);
			LinkedList<string> ll;
		
			TESTM(ll.GetFirst()==NULL,"ll first should be null");
			TESTM(ll.GetLast()==NULL,"ll last should be null");
			
			ll.Insert("Buh!",NULL);
			TESTM(ll.GetFirst()->GetValue().compare("Buh!")==0,"Expected: Buh!\nActual: " + 
				ll.GetFirst()->GetValue());
			TESTM(ll.GetLast()->GetValue().compare("Buh!")==0,"Expected: Buh!\nActual: " + 
				ll.GetLast()->GetValue());
			
			ll.Insert("Muh!",NULL);
			TESTM(ll.GetLast()->GetValue().compare("Buh!")==0,"Expected: Buh!\nActual: " + 
				ll.GetLast()->GetValue());
			TESTM(ll.GetFirst()->GetValue().compare("Muh!")==0,"Expected: Muh!\nActual: " + 
				ll.GetFirst()->GetValue());
				
			LLNode<string>* buh = ll.GetLast();
			TESTM(buh->GetNext() == NULL,"Expected: NULL\nActual: " + buh->GetNext()->GetValue());
			TESTM(buh->GetPrevious()->GetValue() == "Muh!","Expected: Muh!\nActual: " + buh->GetPrevious()->GetValue());
			
			LLNode<string>* muh = ll.GetFirst();
			TESTM(muh->GetNext()->GetValue() == "Buh!","Expected: Buh!\nActual: " + muh->GetNext()->GetValue());
			TESTM(muh->GetPrevious() == NULL,"Expected: NULL\nActual: " + muh->GetPrevious()->GetValue());
			
			//node comparisons
			LLNode<string>* node1 = ll.GetFirst();//Muh!
			LLNode<string>* node2 = ll.GetLast();//Buh!
			TESTM(node1->compare(*node2) > 0, "tested: " + node1->GetValue() + ".compare(" +
				node2->GetValue() + ")\nExpected positive\n");
			TESTM(node2->compare(*node1) < 0, "tested: " + node2->GetValue() + ".compare(" +
				node1->GetValue() + ")\nExpected negative\n");
			TESTM(node1->compare(*node1) == 0, "tested: " + node1->GetValue() + ".compare(" +
				node1->GetValue() + ")\nExpected zero\n");
			
		}
		void testFind(ostream & os){
			printSubheader("FIND",os);
			
			LinkedList<string> ll;
			ll.Insert("Muh!",NULL);
			ll.Insert("Buh!",NULL);
			ll.Insert("Juh!",NULL);
			ll.Insert("Buh!",NULL);
			
			LLNode<string>* node;
			node = ll.Find("Buh!",NULL);
			TESTM(node->GetPrevious() == NULL,"Expected: NULL\nActual: " + node->GetPrevious()->GetValue());
			TESTM(node->GetNext()->GetValue() == "Juh!","Expected: Juh!\nActual: " + node->GetNext()->GetValue());
			TESTM(node->GetValue() == "Buh!","Expected: Buh!\nActual: " + node->GetValue());
			
			LLNode<string>* node2 = ll.Find("Buh!",ll.GetFirst());
			if(node2 == NULL)
			TESTM(node2->GetPrevious()->GetValue() == "Juh!","Expected: Juh!\nActual: " + node2->GetPrevious()->GetValue());
			TESTM(node2->GetNext()->GetValue() == "Muh!","Expected: Muh!\nActual: " + node2->GetNext()->GetValue());
			TESTM(node2->GetValue() == "Buh!","Expected: Buh!\nActual: " + node2->GetValue());
		}
		void testConstructors(ostream & os){
			printSubheader("CONSTRUCTORS",os);
			stringstream sizeStr;
			LinkedList<string> ll;
			ll.Insert("Muh!",NULL);
			ll.Insert("Buh!",NULL);
			ll.Insert("Juh!",NULL);
			
			//test copy constructor
			LinkedList<string> ll2(ll);
			LLNode<string>* node = ll2.GetFirst();
			sizeStr << ll2.GetSize();
			TESTM(ll2.GetSize() == 3,"Expected: 3\nActual: sizeStr\n");
			TESTM(node->GetValue() == "Juh!","Expected: Juh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node->GetValue() == "Buh!","Expected: Buh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node->GetValue() == "Muh!","Expected: Muh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node == NULL,"Expected: NULL\nActual: " + node->GetValue());
			
			//test operator=
			LinkedList<string> ll3 = ll2;
			node = ll3.GetFirst();
			sizeStr << ll3.GetSize();
			TESTM(ll3.GetSize() == 3,"Expected: 3\nActual: sizeStr\n");
			TESTM(node->GetValue() == "Juh!","Expected: Juh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node->GetValue() == "Buh!","Expected: Buh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node->GetValue() == "Muh!","Expected: Muh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node == NULL,"Expected: NULL\nActual: " + node->GetValue());
		}
		void testAddAll(ostream & os){
			printSubheader("ADDALL",os);
			LinkedList<string> ll1;
			ll1.Insert("Muh!",NULL);
			ll1.Insert("Buh!",NULL);
			
			LinkedList<string> ll2;
			ll2.Insert("Juh!",NULL);
			ll2.Insert("Kuh!",NULL);
			
			ll1.addAll(&ll2);
			
			LLNode<string>* node = ll1.GetFirst();
			TESTM(node->GetValue() == "Juh!","Expected: Juh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node->GetValue() == "Kuh!","Expected: Kuh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node->GetValue() == "Buh!","Expected: Buh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node->GetValue() == "Muh!","Expected: Muh!\nActual: " + node->GetValue());
			node = node->GetNext();
			TESTM(node == NULL,"Expected: NULL\nActual: " + node->GetValue());
			
		}