コード例 #1
0
        public void insert(int index, int value)
        {
            index = wrapIndex(index);
            if (index == 0)
            {
                prepend(value);
                return;
            }

            if (index == length - 1)
            {
                append(value);
                return;
            }

            NodeD newNodeD = new NodeD(value);

            NodeD leader   = traverseToIndex(index - 1);
            NodeD follower = leader.next;

            leader.next       = newNodeD;
            newNodeD.previous = leader;
            newNodeD.next     = follower;
            follower.previous = newNodeD;

            this.length++;
        }
コード例 #2
0
        public void prepend(int value)
        {
            NodeD newNodeD = new NodeD(value);

            newNodeD.next      = this.head;
            this.head.previous = newNodeD;
            this.head          = newNodeD;
            this.length++;
        }
コード例 #3
0
        public void append(int value)
        {
            NodeD newNodeD = new NodeD(value);

            newNodeD.previous = this.tail;
            this.tail.next    = newNodeD;
            this.tail         = newNodeD;
            this.length++;
        }
コード例 #4
0
        private NodeD traverseToIndex(int index)
        {
            int counter = 0;

            index = wrapIndex(index);
            NodeD currentNodeD = head;

            while (counter != index)
            {
                currentNodeD = currentNodeD.next;
                counter++;
            }
            return(currentNodeD);
        }
コード例 #5
0
        public void printList()
        {
            if (this.head == null)
            {
                return;
            }
            NodeD current = this.head;

            while (current != null)
            {
                Console.Write("-->" + current.value);
                current = current.next;
            }
            Console.WriteLine();
        }
コード例 #6
0
        public void remove(int index)
        {
            index = wrapIndex(index);
            if (index == 0)
            {
                head = head.next;
                return;
            }

            NodeD leader        = traverseToIndex(index - 1);
            NodeD NodeDToRemove = leader.next;

            leader.next = NodeDToRemove.next;
            NodeDToRemove.next.previous = leader;
            this.length--;
        }
コード例 #7
0
 public DoublyLinkedList(int value)
 {
     this.head   = new NodeD(value);
     this.tail   = this.head;
     this.length = 1;
 }
コード例 #8
0
 public NodeD(int value)
 {
     this.value    = value;
     this.next     = null;
     this.previous = null;
 }