Beispiel #1
0
        public int[] AddLastTest(int val, int[] arr)
        {
            DLinkedList dllist = new DLinkedList(arr);

            dllist.AddLast(val);
            return(dllist.ToArray());
        }
Beispiel #2
0
        public int[] AddAtArrTest(int idx, int[] val, int[] arr)
        {
            DLinkedList dllist = new DLinkedList(arr);

            dllist.AddAtArr(idx, val);
            return(dllist.ToArray());
        }
Beispiel #3
0
        public int[] RemoveAllTest(int val, int[] arr)
        {
            DLinkedList dllist = new DLinkedList(arr);

            dllist.RemoveAll(val);
            return(dllist.ToArray());
        }
Beispiel #4
0
        public int[] ReverseTest(int[] arr)
        {
            DLinkedList dllist = new DLinkedList(arr);

            dllist.Reverse();
            return(dllist.ToArray());
        }
Beispiel #5
0
        public int[] RemoveAtTest(int idx, int[] arr)
        {
            DLinkedList dllist = new DLinkedList(arr);

            dllist.RemoveAt(idx);
            return(dllist.ToArray());
        }
Beispiel #6
0
        public void AddAfterTestTwo()
        {
            //建立了一个节点,把节点添加到链表里面,链表名字是list
            DLinkedListNode node = new DLinkedListNode();
            DLinkedList     list = new DLinkedList(node);

            //new了一个新节点,添加到链表node节点后面
            DLinkedListNode node_1 = new DLinkedListNode();

            list.AddAfter(node_1, node);

            //new了一个新节点,添加到链表node节点后面
            DLinkedListNode newNode = new DLinkedListNode();

            list.AddAfter(newNode, node);

            //链表的头部是node
            Assert.That(list.Head, Is.EqualTo(node));
            //node节点的下一个是newNode
            Assert.That(node.Next, Is.EqualTo(newNode));

            //newNode节点的下一个是node1
            Assert.That(newNode.Previous, Is.EqualTo(node));
            Assert.That(newNode.Next, Is.EqualTo(node_1));
            Assert.That(node_1.Previous, Is.EqualTo(newNode));

            //尾巴是node1
            Assert.That(list.Tail, Is.EqualTo(node_1));
        }
        public void Print(DLinkedList mobile)//prints out the grid
        {
            Console.WriteLine("Grid:");
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    // cells.GetItem(j) gets the row, then calling get item on it again gets the individual element
                    if (cells.GetItem(j).GetItem(i).objs.Count > 0)
                    {
                        //Printing the cells that hold mobile objects.
                        Console.Write(string.Format("[{0,2}]\t", (cells.GetItem(j).GetItem(i).objs[0] as MobileObject).GetId()));
                    }
                    else
                    {
                        //if the cell does not contain an object it print an empty cell [ ], no id"
                        Console.Write("[  ]\t");
                    }
                }
                // Printing values 10 to 160 across X and Y "axis"
                Console.WriteLine(cell_size * (i + 1));
            }

            for (int j = 0; j < size; j++)
            {
                Console.Write((j + 1) * cell_size + "\t");
            }
            Console.WriteLine("");

            //Printing which object is in which cell on the grid
            foreach (MobileObject mob in mobile)
            {
                Console.WriteLine(mob.GetName() + " is in cell with id: " + mob.GetPositionCell());
            }
        }
Beispiel #8
0
        public int[] SetTest(int idx, int val, int[] arr)
        {
            DLinkedList dllist = new DLinkedList(arr);

            dllist.Set(idx, val);
            return(dllist.ToArray());
        }
Beispiel #9
0
        public void AddTest()
        {
            DLinkedList <int> l = new DLinkedList <int>();

            l.Add(1);
            l.Add(2);
        }
Beispiel #10
0
        public int[] RemoveLastTest(int[] arr)
        {
            DLinkedList dllist = new DLinkedList(arr);

            dllist.RemoveLast();
            return(dllist.ToArray());
        }
