Exemple #1
0
void test3()
{
	LinkList list;
	list.PushBack(1);
	list.PushBack(2);
	list.PushBack(3);
	list.PushBack(4);
	cout << list << endl;
	list.Insert(list.Find(1), 2);
	cout << list << endl;
	list.Insert(list.Find(4), 5);
	cout << list << endl;
}
int main()
{
   int a[] = {1,9,3,5,19,38,29,2,4,25,61,-1,7};
   LinkList<int>* list = new LinkList<int>(a, 13);
   list->PrintLinkList();
   int listlength = list->ListLength();
   std::cout<<"the list length is"<<listlength<<std::endl;
   int theLastElement = list->Get(13);
   assert(theLastElement == 7);
   std::cout<<"the last  element value   is"<< theLastElement << std::endl;
   std::cout<<"the values  that  the  array  contained currently display"<<std::endl;
   list->PrintLinkList();
   std::cout<<std::endl;
   std::cout<<"delete the  eigth element 2";
   std::cout<<std::endl;
   list->Delete(8);
   std::cout<<"the values  that  the  array  contained currently display"<<std::endl;
   std::cout<<"the  position of 61 is";
   std::cout<<std::endl;
   int position = list->Locate(61);
   std::cout<<std::endl;
   std::cout<<position<<std::endl;
   std::cout<<"the values  that  the  array  contained currently display"<<std::endl;
   list->PrintLinkList();
   std::cout<<std::endl;
    std::cout<<"now we add a value  at first  position"<<std::endl;
   list->Insert(1, 11111);
   std::cout<<"the values  that  the  array  contained currently display"<<std::endl;
   list->PrintLinkList();
   std::cout<<std::endl;
   list->Invert();
   std::cout<<"the  invert  array list is"<<std::endl;
   list->PrintLinkList();
}
Exemple #3
0
int main() {

    enum HUNG_STATE { PLATFORM, HEAD, BODY, LEFT_ARM, RIGHT_ARM, LEFT_LEG, RIGHT_LEG, HUNG };
    HUNG_STATE hanged = PLATFORM;

    LinkList word;
    char raw_word[255] = "Antelope"; //CHANGE WORD HERE

    for (int i = 0; i < strlen(raw_word); i++) {
        char c = raw_word[i];
        Slot letter(c);
        word.Insert(letter);
        }

    LinkList pickedLetters;

    cout << "Welcome to Hangman!\n" << endl;

    while (hanged != HUNG) { // While the player is not yet 'hanged'
        cout << "The word is now:\n";
        word.Print(); //Print the word; letters will be displayed as chars or blanks

        cout << "The letters you have guessed are:\n";
        pickedLetters.Print();

        cout << "Please enter a letter to guess:" << endl;
        char guess = getchar(); // Take user input for next letter to guess

        pickedLetters.Insert(guess); // Store the entered letter as being guessed

        if (word.RevealLetters()) { //If letters are revealed...
        	cout << "Good guess!\n";
        	}
        else { //If no letters are revealed...
        	++hanged; //Increase hanged status
        	if (hanged != HUNG) { // If the player is not yet 'hanged'...
        		cout << "Try again!\n";
        		}
        	else { // The player is hanged!
        		cout << "Sorry, you've lost!\n";
        		}
        	}
        }

    }
