top of page
Writer's pictureApurba Paul

Doubly Linked List-Part 2

//Function to Insert a node at the end of the doubly linked list

void add_node(int data)

{

Node *newptr=NULL;

newptr=(Node*)malloc(sizeof(Node));

newptr->info=data;

newptr->rptr=NULL;

if(last==NULL)

{

newptr->lptr=NULL;

start=last=newptr;

}

else

{

last->rptr=newptr;

newptr->lptr=last;

last=newptr;

}

}

_______________________________________________________________________________

//Function to Insert a node at the desired position(excluding the first and the last //position ) of the doubly linked list

void add_node_desired(int pos, int data)

{

int x=1;

Node *newptr=NULL,*move=start;

newptr=(Node*)malloc(sizeof(Node));

newptr->info=data;

newptr->rptr=NULL;

while(x<pos)

{

move=move->rptr;

x++;

}


newptr->rptr=move->rptr;

newptr->lptr=move;

move->rptr->lptr=newptr;

move->rptr=newptr;

}

13 views0 comments

Recent Posts

See All

Comments


bottom of page