You are on page 1of 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