int main()
{
	LinkList<int> intlinklist;
	for (int i = 0; i < 5; i++)
	{
		intlinklist.Insert(i, i);
	}
	intlinklist.PrintList();
	system("pause");
}
int main()
{
    LinkList<int> ll;
    cout << "Created a link list..." << endl;
    cout << "length = " << ll.Count() << endl;

    for (int i = 1; i <= 10; i++)
    {
        int val = i * 2;
        ll.Insert(val, ll.Count() + 1);
        cout << val << " inserted. length = " << ll.Count() << endl;
    }
    Display(ll);

    cout << "Reversing ..." << endl;
    ll.Reverse();
    Display(ll);

    cout << "Reversing again ..." << endl;
    ll.Reverse();
    Display(ll);

    int idx = 1;
    int val = ll.Get(idx);
    cout << "Get(" << idx << ") = " << val << "." << endl;
    idx = ll.Locate(val);
    cout << "Locate(" << val << ") = " << idx << "." << endl;
    if (idx == ll.Locate(val))
    {
        cout << val << " is at " << idx << "." << endl;
    }

    idx = 7;
    val = ll.Get(idx);
    if (idx == ll.Locate(val))
    {
        cout << val << " is at " << idx << "." << endl;
    }

    idx = ll.Count();
    val = ll.Get(idx);
    if (idx == ll.Locate(val))
    {
        cout << val << " is at " << idx << "." << endl;
    }

    val = 9999999;
    if (-1 == ll.Locate(val))
    {
        cout << "Unable to locate " << val << "." << endl;
    }

    cout << "Inserting 3 at 3 ..." << endl;
    ll.Insert(3, 3);
    Display(ll);
    cout << "Deleting 3 ..." << endl;
    ll.Delete(3);
    Display(ll);

    cout << "Inserting 0 at 1 ..." << endl;
    ll.Insert(0, 1);
    Display(ll);
    cout << "Deleting 0 ..." << endl;
    ll.Delete(1);
    Display(ll);

    cout << "Inserting 11 at 12 ..." << endl;
    ll.Insert(11, 11);
    Display(ll);
    cout << "Deleting 11 ..." << endl;
    ll.Delete(11);
    Display(ll);
    try
    {
        cout << "call LinkList<int> lla = ll ..." << endl;
        LinkList<int> lla = ll;
        cout << "lla.length = " << lla.Count() << endl;

        cout << "call LinkList<int> llb(ll) ..." << endl;
        LinkList<int> llb(ll);
        cout << "llb.length = " << llb.Count() << endl;
        llb.Insert(77, 7);
        cout << "llb.length = " << llb.Count() << endl;

        cout << "call llb = lla ..." << endl;
        llb = lla;
        cout << "llb.length = " << llb.Count() << endl;
        cout << "lla.length = " << lla.Count() << endl;

        cout << "Splitting llb ..." << endl;
        llb.Insert(88, 8);
        cout << "llb.length = " << llb.Count() << endl;
        LinkList<int> llb2 = llb.Split();
        Display(llb);
        Display(llb2);

        cout << "Splitting ll ..." << endl;
        LinkList<int> llc = ll.Split();
        Display(ll);
        Display(llc);

        cout << "Sorting llc ..." << endl;
        llc.Insert(33, 6);
        llc.Insert(11, 1);
        llc.Insert(20, 2);
        llc.Insert(17, 3);
        llc.Insert(1, 6);
        Display(llc);
        llc.InsertSort();
        Display(llc);

        cout << "Intersecting on llb and llc ..." << endl;
        Display(llb);
        Display(llc);
        LinkList<int> lld = llb.Intersect(llc);
        Display(lld);

        cout << "Merging llc into ll ..." << endl;
        ll.Merge(llc);
        Display(ll);
        Display(llc);
    }
    catch (const char *&ex)
    {
        cout << "Error: " << ex << endl;
    }

    cout << "End." << endl;
    return 0;
}
Exemple #6
0
void Graph::GetPath(Graph_Node* StartNode, Graph_Node* EndNode, LinkList<Graph_Node* > *Path, bool Advanced)
{
	LinkList<PathNode*> OpenList;
	LinkList<PathNode*> ClosedList;
	bool Found = false;								
	/**< Create our first Node on OpenList made From the start Node */
	OpenList.Push_Front(new PathNode(StartNode,0,0,(StartNode->GetPosition() - EndNode->GetPosition()).length()));	
	/**< If we haven't found our goal Node yet */
	while(!Found)
	{
		/**< If there are no more Nodes to expand from then there is no way to get to the goal Node so exit the Method */
		if(OpenList.GetSize() == 0)
		{
			return;
		}													
		/**< Get and remove first node on the OpenList and push onto ClosedList */
		PathNode* Currentnode = OpenList.Pop_Front();
		ClosedList.Push_Front(Currentnode);																
		/**< If Current Node is the goal Node then we have found the Goal Node so exit loop */
		if(Currentnode->mNode == EndNode)
		{
			Found = true;
		}							
		/**< Iterate through each Edge in the Node creating a new PathNode push on the OpenList */
		for(int i = 0;i < Currentnode->mNode->GetEdgeCount();i++)
		{
			float cost, Heuristic;
			/**< The current Node being pushed on the OpenList */
			Graph_Node* node = Currentnode->mNode->GetTo(i);										
			/**< Create new PathNode if Node isn't used or Edge to Node isn't hidden */
			if(!node->GetUsed() && !Currentnode->mNode->GetHidden(i))
			{
				/**< If advanced search get advanced cost and Heuristic*/
				if(!Advanced)
				{
					cost = Currentnode->mKnownCost + (Currentnode->mNode->GetWeight(i));												/**< Add up how much it took to get to this Node */
					Heuristic = (node->GetPosition() - EndNode->GetPosition()).length();												/**< Guess how much it would take to get to the Goal Node */
				}/**< Else  get normal cost and Heuristic*/
				else
				{
					cost = Currentnode->mKnownCost + (Currentnode->mNode->GetWeight(i) * Currentnode->mNode->GetAdvancedWeight(i));	/**< Add up how much it took to get to this Node */
					Heuristic = (node->GetPosition() - EndNode->GetPosition()).length();												/**< Guess how much it would take to get to the Goal Node */
				}
				PathNode* newnode = new PathNode(node,Currentnode,cost, cost + Heuristic);												/**< Create New Path Node */
				int iter = 0;
				/**< Get the position in the OpenList that has a bigger cost than the new Nodes cost. */
				/**< Push new node infront of that position */
				if(OpenList.GetSize() != 0)
				{
					while(newnode->mGuess > OpenList.At(iter)->mGuess && iter != OpenList.GetSize())
					{
						iter++;
					}
				}
				/**< Set the Edge we just Used To Used */
				/**< Insert the New Node into the OpenList */
				node->SetUsed(true);																	
				OpenList.Insert(newnode,iter);															
			}
		}
	}
	/**< Reset all Nodes in the OpenList */
	for(ListIterator<PathNode*> iter(&OpenList); !iter.IsNuLL();iter++)
	{											
		iter.Value()->mNode->SetUsed(false);
	}
	/**< Reset all Nodes in the ClosedList */
	for(ListIterator<PathNode*> iter(&ClosedList); !iter.IsNuLL();iter++)
	{												
		iter.Value()->mNode->SetUsed(false);
	}
	/**< Set the first Node on the ClosedList  */
	/**< (Which is the last Node added so will be the Goal Node) */
	/**< to the Node we will use to iterate through the Closed List */
	PathNode* CreatePathNode = ClosedList.At(0);
	/**< While the PathNode isnt 0. */
	/**< (The start Node was added to the ClosedList, with a previous that is set to 0) */
	/**< Add the GraphNode to the Path then set the created PathNode to the previous Node */
	/**<(Which is the PathNode Used to expand The current Node) */
	while(CreatePathNode != 0)
	{
		Path->Push_Front(CreatePathNode->mNode);
		CreatePathNode = CreatePathNode->mPrevious;
	}
}