Doubly linked list insertion

insert node at the beginning of a Doubly linked list

Doubly linked list insertion

import code from the create and display nodes and getnode function to assign data click here

  1. create a function inserbeg( )
  2. create a *newnode pointer
  3. call getnode( ) function to assign data to newnode
 assign data to newnode

4.link newnode->next address to head pointer

ne wnode is linked to the next node

5.link head->prev address to newnode

Doubly linked list insertion 1

6.initialize head pointer to the newnode

initialize head pointer to the newnode

code to insert node at the beginning of a Doubly linked list

void dllinsertbeg()
{
	node *newnode;
	newnode = getnode();
	if(head == NULL)
		head = newnode;
	else
	{
		newnode -> next = head;
		head -> prev = newnode;
		head = newnode;
	 }
}

insert node at the ending of a Doubly linked list

insert node at the ending of a Doubly linked list

import code from the create and display nodes and getnode function to assign data click here

  1. create a function inserbeg( )
  2. create a *newnode ,*temp pointer
  3. call getnode( ) function to assign data to newnode,initialize head for temp
assign data to newnode

4.move temp pointer to the last node by replacing temp with next node in the doubly linked list ( temp = temp -> next) until the condition (temp -> next != NULL),so the temp will be pointed to the last node when the condition fails

Doubly linked list insertion 2

5.link next address of temp to newnode (temp -> next = newnode)

Doubly linked list insertion 3

6.link previous of newnode node to temp node (newnode -> previous = temp)

link previous of newnode node to temp node

code to insert node at the ending of a Doubly linked list

void dllinsertend()
{
	node *newnode, *temp;
	newnode = getnode();
	if(head == NULL)
		head = newnode;
	else
	{
		temp = head;
		while(temp -> next != NULL)
			temp = temp -> next;
		temp -> next = newnode;
		newnode -> prev = temp;
	 }
}
Default image
Vishal Devxo
Vishal Devxo is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python