예제 #1
0
        public static void Main()
        {
            MyLinkedList <string> myList = new MyLinkedList <string>();

            myList.AddLast("Milko");
            myList.AddLast("Ilio");
            myList.AddFirst("Koko");
            myList.RemoveFirst();
            myList.RemoveLast();
            myList.RemoveFirst();

            // Next row will throw an exception if uncomment
            //myList.RemoveLast();
        }
예제 #2
0
        static void Main()
        {
            MyLinkedList mylist = new MyLinkedList(0);

            mylist.Append(1);
            mylist.Append(2);
            mylist.Append(3);
            mylist.Append(4);
            mylist.Append(5);
            mylist.Length();
            mylist.PrintAll();
            mylist.Reverse();
            mylist.PrintAll();
            Console.ReadKey();
        }
예제 #3
0
        static void Main(string[] args)
        {
            MyLinkedList <char> myLinkedList = new MyLinkedList <char>();

            myLinkedList.AddFirst('a');
            myLinkedList.AddFirst('b');
            myLinkedList.AddFirst('c');

            MyNode <char> a       = myLinkedList.First;
            MyNode <char> current = a;

            while (a != null)
            {
                Console.WriteLine(current.Value);
                current = current.Next;
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            var LinkedList = new MyLinkedList <string>();

            LinkedList.Add("Ola");
            LinkedList.Add("ma");
            LinkedList.Add("Psa");
            LinkedList.Add("i");
            LinkedList.Add("Kota");
            foreach (var item in LinkedList.AsList())
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(LinkedList.GetSize());
            LinkedList.GetByIndex(0);
            Console.ReadLine();
        }
예제 #5
0
        public void sort(IComparer <T> comparer)
        {
            //будем делегировать запрос на сортировку классу System.Array
            T[] array      = new T[count];
            int arrayIndex = 0;

            this.CopyTo(array, arrayIndex);
            Array.Sort(array, comparer);
            MyLinkedList <T>     tmp         = new MyLinkedList <T>(array);
            MyLinkedListNode <T> current     = this.Head;
            MyLinkedListNode <T> current_tmp = tmp.Head;

            while (current != null)
            {
                current.Value = current_tmp.Value;
                current       = current.Next;
                current_tmp   = current_tmp.Next;
            }
        }
예제 #6
0
        static void Main(string[] args)
        {
            MyLinkedList <int> linkedList = new MyLinkedList <int>();

            linkedList.Add(4);
            linkedList.Add(3);
            linkedList.Add(5);
            linkedList.Add(1);
            foreach (var item in linkedList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("::::::::::::::::::::");
            linkedList.SetOnFirst();
            linkedList.Add(2);
            foreach (var item in linkedList)
            {
                Console.WriteLine(item);
            }
        }
예제 #7
0
        static void Main(string[] args)
        {
            var linkedList = new MyLinkedList();

            linkedList.AddLast(10);
            linkedList.AddLast(20);
            linkedList.AddLast(30);
            linkedList.AddFirst(5);
            linkedList.AddFirst(1);

            Console.WriteLine(linkedList.IndexOf(30));
            Console.WriteLine(linkedList.Contains(1));
            Console.WriteLine(linkedList.GetSize());

            Console.WriteLine(linkedList.GetSize());
            Console.WriteLine(String.Join(',', linkedList.ToArray()));
            linkedList.Reverse();
            Console.WriteLine(String.Join(',', linkedList.ToArray()));
            Console.WriteLine(linkedList.GetKthFromTheEnd(3));

            linkedList.PrintMiddle();
        }
예제 #8
0
        int IComparable.CompareTo(object obj)
        {
            MyLinkedList <T> temp = obj as MyLinkedList <T>;

            if (temp != null)
            {
                if (this.Count > temp.Count)
                {
                    return(1);
                }
                if (this.Count < temp.Count)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            }
            else
            {
                throw new ArgumentException("Argument isn't a MyLinkedList");
            }
        }
예제 #9
0
        static void Main(string[] args)
        {
            string[]              words   = { "the", "fox", "jumps", "over", "the", "dog" };
            LinkedList <string>   list    = new LinkedList <string>(words);
            MyLinkedList <string> my_list = new MyLinkedList <string>(words);

            Test(list);
            Test(my_list);

            int[] BigArrOfInt = new int[10000];
            for (int i = 0; i < 10000; i++)
            {
                BigArrOfInt[i] = i;
            }
            Console.WriteLine("Производительность LinkedList: ");
            Stopwatch performance = new Stopwatch();

            performance.Start();
            LinkedList <int> new_list = new LinkedList <int>(BigArrOfInt);

            for (int i = 0; i < 10000; i++)
            {
                if (i % 3 == 0)
                {
                    new_list.Remove(i);
                }
            }
            for (int i = 0; i < 1000; i++)
            {
                new_list.AddLast(i * i * i);
            }
            performance.Stop();
            Console.WriteLine("{0} in ms", performance.ElapsedMilliseconds);
            performance.Reset();

            Console.WriteLine("Производительность MyLinkedList: ");
            performance.Start();
            LinkedList <int> new_my_list = new LinkedList <int>(BigArrOfInt);

            for (int i = 0; i < 10000; i++)
            {
                if (i % 3 == 0)
                {
                    new_my_list.Remove(i);
                }
            }
            for (int i = 0; i < 1000; i++)
            {
                new_my_list.AddLast(i * i * i);
            }
            performance.Stop();
            Console.WriteLine("{0} in ms", performance.ElapsedMilliseconds);
            Console.WriteLine();
            Console.WriteLine("Test collection LinkedList: ");
            TestCollection(new LinkedList <string>());
            Console.WriteLine();
            Console.WriteLine("Test collection MyLinkedList: ");
            TestCollection(new MyLinkedList <string>());
            Console.WriteLine();
            Console.WriteLine("test IComporable in MyLinkedList");
            int[] arr0 = { 1, 3, 5 };
            int[] arr1 = { 1 };
            int[] arr2 = { 4, 8 };
            MyLinkedList <int> list0 = new MyLinkedList <int>(arr0);
            MyLinkedList <int> list1 = new MyLinkedList <int>(arr1);
            MyLinkedList <int> list2 = new MyLinkedList <int>(arr2);

            MyLinkedList <int>[] arr = { list0, list1, list2 };
            Console.WriteLine("Current list: ");
            for (int i = 0; i < 3; i++)
            {
                Console.Write("{0}) ", i + 1);
                MyLinkedList <int> tmp = new MyLinkedList <int>(arr[i]);

                foreach (var item in tmp)
                {
                    Console.Write("{0} ", item);
                }
                Console.WriteLine();
            }
            Console.WriteLine("Sorted List (По числу элементов в списке) : ");
            Array.Sort(arr);
            for (int i = 0; i < 3; i++)
            {
                Console.Write("{0}) ", i + 1);
                MyLinkedList <int> tmp = new MyLinkedList <int>(arr[i]);

                foreach (var item in tmp)
                {
                    Console.Write("{0} ", item);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine("Test IClonable and Sort() in MyLinkedList");
            int[] nums = { 7, 5, 9, 15, 11, 4, 24, 1, 8 };
            MyLinkedList <int> my_link_list          = new MyLinkedList <int>(nums);
            MyLinkedList <int> clone_of_my_link_list = (MyLinkedList <int>)my_link_list.Clone();

            Console.Write("List: ");
            foreach (var i in my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.Write("Cloned List: ");
            foreach (var i in clone_of_my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Change Cloned List (Remove 5 and 11) :");
            clone_of_my_link_list.Remove(5);
            clone_of_my_link_list.Remove(11);
            Console.Write("List: ");
            foreach (var i in my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.Write("Cloned List: ");
            foreach (var i in clone_of_my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Now we will sort() our original list : ");
            my_link_list.sort(new IntComparer());
            Console.Write("List: ");
            foreach (var i in my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.Write("Cloned List: ");
            foreach (var i in clone_of_my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.ReadLine();
        }
예제 #10
0
        public static void Test(MyLinkedList <string> sentence)
        {
            // Create the link list.
            Display(sentence, "MyLinkedList: ");
            Console.WriteLine("sentence.Contains(\"jumps\") = {0}",
                              sentence.Contains("jumps"));

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

            // Move the first node to be the last node.
            MyLinkedListNode <string> mark1 = sentence.Head;

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

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

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

            // Indicate the last occurence of 'the'.
            sentence.RemoveFirst();
            MyLinkedListNode <string> current = sentence.FindLast("the");

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

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

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

            // Add 'quick' and 'brown' before 'fox':
            sentence.AddBefore(current, "quick");
            sentence.AddBefore(current, "brown");
            IndicateNode(sentence, current, "Test 8: Add 'quick' and 'brown' before 'fox':");

            // Keep a reference to the current node, 'fox',
            // and to the previous node in the list. Indicate the 'dog' node.
            mark1 = current;
            MyLinkedListNode <string> mark2 = current.Previous;

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

            // The AddBefore method throws an InvalidOperationException
            // if you try to add a node that already belongs to a list.

            Console.WriteLine("Test 10: Throw exception by adding node (fox) already in the list:");
            try
            {
                sentence.AddBefore(current, mark1);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine("Exception message: {0}", ex.Message);
            }
            Console.WriteLine();

            // Remove the node referred to by mark1, and then add it
            // before the node referred to by current.
            // Indicate the node referred to by current.
            sentence.Remove(mark1);
            sentence.AddBefore(current, mark1);
            IndicateNode(sentence, current, "Test 11: Move a referenced node (fox) before the current node (dog):");

            // Remove the node referred to by current.
            sentence.Remove(current);
            IndicateNode(sentence, current, "Test 12: Remove current node (dog) and attempt to indicate it:");

            // Add the node after the node referred to by mark2.
            sentence.AddAfter(mark2, current);
            IndicateNode(sentence, current, "Test 13: Add node removed in test 11 after a referenced node (brown):");

            // The Remove method finds and removes the
            // first node that that has the specified value.
            sentence.Remove("old");
            Display(sentence, "Test 14: Remove node that has the value 'old':");

            // When the linked list is cast to ICollection(Of String),
            // the Add method adds a node to the end of the list.
            sentence.RemoveLast();
            ICollection <string> icoll = sentence;

            icoll.Add("rhinoceros");
            Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':");

            Console.WriteLine("Test 16: Copy the list to an array:");
            // Create an array with the same number of
            // elements as the inked list.
            string[] sArray = new string[sentence.Count];
            sentence.CopyTo(sArray, 0);

            foreach (string s in sArray)
            {
                Console.WriteLine(s);
            }

            // Release all the nodes.
            sentence.Clear();

            Console.WriteLine();
            Console.WriteLine("Test 17: Clear linked list. Contains 'jumps' = {0}",
                              sentence.Contains("jumps"));

            Console.WriteLine();
        }
예제 #11
0
        static void Main(string[] args)
        {
            MyLinkedList <int>    myLLInt       = new MyLinkedList <int>();
            MyLinkedList <Person> myLLPerson    = new MyLinkedList <Person>();
            MyStack <Person>      myStackPerson = new MyStack <Person>();
            Person vasya = new Person(12345, 32, "Vasya", "Netanya");
            Person moshe = new Person(54321, 29, "Moshe", "Lod");
            Person helen = new Person(32323, 43, "Helen", "Rehovot");
            Person shir  = new Person(67895, 16, "Shir", "Netanya");

            Person[] arrayPersons = new Person[4] {
                vasya, moshe, helen, shir
            };
            Console.WriteLine();
            Console.WriteLine("----------------------MyLinkedList<int> verification -----------------------");
            Console.WriteLine("----------- AddTail, AddHead, AddAfter, AddBefore verifications-----------");
            myLLInt.AddTail(25);
            myLLInt.AddHead(20);
            myLLInt.AddHead(15);
            myLLInt.AddAfter(myLLInt.Tail, 30);
            myLLInt.AddAfter(myLLInt.Tail.Prev, 27);
            myLLInt.AddBefore(myLLInt.Head, 10);
            myLLInt.AddAfter(myLLInt.Head.Next, 17);

            Console.WriteLine(myLLInt.ToString());
            Console.WriteLine();
            Console.WriteLine("---------------Contains, Find verifications -------------");
            Node <int> node = myLLInt.Head;

            for (int i = 0; i < myLLInt.Count; i++)
            {
                Console.WriteLine("Contains " + node.Data + " verification: " + myLLInt.Contains(node.Data));
                Console.WriteLine("Find " + node.Data + " verification: " + myLLInt.Find(node.Data).Equals(node));
                node = node.Next;
            }
            Console.WriteLine();
            Console.WriteLine("----------------- Remove verification ---------------------");
            myLLInt.RemoveHead();
            myLLInt.RemoveTail();
            myLLInt.Remove(myLLInt.Head.Next);
            myLLInt.Remove(myLLInt.Tail.Prev);
            Console.WriteLine(myLLInt);
            Console.WriteLine();
            Console.WriteLine("----------------- CopyTo verification --------------------");
            int[] arr = new int[7];
            myLLInt.CopyTo(arr, 2);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.WriteLine();
            Console.WriteLine("---------------------foreach--LinkedList<Person>-----------------");

            myLLPerson.AddHead(vasya);
            myLLPerson.AddTail(moshe);
            myLLPerson.AddTail(helen);

            foreach (Person prs in myLLPerson)
            {
                Console.WriteLine(prs);
            }
            Console.WriteLine();
            Console.WriteLine("---------------------foreach--LinkedList<int>-----------------");
            foreach (int num in myLLInt)
            {
                Console.WriteLine(num);
            }
            Console.WriteLine();
            Console.WriteLine("--------------------- Stack Verification<Person>----------------");

            myStackPerson = fillMyStack(arrayPersons, myStackPerson);
            Console.WriteLine();
            Console.WriteLine("----------------------Peek() Pop() Verification-----------------");

            Console.WriteLine();
            int iterations = myStackPerson.Count;

            for (int i = 0; i < iterations; i++)
            {
                Console.WriteLine("Person: " + myStackPerson.Peek() + " : "
                                  + myStackPerson.Peek().Equals(myStackPerson.Pop()));
            }
            Console.WriteLine();
            Console.WriteLine("------------------------Capacity, Constructor with IEnumerable, foreach-----------------");
            myStackPerson = new MyStack <Person>(arrayPersons);
            myStackPerson = fillMyStack(arrayPersons, myStackPerson);
            myStackPerson = fillMyStack(arrayPersons, myStackPerson);

            Console.WriteLine("Count: " + myStackPerson.Count);
            foreach (Person person in myStackPerson)
            {
                Console.WriteLine(person);
            }

            Console.WriteLine();
            Console.WriteLine("------------------------Clear()-----------------------------");
            myStackPerson.Clear();
            foreach (Person person in myStackPerson)
            {
                Console.WriteLine(person);
            }
            myStackPerson.Push(vasya);
            myStackPerson.Push(moshe);
            foreach (Person person in myStackPerson)
            {
                Console.WriteLine(person);
            }



            /*Console.Writeln("----------------TrimExcees verification--------------");
             * Console.WriteLine("Capacity: " + myStackPerson.Capacity);
             * myStackPerson.TrimExcees();
             * Console.WriteLine("Capacity: " + myStackPerson.Capacity);*/
            //Console.WriteLine(myStackPerson.Peek());

            Console.WriteLine("----------------------Queue: Constructors verification-----------------------------------------------");
            Console.WriteLine();

            MyQueue <Person> myQ = new MyQueue <Person>(arrayPersons);

            foreach (Person prs in myQ)
            {
                Console.WriteLine(prs);
            }
            Console.WriteLine("MyQ - Count: " + myQ.Count + " Capacity: " + myQ.Capacity);
            MyQueue <Person> myQ2 = new MyQueue <Person>(3);
            MyQueue <int>    myQ3 = new MyQueue <int>();

            Console.WriteLine("MyQ2 - Count: " + myQ2.Count + " Capacity: " + myQ2.Capacity);
            Console.WriteLine("MyQ3 - Count: " + myQ3.Count + " Capacity: " + myQ3.Capacity);

            Console.WriteLine();
            Console.WriteLine("----------------------Queue: Enqueue, dequeue, peek, increasing capacity, treemExcees verification----------");
            Console.WriteLine();

            Console.WriteLine("---------MyQ2--------");
            Console.WriteLine("MyQ2 - Count: " + myQ2.Count + " Capacity: " + myQ2.Capacity);
            foreach (Person prs in arrayPersons)
            {
                myQ2.Enqueue(prs);
            }
            foreach (Person prs in myQ2)
            {
                Console.WriteLine(prs);
            }
            Console.WriteLine("MyQ2 - Count: " + myQ2.Count + " Capacity: " + myQ2.Capacity);
            Console.WriteLine("---------MyQ--------");
            Console.WriteLine("MyQ - Count: " + myQ.Count + " Capacity: " + myQ.Capacity);
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("Iteration#" + i);
                Console.WriteLine("Peek/Enqueue/Dequeue: " + myQ.Peek());
                myQ.Enqueue(myQ.Dequeue());
                foreach (Person prs in myQ)
                {
                    Console.WriteLine(prs);
                }
                Console.WriteLine("MyQ - Count: " + myQ.Count + " Capacity: " + myQ.Capacity);
                Console.WriteLine();
            }
            Console.WriteLine("Vasya was added, capacity has to be changed");
            myQ.Enqueue(vasya);
            Console.WriteLine("MyQ - Count: " + myQ.Count + " Capacity: " + myQ.Capacity);
            myQ.TrimExcees();
            Console.WriteLine("TreemExcees -> MyQ - Count: " + myQ.Count + " Capacity: " + myQ.Capacity);
            Console.WriteLine();
            Console.WriteLine("----------------------Queue: Clear verification----------");
            Console.WriteLine();
            Console.WriteLine("MyQ - Count: " + myQ.Count + " Capacity: " + myQ.Capacity);
            foreach (Person prs in myQ)
            {
                Console.WriteLine(prs);
            }
            myQ.Clear();
            Console.WriteLine("MyQ - Count: " + myQ.Count + " Capacity: " + myQ.Capacity);
            foreach (Person prs in myQ)
            {
                Console.WriteLine(prs);
            }


            Console.ReadKey();
        }
예제 #12
0
 public ListEnum(MyLinkedList <T> list)
 {
     this.list = list;
     position  = -1;
 }