Beispiel #11
0
        public void SetByIndexTest(int[] array, int idx, int expected)
        {
            DLinkedList actualList = new DLinkedList(array);
            int         actual     = actualList[idx];

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        /// Get all outgoing weighted edges from vertex
        /// </summary>
        public virtual IEnumerable <WeightedEdge <T> > OutgoingEdges(T vertex)
        {
            if (!HasVertex(vertex))
            {
                return(null);
            }

            int source = _vertices.IndexOf(vertex);
            var list   = new DLinkedList <WeightedEdge <T> >();

            // Check existence of vertex
            if (source != -1)
            {
                for (int adjacent = 0; adjacent < _vertices.Count; ++adjacent)
                {
                    if (_vertices[adjacent] != null && _doesEdgeExist(source, adjacent))
                    {
                        var edge = new WeightedEdge <T>(
                            (T)_vertices[source],
                            (T)_vertices[adjacent],
                            _getEdgeWeight(source, adjacent));

                        list.Append(edge);
                    }
                }
            }

            return(list);
        }
Beispiel #13
0
        /// <summary>
        /// Rehash the the current collection elements to a new collection.
        /// </summary>
        private void _rehash(ref DLinkedList <TKey, TValue>[] newHashTableStore, int oldHashTableSize)
        {
            // Reset the free slots count
            this._freeSlotsCount = newHashTableStore.Length;

            for (int i = 0; i < oldHashTableSize; ++i)
            {
                var chain = _hashTableStore[i];

                if (chain != null && chain.Count > 0)
                {
                    var head = chain.Head;

                    while (head != null)
                    {
                        uint hash = _getHashOfKey(head.Key, newHashTableStore.Length);

                        if (newHashTableStore[hash] == null)
                        {
                            _freeSlotsCount--;
                            newHashTableStore[hash] = new DLinkedList <TKey, TValue>();
                        }

                        newHashTableStore[hash].Append(head.Key, head.Value);

                        head = head.Next;
                    }
                }
            }//end-for
        }
Beispiel #14
0
 void Add(DLinkedList node)
 {
     node.pre      = head;
     node.post     = head.post;
     head.post.pre = node;
     head.post     = node;
 }
Beispiel #15
0
        public void RemoveTest()
        {
            DLinkedList <int> l = new DLinkedList <int>();

            l.Add(1);
            l.Add(2);
            l.Remove(0);
            Assert.AreEqual(l[0], 2);

            l.Add(3);
            l.Add(4);
            l.Add(5);
            l.Remove(2);

            Assert.AreEqual(l[0], 2);
            Assert.AreEqual(l[1], 3);
            Assert.AreEqual(l[2], 5);

            l.Remove(1);

            Assert.AreEqual(l[0], 2);
            Assert.AreEqual(l[1], 5);

            l.Remove(1);

            Assert.AreEqual(l[0], 2);

            l.Add(3);
            l.Add(4);

            Assert.AreEqual(l[0], 2);
            Assert.AreEqual(l[1], 3);
            Assert.AreEqual(l[2], 4);
        }
Beispiel #16
0
        public void GetMaximumTest(int[] array, int expected)
        {
            DLinkedList ll = new DLinkedList(array);

            int actual = ll.GetMaximum();

            Assert.AreEqual(expected, actual);
        }
Beispiel #17
0
        public void AddByIndexTest(int[] array, int index, int value, int[] expArray)
        {
            DLinkedList expected = new DLinkedList(expArray);
            DLinkedList actual   = new DLinkedList(array);

            actual.AddByIndex(index, value);
            Assert.AreEqual(expected, actual);
        }
Beispiel #18
0
 private static void SaveDLL(DLinkedList <int> dll)
 {
     using (Stream stream = File.Open("data.bin", FileMode.Create))
     {
         BinaryFormatter bin = new BinaryFormatter();
         bin.Serialize(stream, dll);
     }
 }
Beispiel #19
0
        public void MergeDecreaseSortTest(int[] array, int[] expectedArray)
        {
            DLinkedList actual   = new DLinkedList(array);
            DLinkedList expected = new DLinkedList(expectedArray);

            actual.SortDecrease();
            Assert.AreEqual(expected, actual);
        }
Beispiel #20
0
        public void AddToBigginingTest(int[] array, int value, int[] expectedArray)
        {
            DLinkedList actual   = new DLinkedList(array);
            DLinkedList expected = new DLinkedList(expectedArray);

            actual.AddToBiginning(value);
            Assert.AreEqual(expected, actual);
        }
Beispiel #21
0
        public void DeleteFromEndTest(int[] array, int[] expectedArray)
        {
            DLinkedList actual   = new DLinkedList(array);
            DLinkedList expected = new DLinkedList(expectedArray);

            actual.DeleteFromEnd();
            Assert.AreEqual(expected, actual);
        }
Beispiel #22
0
        public void DeleteNElementsFromBiginning(int[] array, int number, int[] expectedArray)
        {
            DLinkedList actual   = new DLinkedList(array);
            DLinkedList expected = new DLinkedList(expectedArray);

            actual.DeleteFromBiginning(number);
            Assert.AreEqual(expected, actual);
        }
Beispiel #23
0
        public void AddArrayToTest(int[] array, int idx, int[] addedArray, int[] expectedArray)
        {
            DLinkedList actual   = new DLinkedList(array);
            DLinkedList expected = new DLinkedList(expectedArray);

            actual.AddTo(idx, addedArray);
            Assert.AreEqual(expected, actual);
        }
Beispiel #24
0
        public void DeleteElementsWithValueTest(int[] array, int value, int[] expectedArray)
        {
            DLinkedList actual   = new DLinkedList(array);
            DLinkedList expected = new DLinkedList(expectedArray);

            actual.DeleteElementsWithValue(value);
            Assert.AreEqual(expected, actual);
        }
Beispiel #25
0
        public void GetIndexByValueTest(int[] array, int value, int index)
        {
            int         expected = index;
            DLinkedList ll       = new DLinkedList(array);
            int         actual   = ll.GetIndexByValue(index);

            Assert.AreEqual(expected, actual);
        }
Beispiel #26
0
        public void GetByIndexTest(int[] array, int index, int expected)
        {
            DLinkedList ll = new DLinkedList(array);

            int actual = ll[index];

            Assert.AreEqual(expected, actual);
        }
Beispiel #27
0
        int removeTail()
        {
            DLinkedList node = tail.pre;
            int         key  = map.FirstOrDefault(pair => pair.Value == node).Key;

            deleteNode(node);
            return(key);
        }
Beispiel #28
0
        public void GetIndexOfMinElementTest(int[] array, int expected)
        {
            DLinkedList list = new DLinkedList(array);

            int actual = list.GetIndexOfMinElement();

            Assert.AreEqual(expected, actual);
        }
Beispiel #29
0
        public void GetLengthTest(int[] array, int expLength)
        {
            int         expected = expLength;
            DLinkedList ll       = new DLinkedList(array);
            int         result   = ll.Length;

            Assert.AreEqual(expected, result);
        }
Beispiel #30
0
        public void ReversListTest(int[] array, int[] expectedArray)
        {
            DLinkedList actual   = new DLinkedList(array);
            DLinkedList expected = new DLinkedList(expectedArray);

            actual.ReversList();
            Assert.AreEqual(expected, actual);
        }
        public void IsEmpty()
        {
            DLinkedList<int>emptyList = new DLinkedList<int>();
            bool expectedResult = true, result;


            result = emptyList.IsEmpty();

            Assert.AreEqual(expectedResult, result);

        }
        public void PushFront()
        {
            DLinkedList<int> list = new DLinkedList<int>();
            int expectedResult = 5, result;
            
            list.PushFront(expectedResult);
            result = list.OnFront();

            Assert.AreEqual(expectedResult, result);

        }
        public void PopBack()
        {
            DLinkedList<int> list = new DLinkedList<int>();
            int expectedResult = 2, result;

            list.PushFront(1);
            list.PushFront(2);
            list.PushFront(3);
            list.PopBack();

            result = list.OnBack();

            Assert.AreEqual(expectedResult, result);
        }
 public void OnBack_ListIsEmptyThrowsException()
 {
     DLinkedList<int> emptyList = new DLinkedList<int>();
     emptyList.OnBack();
 }
        public void GetEnumerator()
        {
            DLinkedList<string> list = new DLinkedList<string>();
            string result="", expectedResult = "abcdef";
            
            list.PushBack("a");
            list.PushBack("b");
            list.PushBack("c");
            list.PushBack("d");
            list.PushBack("e");
            list.PushBack("f");

            foreach (var s in list)
            {
                result += s;
            }

            Assert.AreEqual(expectedResult,result);
        }
        public void Clear()
        {
            DLinkedList<string> list = new DLinkedList<string>();
            
            list.PushBack("a");
            list.PushBack("b");
            list.PushBack("c");
            list.PushBack("d");
            list.PushBack("e");
            list.PushBack("f");
            list.Clear();

            Assert.IsTrue(list.IsEmpty());
        }
 public void PopFront_ListIsEmptyThrowsException()
 {
     DLinkedList<int> emptyList = new DLinkedList<int>();
     emptyList.PopFront();
 }
		public static void DoTest ()
		{
			int index = 0;
			DLinkedList<string> listOfStrings = new DLinkedList<string>();

            listOfStrings.Append("zero");
			listOfStrings.Append("fst");
			listOfStrings.Append("sec");
			listOfStrings.Append("trd");
			listOfStrings.Append("for");
			listOfStrings.Append("fft");
			listOfStrings.Append("sxt");
			listOfStrings.Append("svn");
			listOfStrings.Append("egt");

			// Print
			Console.WriteLine(listOfStrings.ToReadable());

			// Remove 1st
			listOfStrings.RemoveAt(0);
			Console.WriteLine("Remove At 0:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[0] == "fst", "Wrong first element.");

            // Remove 4th
            listOfStrings.RemoveAt(4);
            Console.WriteLine("Remove At 4:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[4] == "sxt", "Wrong 4th element.");

			// Remove 5th and 6th
            // Note that after removing 5th, the old element at index 6 becomes at index 5.
			listOfStrings.RemoveAt(5);
			listOfStrings.RemoveAt(5);
			Console.WriteLine("Remove At 5 & 6:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[4] == "sxt", "Wrong element at index 5.");
            Debug.Assert(listOfStrings.Count < 6, "Wrong element at index 6. There must be no element at index 5.");

			// Remove 3rd
            listOfStrings.RemoveAt(listOfStrings.Count - 1);
			Console.WriteLine("Removed last:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[3] == "for", "Wrong element at index 3.");

			// Remove 1st
			listOfStrings.RemoveAt(0);
			Console.WriteLine("Remove 0th:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[0] == "sec", "Wrong element at index 0.");

			listOfStrings.Prepend("semsem3");
			listOfStrings.Prepend("semsem2");
			listOfStrings.Prepend("semsem1");
			Console.WriteLine("Prepend 3 items:\r\n" + listOfStrings.ToReadable());
			Console.WriteLine("Count: " + listOfStrings.Count);

			listOfStrings.InsertAt("InsertedAtLast1", listOfStrings.Count);
			listOfStrings.InsertAt("InsertedAtLast2", listOfStrings.Count);
			listOfStrings.InsertAt("InsertedAtMiddle", (listOfStrings.Count / 2));
			listOfStrings.InsertAt("InsertedAt 4", 4);
			listOfStrings.InsertAt("InsertedAt 9", 9);
			listOfStrings.InsertAfter("InsertedAfter 11", 11);
			Console.WriteLine("Inserts 3 items At:\r\n" + listOfStrings.ToReadable());

			// Test the remove item method
			listOfStrings.Remove ("trd");
			Console.WriteLine("Removed item 'trd':\r\n" + listOfStrings.ToReadable());

			listOfStrings.Remove ("InsertedAt 9");
			Console.WriteLine("Removed item 'InsertedAt 9':\r\n" + listOfStrings.ToReadable());

			// Print count
			Console.WriteLine("Count: " + listOfStrings.Count);

			Console.WriteLine();

			index = 0;
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			index = (listOfStrings.Count / 2) + 1;
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			index = (listOfStrings.Count / 2) + 2;
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			index = (listOfStrings.Count - 1);
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			Console.WriteLine();

			var firstRange = listOfStrings.GetRange(4, 6);
			Console.WriteLine("GetRange(4, 6):\r\n" + firstRange.ToReadable());

			var secondRange = firstRange.GetRange(4, 10);
			Console.WriteLine("From Previous GetRange(4, 10):\r\n" + secondRange.ToReadable());

			var thirdRange = (new DLinkedList<string>()).GetRange(0, 10);
			Console.WriteLine("Empty List: GetRange(0, 10):\r\n" + thirdRange.ToReadable());

			var arrayVersion = listOfStrings.ToArray();
			Debug.Assert (arrayVersion.Length == listOfStrings.Count);



            /****************************************************************************************/

            var stringsIterators = listOfStrings.GetEnumerator();

            Debug.Assert(stringsIterators.Current == listOfStrings[0], "Wrong enumeration.");

            if (stringsIterators.MoveNext() == true)
            {
                Debug.Assert(stringsIterators.Current == listOfStrings[1], "Wrong enumeration.");
            }

            stringsIterators.Dispose();

            Debug.Assert(listOfStrings != null && listOfStrings.Count > 0, "Enumartor has side effects!");

            /****************************************************************************************/

            var listOfNumbers = new DLinkedList<int>();

            listOfNumbers.Append(23);
            listOfNumbers.Append(42);
            listOfNumbers.Append(4);
            listOfNumbers.Append(16);
            listOfNumbers.Append(8);
            listOfNumbers.Append(15);
            listOfNumbers.Append(9);
            listOfNumbers.Append(55);
            listOfNumbers.Append(0);
            listOfNumbers.Append(34);
            listOfNumbers.Append(12);
            listOfNumbers.Append(2);

            listOfNumbers.SelectionSort();

            var intArray = listOfNumbers.ToArray();

            Debug.Assert(intArray[0] == 0 && intArray[intArray.Length - 1] == 55, "Wrong sorting!");
		}