コード例 #1
0
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void DeleteAtIndex(int index)
        {
            var tempList = this.node;

            if (index == 0)
            {
                this.node = this.node.next;
            }
            else if (index < Count)
            {
                for (int i = 0; i < index; i++)
                {
                    if (i == (index - 1))
                    {
                        var prev = tempList;
                        tempList  = tempList.next;
                        prev.next = tempList.next;
                    }
                    else
                    {
                        tempList = tempList.next;
                    }
                }
            }
            else
            {
                return;
            }
            Count--;
        }
コード例 #2
0
        /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
        public void AddAtIndex(int index, int val)
        {
            if (index == 0)
            {
                this.AddAtHead(val);
                return;
            }
            if (index == Count)
            {
                this.AddAtTail(val);
                return;
            }

            if (index < Count)
            {
                var tempList = this.node;
                for (int i = 0; i < index; i++)
                {
                    if (i == (index - 1))
                    {
                        var curr = new SinglyListNode(val);
                        curr.next     = tempList.next;
                        tempList.next = curr;
                    }
                    else
                    {
                        tempList = tempList.next;
                    }
                }
            }

            Count++;
        }
コード例 #3
0
 /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
 public void AddAtHead(int val)
 {
     if (node == null)
     {
         node = new SinglyListNode(val);
     }
     else
     {
         SinglyListNode newHeadNode = new SinglyListNode(val);
         newHeadNode.next = node;
         this.node        = newHeadNode;
     }
     Count++;
 }
コード例 #4
0
 /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
 public int Get(int index)
 {
     if (Count != 0 && index < Count)
     {
         SinglyListNode tempList = node;
         for (int i = 0; i < index; i++)
         {
             tempList = tempList.next;
         }
         return(tempList.val);
     }
     else
     {
         return(-1);
     }
 }