Q. Program to insert a new node at the beginning of the doubly linked list.ExplanationIn this program, we will create a doubly linked list and insert every new node at the beginning of the list. If the list is empty, then head and tail will point to the newly added node. If the list is not empty then, insert the new node at the beginning of the list such that the head's previous will point to new node. Make new node as head of the list, and its previous will point to null. Show Consider the above example. Initially, 1 was the head of the list. Now new will be inserted before node 1 such that node 1's previous will point to new. Make new as the head of the list, and its previous will point to null. Algorithm
Doubly Linked List | Set 1 (Introduction and Insertion)We strongly recommend to refer following post as a prerequisite of this post. 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 Algorithm:
Example in C:
#include<stdio.h> #include<stdlib.h> void insertAtBeginning(int); struct node { int data; struct node *next; struct node *prev; }; struct node *head; void main () { int choice,item; do { printf("\nEnter the element to insert:\n"); scanf("%d",&item); insertAtBeginning(item); printf("\nPress 1 to insert more:\n"); scanf("%d",&choice); }while(choice == 1); } void insertAtBeginning(int item) { struct node *ptr = (struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { printf("\nOVERFLOW"); } else { if(head==NULL) { ptr->next = NULL; ptr->prev=NULL; ptr->data=item; head=ptr; } else { ptr->data=item; ptr->prev=NULL; ptr->next = head; head->prev=ptr; head=ptr; } } } Output:
Enter the element to insert: 2 Press 1 to insert more: 1 Enter the element to insert: 4 Press 1 to insert more: 1 Enter the element to insert: 6 Press 1 to insert more: 1 Enter the element to insert: 8 Press 1 to insert more: Please Share Insert node at beginning of Doubly linked listThat is very simple to add new created doubly linked list node at front (beginning) position. That are similar to single linked list but only the difference are this are contain two pointer fields. next and previous pointer. next pointer are hold the reference of next node of linked list and previous pointer are hold the reference of previous node. Insertion approach: There is two cases to add doubly linked list node at front position. First is when linked list are empty, and we are insert first element of linked list. In this case created newly doubly linked list node and set this node pointer fields to (next and previous) NULL. and assign this node address to head (head pointer) of linked list. When linked list are not empty then create a doubly linked list node and this node next pointer are assign the address of first node of linked list (Note that head pointer are holds the reference of first node). And this head node previous pointer field are assign newly created node and new created node next pointer is assign to head of linked list. after that provide this new node reference to head pointer. Suppose we are inserted the following (80,70,60,50,40,30,20,10) node in a sequence. Here given code implementation process.
In this above example doubly linked list are accessed by first node. Observed that in case we are have one more variable which are point to last node then we can access both direction of linked list. |