Exemplo n.º 1
0
        public void DeleteNodeAt(int position)
        {
            DLLNode current = head, temp = null;
            int     currentPosition = 1;

            if (position == 1)
            {
                head      = head.next;
                head.prev = null;
                return;
            }

            while (currentPosition < position && current.next != null)
            {
                currentPosition++;
                temp    = current;
                current = current.next;
            }

            if (current.next == null)
            {
                temp.next = null;
                return;
            }
            temp.next = current.next;
        }
Exemplo n.º 2
0
        public void PrintAllNodes()
        {
            DLLNode temp     = head;
            int     position = 1;

            while (temp != null)
            {
                Console.WriteLine($"Item : {temp.data} Position at : {position++}");
                temp = temp.next;
            }
        }
Exemplo n.º 3
0
        public void AddNode(int data)
        {
            if (head == null)
            {
                head = new DLLNode(data);
                return;
            }
            DLLNode current = head, temp = null;

            while (current.next != null)
            {
                temp    = current;
                current = current.next;
            }
            current.next = new DLLNode(data);
            current.prev = temp;
        }
Exemplo n.º 4
0
        public void InsertNodeAt(int data, int position)
        {
            DLLNode current = head, temp = null, newNode = new DLLNode(data);
            int     cuurentPosition = 1;

            if (head == null || position == 1)
            {
                head = new DLLNode(data);
                if (current != null)
                {
                    current.prev = head;
                    head.next    = current;
                }
                return;
            }

            while (cuurentPosition < position && current.next != null)
            {
                cuurentPosition++;
                temp    = current;
                current = current.next;
            }

            if (current.next == null)
            {
                newNode.prev = current;
                current.next = newNode;
                return;
            }
            newNode.prev = temp;
            if (temp == null)
            {
                temp = newNode;
            }
            else
            {
                temp.next = newNode;
            }
            current.prev = newNode;
            newNode.next = current;
        }
Exemplo n.º 5
0
 public DLLNode(int data)
 {
     this.data = data;
     next      = null;
     prev      = null;
 }