Doubly linked list insertion

insert node at the beginning of a Doubly linked list

Doubly linked list insertion

use 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
Doubly linked list insertion

4.link newnode->next address to head pointer

Doubly linked list insertion

5.link head->prev address to newnode

Doubly linked list insertion

6.initialize head pointer to the newnode

Doubly linked list insertion

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

Doubly linked list insertion

use 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
Doubly linked list insertion

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

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

Doubly linked list insertion

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

Doubly linked list insertion

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;
}
}
Last updated on by vishal devxo