Doubly linked list insertion

Last Updated on

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

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

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

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;
	 }
}