Example #1
0
    /* Given a node as prev_node, insert a new node after the given node */
    public void InsertAfter(NodeDouble prev_Node, int new_data)
    {
        /*1. check if the given prev_node is NULL */
        if (prev_Node == null)
        {
            Console.WriteLine("The given previous node cannot be NULL ");
            return;
        }

        /* 2. allocate node
         * 3. put in the data */
        NodeDouble new_node = new NodeDouble(new_data);

        /* 4. Make next of new node as next of prev_node */
        new_node.next = prev_Node.next;


        /* 5. Make the next of prev_node as new_node */
        prev_Node.next = new_node;


        /* 6. Make prev_node as previous of new_node */
        new_node.prev = prev_Node;

        /* 7. Change previous of new_node's next node */
        if (new_node.next != null)
        {
            new_node.next.prev = new_node;
        }
    }
Example #2
0
    //Add a node at the end of the list
    void append(int new_data)
    {
        /* 1. allocate node
         * 2. put in the data */
        NodeDouble new_node = new NodeDouble(new_data);

        NodeDouble last = head;/* used in step 5*/

        /* 3. This new node is going to be the last node, so
         * make next of it as NULL*/

        new_node.next = null;

        /* 4. If the Linked List is empty, then make the new
         * node as head */
        if (head == null)
        {
            new_node.prev = null;
            head          = new_node;
            return;
        }

        /* 5. Else traverse till the last node */
        while (last.next != null)
        {
            last = last.next;
        }

        /* 6. Change the next of last node */
        last.next = new_node;

        /* 7. Make last node as previous of new node */
        new_node.prev = last;
    }
Example #3
0
    /*Function to delete a node in a Doubly Linked List.
     * head_ref --> pointer to head node pointer.
     * del  -->  pointer to node to be deleted. */
    void deleteNode(NodeDouble head_ref, NodeDouble del)
    {
        /* base case */
        if (head == null || del == null)
        {
            return;
        }

        /* If node to be deleted is head node */
        if (head == del)
        {
            head = del.next;
        }

        /* Change next only if node to be deleted is NOT the last node */
        if (del.next != null)
        {
            del.next.prev = del.prev;
        }

        /* Change prev only if node to be deleted is NOT the first node */
        if (del.prev != null)
        {
            del.prev.next = del.next;
        }

        /* Finally, free the memory occupied by del*/
        return;
    }
Example #4
0
        public static NodeDouble <int> CloneDoubleLinkedList(NodeDouble <int> linkedList)
        {
            var current = linkedList;

            // insert cloned nodes after each orriginal node
            while (current != null)
            {
                var cloneCurrent = new NodeDouble <int>(current.Value);
                // connecting - cloneCurrent.Next to current.Next and current to cloneCurrent
                cloneCurrent.Next = current.Next;
                current.Next      = cloneCurrent;

                current = cloneCurrent.Next;
            }

            // connecting .Previous clones
            current = linkedList;
            while (current != null)
            {
                if (current.Previous != null)
                {
                    var cloneOfThePrevious = current.Previous.Next;
                    var cloneOfTheCurrent  = current.Next;
                    if (cloneOfTheCurrent != null)
                    {
                        cloneOfTheCurrent.Previous = cloneOfThePrevious;
                    }

                    current = cloneOfTheCurrent.Next;
                }
                else
                {
                    current = current.Next.Next;
                }
            }

            // razdvajanje u dve liste
            current = linkedList;
            var linkedListClone = current.Next;

            while (current != null)
            {
                var tempPointer = current.Next;
                if (current.Next != null)
                {
                    current.Next = current.Next.Next;
                }

                current = tempPointer;
            }

            return(linkedListClone);
        }
Example #5
0
    // This function prints contents of linked list starting from the given node
    public void printlist(NodeDouble node)
    {
        NodeDouble last = null;

        Console.WriteLine("Traversal in forward Direction");
        while (node != null)
        {
            Console.WriteLine(node.data + " ");
            last = node;
            node = node.next;
        }
        Console.WriteLine();
        Console.WriteLine("Traversal in reverse direction");
        while (last != null)
        {
            Console.WriteLine(last.data + " ");
            last = last.prev;
        }
    }
