Doubly Linked List | Set 1 (Introduction and Insertion)We strongly recommend to refer following post as a prerequisite of this post. Show Following is representation of a DLL node in C language. C++
C
Java
Python3
C#
Javascript
Following are advantages/disadvantages of doubly linked list over singly linked list. Disadvantages over singly linked list Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. 1) Add a node at the front: (A 5 steps process) Following are the 5 steps to add node at the front. C++
C
Java
Python3
C#
Javascript
Four steps of the above five steps are same as the 4 steps used for inserting at the front in singly linked list. The only extra step is to change previous of head. C++
C
Java
Python3
C#
Javascript
Five of the above steps step process are same as the 5 steps used for inserting after a given node in singly linked list. The two extra steps are needed to change previous pointer of new node and previous pointer of new node’s next node. Following are the 7 steps to add node at the end. C++
C
Java
Python3
C#
Javascript
Six of the above 7 steps are same as the 6 steps used for inserting after a given node in singly linked list. The one extra step is needed to change previous pointer of new node. Steps
Below is the implementation of the above approach: Code block Output: Created DLL is: Traversal in forward Direction 9 1 5 7 6 Traversal in reverse direction 6 7 5 1 9 A complete working program to test above functions. C++
C
Java
Python3
C#
Javascript
Output: Created DLL is: Traversal in forward Direction 1 7 5 8 6 4 Traversal in reverse direction 4 6 8 5 7 1Also see: Delete a node in double Link List
Article Tags :
Data Structures Linked List
doubly linked list Visa Practice Tags :
Visa Data Structures Linked List Insertion in doubly linked list after Specified nodeIn order to insert a node after the specified node in the list, we need to skip the required number of nodes in order to reach the mentioned node and then make the pointer adjustments as required. Use the following steps for this purpose.
make the prev of the new node ptr point to temp. make the next pointer of temp point to the new node ptr. make the previous pointer of the next node of temp point to the new node. Q. Program to insert a new node at the middle of doubly linked list.ExplanationIn this program, we create a doubly linked list and insert a new node in the middle of list. If list is empty, both head and tail will point to new node. If list is not empty, then we will calculate the size of the list and divide it by 2 to get the mid-point of the list where new node needs to be inserted. Consider the above diagram; a new node needs to be added to the middle of the list. First, we calculate the size which in this case is 4. So, to get the mid-point, we divide it by 2 and store it in a variable mid. Node current will point to head. First, we iterate through the list until current points to mid position. Define another node temp which point to node next to current. Insert the new node between current and temp Algorithm
|