Esempio n. 1
0
        private static void DemoLinkedListFeatures()
        {
            //in this library, LinkedList is a generic
            //you can make a list of any type by <namingIt>
            //here we're just making the original list, based on the array of words.
            string[]            words    = { "the", "actor", "jumped", "over", "the", "director" };
            LinkedList <string> sentence = new LinkedList <string>(words);

            Display(sentence, "The linked list values:");

            //now we're just adding something as First
            sentence.AddFirst("today");
            Display(sentence, "Test 1: Add 'today' to beginning of the list:");

            //move a node from First to Last
            //do it by creating a temporary node that "copies" First
            //then delete First, then add its copy as Last.
            LinkedListNode <string> mark1 = sentence.First;

            sentence.RemoveFirst();
            sentence.AddLast(mark1);
            Display(sentence, "Test 2: Move first node to be last node:");

            //this just changes the value of the last node
            //by deleting the old last, and adding a new last.
            sentence.RemoveLast();
            sentence.AddLast("yesterday");
            Display(sentence, "Test 3: Change the last node to 'yesterday':");

            //move the last node to first
            //again, by using a temporary placeholder
            mark1 = sentence.Last;
            sentence.RemoveLast();
            sentence.AddFirst(mark1);
            Display(sentence, "Test 4: Move last node to be first node:");

            //look for last occurance of a certain word (the)
            sentence.RemoveFirst();
            LinkedListNode <string> current = sentence.FindLast("the");

            IndicateNode(current, "Test 5: Indicate last occurence of 'the':");

            //add some words where we say.
            sentence.AddAfter(current, "old");
            sentence.AddAfter(current, "lazy");
            IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':");

            //sets "actor" as the node.
            current = sentence.Find("actor");
            IndicateNode(current, "Test 7: Indicate the 'actor' node:");

            //add words before the current node (actor)
            sentence.AddBefore(current, "quick");
            sentence.AddBefore(current, "skinny");
            IndicateNode(current, "Test 8: Add 'quick' and 'skinny' before 'actor':");


            Console.ReadLine();
        }
Esempio n. 2
0
        private static void DemoLinkedListFeatures()
        {
            string[]            words    = { "the", "actor", "jumped", "over", "the", "director" };
            LinkedList <string> sentence = new LinkedList <string>(words);

            Display(sentence, "The linked list values:");


            sentence.AddFirst("today");
            Display(sentence, "Test 1: Add 'today' to beginning of the list:");


            LinkedListNode <string> mark1 = sentence.First;

            sentence.RemoveFirst();
            sentence.AddLast(mark1);
            Display(sentence, "Test 2: Move first node to be last node:");

            sentence.RemoveLast();
            sentence.AddLast("yesterday");
            Display(sentence, "Test 3: Change the last node to 'yesterday':");

            mark1 = sentence.Last;
            sentence.RemoveLast();
            sentence.AddFirst(mark1);
            Display(sentence, "Test 4: Move last node to be first node:");


            sentence.RemoveFirst();
            LinkedListNode <string> current = sentence.FindLast("the");

            IndicateNode(current, "Test 5: Indicate last occurence of 'the':");

            sentence.AddAfter(current, "old");
            sentence.AddAfter(current, "lazy");
            IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':");

            current = sentence.Find("actor");
            IndicateNode(current, "Test 7: Indicate the 'actor' node:");

            sentence.AddBefore(current, "quick");
            sentence.AddBefore(current, "skinny");
            IndicateNode(current, "Test 8: Add 'quick' and 'skinny' before 'actor':");


            Console.ReadLine();
        }