0% found this document useful (0 votes)
30 views1 page

Double Link List Insert

This method inserts a new node with data x into a doubly linked list at position p. It first checks if p is a valid index. It then determines if p is before or after the midpoint and inserts accordingly, updating pointers and size as needed.

Uploaded by

ShowPiece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views1 page

Double Link List Insert

This method inserts a new node with data x into a doubly linked list at position p. It first checks if p is a valid index. It then determines if p is before or after the midpoint and inserts accordingly, updating pointers and size as needed.

Uploaded by

ShowPiece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

int IntegerDoublyLinkedList::Insert(int x, int p)

{
if (p>CurrentSize || p<0)
{
return -1;
}
if (p < CurrentSize/2)
{
if (p==0)
{
DNode *N = new DNode ;
N->Data = x ;
N->Previous = NULL ;
N->Next = NULL ;
First = N ;
Last = N ;
CurrentSize ++ ;
}
else
{
DNode *N = new DNode ;
N->Data = x ;
DNode *Temp = First ;
for ( int i = 1 ; i < p ; i++ )
Temp = Temp->Next ;
N->Next = NULL ;
Temp->Next = N ;
N->Previous = Temp ;
Last = N ;
CurrentSize ++ ;
}
}
else
{
DNode *N = new DNode ;
N->Data = x ;
DNode *Temp = Last ;
for (int i =CurrentSize ; i<p ; i++)
Temp = Temp->Next ;
Temp->Next = N ;
N->Previous = Temp ;
N->Next = NULL ;
Last = N ;
CurrentSize ++ ;
}
return 0 ;
}

You might also like