Example #6
0
    NodeDouble head; // head of list

    //Adding a node at the front of the list
    public void push(int new_data)
    {
        /* 1. allocate node
         * 2. put in the data */
        NodeDouble new_Node = new NodeDouble(new_data);

        /* 3. Make next of new node as head and previous as NULL */
        new_Node.next = head;
        new_Node.prev = null;

        /* 4. change prev of head node to new node */
        if (head != null)
        {
            head.prev = new_Node;
        }

        /* 5. move the head to point to the new node */
        head = new_Node;
    }
Example #7
0
    public static NodeDouble <T> CreateDoubleLinkedList(T[] input)
    {
        if (input == null || input.Length == 0)
        {
            throw new System.ArgumentException("input cannot ne null or empty");
        }

        NodeDouble <T> linkedList = new NodeDouble <T>(input[0]);
        var            nextNode   = linkedList;

        for (int i = 1; i < input.Length; i++)
        {
            nextNode.Next          = new NodeDouble <T>(input[i]);
            nextNode.Next.Previous = nextNode;
            nextNode = nextNode.Next;
        }

        return(linkedList);
    }
Example #8
0
    /* UTILITY FUNCTIONS */
    /* Function to insert a node at the beginning of the Doubly Linked List */
    void push(NodeDouble head_ref, int new_data)
    {
        /* allocate node */
        NodeDouble new_node = new NodeDouble(new_data);

        /* since we are adding at the begining,
         * prev is always NULL */
        new_node.prev = null;

        /* link the old list off the new node */
        new_node.next = (head);

        /* change prev of head node to new node */
        if ((head) != null)
        {
            (head).prev = new_node;
        }

        /* move the head to point to the new node */
        (head) = new_node;
    }
Example #9
0
    /* Function to reverse a Doubly Linked List */
    void reverse()
    {
        NodeDouble temp    = null;
        NodeDouble current = head;

        /* swap next and prev for all nodes of
         * doubly linked list */
        while (current != null)
        {
            temp         = current.prev;
            current.prev = current.next;
            current.next = temp;
            current      = current.prev;
        }

        /* Before changing head, check for the cases like empty
         * list and list with only one node */
        if (temp != null)
        {
            head = temp.prev;
        }
    }
Example #10
0
 // Constructor to create a new node
 // next and prev is by default initialized as null
 public NodeDouble(int d)
 {
     this.data = d;
     this.next = null;
     this.prev = null;
 }
Example #11
0
        public static void TestCloneDoubleLinkedList()
        {
            //Given
            int[] testArray = new int[8] {
                1, 2, 3, 4, 5, 6, 7, 8
            };
            var linkedList = NodeDouble <int> .CreateDoubleLinkedList(testArray);

            // now lets mess the list a bit, with some random pointers
            NodeDouble <int>[] testNodes = new NodeDouble <int> [8];
            //put all nodes in to an array so we can access by index
            var nd = linkedList;

            for (int i = 0; i < testArray.Length; i++)
            {
                testNodes[i] = nd;
                nd           = nd.Next;
            }

            // reconnect .Previous nodes
            for (int i = 0; i < testNodes.Length; i++)
            {
                if (i == 0)
                {
                    testNodes[0].Previous = testNodes[7];
                }
                if (i == 1)
                {
                    testNodes[1].Previous = testNodes[0];
                }
                if (i == 2)
                {
                    testNodes[2].Previous = testNodes[0];
                }
                if (i == 3)
                {
                    testNodes[3].Previous = testNodes[1];                     // e.g. connect 3 to 1
                }
                if (i == 4)
                {
                    testNodes[4].Previous = testNodes[2];
                }
                if (i == 5)
                {
                    testNodes[5].Previous = testNodes[4];
                }
                if (i == 6)
                {
                    testNodes[6].Previous = testNodes[6];                     // or connect 6 to it self :p
                }
            }

            //When
            var clonedLinkedList = Solution.CloneDoubleLinkedList(linkedList);

            //Then
            var isAllCloned = true;
            var node        = linkedList;
            var clonedNode  = clonedLinkedList;

            while (node != null)
            {
                // if any of these conditions are true we do not have clone
                if (node.Value != clonedNode.Value ||
                    node == clonedNode ||
                    (node.Previous != null ? node.Previous == clonedNode.Previous : false))
                {
                    isAllCloned = false;
                    break;
                }

                node       = node.Next;
                clonedNode = clonedNode.Next;
            }

            Assert.True(isAllCloned);
        }
Example #12
0
 public NodeDouble(T value)
 {
     Value    = value;
     Next     = null;
     Previous = null;
 }