コード例 #1
28
        // READ: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
        // list [i] = value; i is new list q is old list
        public string this[int i]
        {
            get { return this.ElementAt(i); }
            set {
                var placeHolderList = new SinglyLinkedList();

                for (var q = 0; q < this.Count(); q++)
                {
                   if (q==i)
                    {
                        placeHolderList.AddLast(value);
                    }
                    else
                    {
                        placeHolderList.AddLast(this.ElementAt(q));
                    }
                }

                first_node = new SinglyLinkedListNode(placeHolderList.First());
                for (var w = 1; w < placeHolderList.Count(); w++)
                {
                    this.AddLast(placeHolderList.ElementAt(w));
                }
            }
        }
コード例 #2
0
 public void AddAfterItemThatDoesntExist()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddAfter("cat", "grille");
 }
コード例 #3
0
        // READ: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
        public string this[int i]
        {
            //uses the ElementAt function to get value (saves you from duplicating your code)
            get { return this.ElementAt(i); }
            set
            {
                //using a placeholder list helps save the values in order without having to do voodoo with the pointers
                var placeholderList = new SinglyLinkedList();
                for (var q = 0; q < this.Count(); q++)
                {
                    //if this is the place where you need to exchange the value, insert it here
                    if (q == i)
                    {
                        placeholderList.AddLast(value);
                    }
                    //otherwise, insert the value that was preexisting in the list
                    else
                    {
                        placeholderList.AddLast(this.ElementAt(q));
                    }
                }

                //now swap out the values in the placeholder list into the real list.
                //first swap the first value to clear out the old list
                first_node = new SinglyLinkedListNode(placeholderList.First());
                //then loop through the placeholder list and add the values to the real list in order
                for (var w = 1; w < placeholderList.Count(); w++)
                {
                    this.AddLast(placeholderList.ElementAt(w));
                }
            }
        }
コード例 #4
0
        // READ: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
        public string this[int i]
        {
            get { return this.ElementAt(i); }
            set
            {
                var copy_of_list = new SinglyLinkedList();
                for (var j = 0; j < this.Count(); j++)
                {
                    if (j == i)
                    {
                        copy_of_list.AddLast(value);
                    }
                    else
                    {
                        copy_of_list.AddLast(this.ElementAt(j));
                    }
                }

                first_node = new SinglyLinkedListNode(copy_of_list.First());
                for (var o = 1; o < copy_of_list.Count(); o++)
                {
                    this.AddLast(copy_of_list.ElementAt(o));
                }
            }
        }
コード例 #5
0
        // READ: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
        public string this[int i]
        {
            get { return this.ElementAt(i); }
            set
            {
                var newList = new SinglyLinkedList();

                for (var x = 0; x < this.Count(); x++)
                {
                    if (x == i)
                    {
                        newList.AddLast(value);
                    }
                    else
                    {
                        newList.AddLast(this.ElementAt(x));
                    }
                }
                    first_node = new SinglyLinkedListNode(newList.First());
                    for (var w = 1; w < newList.Count(); w++)
                {
                    this.AddLast(newList.ElementAt(w));

                }
                }
        }
コード例 #6
0
        // READ: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
        public string this[int index]
        {
            get
            {
                return this.ElementAt(index);
            }
            set
            {
                var placeholder = new SinglyLinkedList();
                for(var j = 0; j < this.Count(); j++)
                {
                    if (j == index)
                    {
                        placeholder.AddLast(value);
                    } else
                    {
                        placeholder.AddLast(this.ElementAt(j));
                    }
                }
                first_node = new SinglyLinkedListNode(placeholder.First());

                for(var k = 1; k < placeholder.Count(); k++ )
                {
                    this.AddLast(placeholder.ElementAt(k));
                }
               }
        }
コード例 #7
0
 public void AddAfterLastItem()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddAfter("bar", "grille");
     var expected = new string[] { "foo", "bar", "grille" };
     CollectionAssert.AreEqual(expected, list.ToArray());
 }
コード例 #8
0
 public void AddFirstOnLongerList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddFirst("grille");
     var expected = new string[] { "grille", "foo", "bar" };
     CollectionAssert.AreEqual(expected, list.ToArray());
 }
コード例 #9
0
 public void CaitlinsArrayCount()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     string[] lexpected = new string[] { "foo", "bar", "grille" };
     int expected = lexpected.Length;
     string[] lactual = list.ToArray();
     int actual = lactual.Length;
     Assert.AreEqual(expected, actual);
 }
コード例 #10
0
        public void AddAfter()
        {
            SinglyLinkedList list = new SinglyLinkedList();
            list.AddLast("foo");
            list.AddLast("grille");
            // NOTE: This assert isn't necessary.  It is merely here to remind you of / verify the state of the list prior to inserting the new node.
            var expected = new string[] { "foo", "grille" };
            CollectionAssert.AreEqual(expected, list.ToArray());

            list.AddAfter("foo", "bar");
            expected = new string[] { "foo", "bar", "grille" };
            CollectionAssert.AreEqual(expected, list.ToArray());
        }
コード例 #11
0
        public void UnnamedSort()
        {
            int count = this.Count();

            if (count < 2)
            {
                return;
            }
            SinglyLinkedList sorted = new SinglyLinkedList();

            for (int i = 0; i < count; i++)
            {
                SinglyLinkedListNode least       = this.firstNode;
                SinglyLinkedListNode currentNode = firstNode.Next;
                while (currentNode != null)
                {
                    if (least > currentNode)
                    {
                        least = currentNode;
                    }
                    currentNode = currentNode.Next;
                }
                sorted.AddLast(least.Value);
                this.Remove(least.Value);
            }
            this.firstNode = sorted.firstNode;
        }
コード例 #12
0
        public void AddAfter(string existingValue, string value)
        {
            //Create an int to store the place in line of the newValue
            int testForValue = -1;

            //find the existing value in the linked list and assign the place in testForValue
            for (var i = 0; i < this.Count(); i++)
            {
                if (this.ElementAt(i) == existingValue)
                {
                    testForValue = i;
                    break;
                }
            }

            //if the existing value isn't in the linked list (if it remains a -1 after the if statement), throw an error
            if (testForValue < 0)
            {
                throw new ArgumentException();
            }

            //use a placeholder linked list to store the nodes in the correct order
            var placeholderList = new SinglyLinkedList();

            //loop through the current linked list and assign each of the values to the placeholder list
            //and insert the new value in its proper place
            for (var q = 0; q < this.Count(); q++)
            {
                //add each value to the placeholder list
                placeholderList.AddLast(this.ElementAt(q));
                //if this spot is the spot where you need to add the new value, add the new value here
                if (q == testForValue)
                {
                    placeholderList.AddLast(value);
                }
            }
            //now reassign the values to the current linked list in order (this.First() etc)
            first_node = new SinglyLinkedListNode(placeholderList.First());
            for (var w = 1; w < placeholderList.Count(); w++)
            {
                this.AddLast(placeholderList.ElementAt(w));
            }
        }
コード例 #13
0
        public void AddAfter(string existingValue, string value)
        {
            //Find the place of  existingValue
            int position = IndexOf(existingValue);
            if (position == -1)//if not in list throw exception
            {

                throw new ArgumentException();
            }
            var placeholder = new SinglyLinkedList(); //assigns a new list to hold values
            for (var i = 0; i < this.Count(); i++)
            {
                placeholder.AddLast(this.ElementAt(i));
                if (i == position)
                {
                    placeholder.AddLast(value); //find the correct position and add new value
                }
            }
            first_node = new SinglyLinkedListNode(placeholder.First());//assign first node to be the new list
            for (var j = 1; j < placeholder.Count(); j++)
            {
                this.AddLast(placeholder.ElementAt(j));//add the remaining values in list
            }
        }
コード例 #14
0
        // READ: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
        public string this[int i]
        {
            get {return this.ElementAt(i); }
            set
            {
                var placeholder = new SinglyLinkedList();
                for (var k = 0; k < this.Count(); k++)
                {

                    if (i == k)
                    {
                        placeholder.AddLast(value); //if this is the position this is the new value and don't save the existing value.
                    }else
                    {
                        placeholder.AddLast(this.ElementAt(k));
                    }
                }
                first_node = new SinglyLinkedListNode(placeholder.First());//assign first node to be the new list
                for (var j = 1; j < placeholder.Count(); j++)
                {
                    this.AddLast(placeholder.ElementAt(j));//add the remaining values in list
                }
            }
        }
コード例 #15
0
        public void Remove(string value)
        {
            int number          = this.IndexOf(value);
            var placeholderList = new SinglyLinkedList();

            for (var i = 0; i < this.Count(); i++)
            {
                if (i != number)
                {
                    placeholderList.AddLast(this.ElementAt(i));
                }
            }
            first_node = new SinglyLinkedListNode(placeholderList.First());
            for (var q = 1; q < placeholderList.Count(); q++)
            {
                this.AddLast(placeholderList.ElementAt(q));
            }
        }
コード例 #16
0
        public void MergeSort(SinglyLinkedList input)
        {
            // Incomplete
            int count = this.Count();

            Console.WriteLine("Original List: {0}", this);
            SinglyLinkedList listOne = new SinglyLinkedList();
            SinglyLinkedList listTwo = new SinglyLinkedList();
            decimal          split   = count / 2;

            for (int i = 0; i < split; i++)
            {
                Console.WriteLine("Iteration {0}", i);
                Console.WriteLine("Split {0}", split);
                listOne.AddLast(this[i]);
                listTwo.AddLast(this[i + (int)split]);
            }
            Console.WriteLine("listOne: {0}", listOne);
            Console.WriteLine("listTwo: {0}", listTwo);
        }
コード例 #17
0
 public void AddFirst(string value)
 {
     //if this is the first node, this is just an assignment
     if (this.First() == null)
     {
         first_node = new SinglyLinkedListNode(value);
     }
     else
     {
         //create a placeholder list to store the values in order.
         var placeholderList = new SinglyLinkedList();
         placeholderList.AddFirst(value);
         for (var i = 0; i < this.Count(); i++)
         {
             placeholderList.AddLast(this.ElementAt(i));
         }
         //now reassign the values to the current linked list in order (this.First() etc)
         first_node = new SinglyLinkedListNode(placeholderList.First());
         for (var q = 1; q < placeholderList.Count(); q++)
         {
             this.AddLast(placeholderList.ElementAt(q));
         }
     }
 }
コード例 #18
0
 public void LastOnShortList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     Assert.AreEqual("foo", list.Last());
 }
コード例 #19
0
 public void FirstOnShortList()
 {
     SinglyLinkedList<string> list = new SinglyLinkedList<string>();
     list.AddLast("foo");
     Assert.AreEqual("foo", list.First());
 }
コード例 #20
0
 public void LastOnLongerList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     Assert.AreEqual("grille", list.Last());
 }
コード例 #21
0
 public void AddFirst(string value)
 {
     //if this is the first node, this is just an assignment
     if (this.First() == null)
     {
         first_node = new SinglyLinkedListNode(value);
     }
     else
     {
         //create a placeholder list to store the values in order.
         var placeholderList = new SinglyLinkedList();
         placeholderList.AddFirst(value);
         for (var i = 0; i < this.Count(); i++)
         {
             placeholderList.AddLast(this.ElementAt(i));
         }
         //now reassign the values to the current linked list in order (this.First() etc)
         first_node = new SinglyLinkedListNode(placeholderList.First());
         for (var q = 1; q < placeholderList.Count(); q++ )
         {
             this.AddLast(placeholderList.ElementAt(q));
         }
     }
 }
コード例 #22
0
 public void ToStringOnMultipleItemList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     Assert.AreEqual("{ \"foo\", \"bar\", \"grille\" }", list.ToString());
 }
コード例 #23
0
 public void ElementAt0()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     Assert.AreEqual("foo", list.ElementAt(0));
 }
コード例 #24
0
 public void CountOneItemList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     Assert.AreEqual(1, list.Count());
 }
コード例 #25
0
        public void AddAfter(string existingValue, string value)
        {
            int testForValue = -1;

            for (var i = 0; i < this.Count(); i++)
            {
                if (this.ElementAt(i) == existingValue)
                {
                    testForValue = i;
                    break;
                }

            }
            if (testForValue == -1)
            {
                throw new ArgumentException();
            }//one = equalss setting value, two == equals tests for equality
            var placeHolderList = new SinglyLinkedList();

            for (var q = 0; q < this.Count(); q++)
            {
                placeHolderList.AddLast(this.ElementAt(q));

                if (q ==testForValue)
                {
                    placeHolderList.AddLast(value);
                }
            }

            first_node = new SinglyLinkedListNode(placeHolderList.First());
            for (var w = 1; w <placeHolderList.Count(); w++)
            {
                this.AddLast(placeHolderList.ElementAt(w));
            }
        }
コード例 #26
0
 public void IndexOfRemovedElement()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     list.Remove("foo");
     Assert.AreEqual(-1, list.IndexOf("foo"));
 }
コード例 #27
0
 public void Remove(string value)
 {
     int number = this.IndexOf(value);
     var placeholderList = new SinglyLinkedList();
     for (var i = 0; i < this.Count(); i++)
     {
         if (i != number)
         {
             placeholderList.AddLast(this.ElementAt(i));
         }
     }
     first_node = new SinglyLinkedListNode(placeholderList.First());
     for (var q = 1; q < placeholderList.Count(); q++)
     {
         this.AddLast(placeholderList.ElementAt(q));
     }
 }
コード例 #28
0
 public void ToArrayOnSingleItemList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     string[] expected = new string[] { "foo" };
     CollectionAssert.AreEqual(expected, list.ToArray());
 }
コード例 #29
0
 public void ToArrayOnMultipleItemList()
 {
     SinglyLinkedList<string> list = new SinglyLinkedList<string>();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     string[] expected = new string[] { "foo", "bar", "grille" };
     CollectionAssert.AreEqual(expected, list.ToArray());
 }
コード例 #30
0
 public void ToStringOnSingleItemList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     Assert.AreEqual("{ \"foo\" }", list.ToString());
 }
コード例 #31
0
 public void ElementAtEndOfList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     Assert.AreEqual("grille", list.ElementAt(2));
 }
コード例 #32
0
 public void ElementAtN()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("bar");
     list.AddLast("grille");
     list.ElementAt(5);
 }
コード例 #33
0
 public void ElementAtNegativeIndex()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     list.ElementAt(-2);
 }
コード例 #34
0
 public void CountNItemList()
 {
     SinglyLinkedList list = new SinglyLinkedList();
     list.AddLast("foo");
     list.AddLast("bar");
     list.AddLast("grille");
     list.AddLast("baz");
     Assert.AreEqual(4, list.Count());
 }
コード例 #35
0
        public void AddAfter(string existingValue, string value)
        {
            //Create an int to store the place in line of the newValue
            int testForValue = -1;

            //find the existing value in the linked list and assign the place in testForValue
            for (var i = 0; i < this.Count(); i++)
            {
                if (this.ElementAt(i) == existingValue)
                {
                    testForValue = i;
                    break;
                }
            }

            //if the existing value isn't in the linked list (if it remains a -1 after the if statement), throw an error
            if (testForValue < 0)
            {
                throw new ArgumentException();
            }

            //use a placeholder linked list to store the nodes in the correct order
            var placeholderList = new SinglyLinkedList();

            //loop through the current linked list and assign each of the values to the placeholder list
            //and insert the new value in its proper place
            for (var q = 0; q < this.Count(); q++)
            {
                //add each value to the placeholder list
                placeholderList.AddLast(this.ElementAt(q));
                //if this spot is the spot where you need to add the new value, add the new value here
                if (q == testForValue)
                {
                    placeholderList.AddLast(value);
                }
            }
            //now reassign the values to the current linked list in order (this.First() etc)
            first_node = new SinglyLinkedListNode(placeholderList.First());
            for (var w = 1; w < placeholderList.Count(); w++)
            {
                this.AddLast(placeholderList.ElementAt(w));
            }
        }