Insert a node in Doubly linked list at given position
PROBLEM :
Given a doubly linked list, a position p and and integer x your task is to add a new node with value x at position just after pth node in the doubly linked list .
In this function problem, the function addNode takes three argument: Address of the head of the linked list, the position p and integer x where the node is to be added .
There are multiple test cases. For each test case, this function will be called individually.
Note: The first node is considered as node no 0.
Example
Input:
The first line contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of 3 lines. First line of each test case contains an integer N denoting the size of the linked list. Second line of each test case consists of 'N' space separated integers denoting the elements of the linked list.The third line contains two integers x and p
Output:
Prints the nodes of the linked list .
Constraints:
1<=T<=200
1<=N<=200
Example:
Input:
2
3
2 4 5
2 6
4
1 2 3 4
0 44
Output:
2 4 5 6
44 1 2 3 4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node of a doubly linked list */
/*
struct node
{
int data;
struct node *next; // Pointer to next node in DLL
struct node *prev; // Pointer to previous node in DLL
}; */
void addNode(struct node **head_ref,int pos,int data)
{
struct node *temp ,*ptr ;
temp=(struct node*)malloc(sizeof(struct node*)) ;
temp->data=data ;
temp->next=NULL ;
temp->prev=NULL ;
if(pos==0)
{
temp->next=(*head_ref) ;
if((*head_ref)!= NULL)
(*head_ref)->prev=temp ;
(*head_ref)=temp ;
return ;
}
ptr=(*head_ref) ;
while(pos!=0)
{
ptr=ptr->next ;
pos=pos-1 ;
}
temp->next=ptr->next ;
if(ptr->next)
ptr->next->prev=temp ;
temp->prev=ptr ;
ptr->next=temp ;
}
}
---------------------------------------------------------------------------------
Given a doubly linked list, a position p and and integer x your task is to add a new node with value x at position just after pth node in the doubly linked list .
In this function problem, the function addNode takes three argument: Address of the head of the linked list, the position p and integer x where the node is to be added .
There are multiple test cases. For each test case, this function will be called individually.
Note: The first node is considered as node no 0.
Example
Input:
The first line contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of 3 lines. First line of each test case contains an integer N denoting the size of the linked list. Second line of each test case consists of 'N' space separated integers denoting the elements of the linked list.The third line contains two integers x and p
Output:
Prints the nodes of the linked list .
Constraints:
1<=T<=200
1<=N<=200
Example:
Input:
2
3
2 4 5
2 6
4
1 2 3 4
0 44
Output:
2 4 5 6
44 1 2 3 4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node of a doubly linked list */
/*
struct node
{
int data;
struct node *next; // Pointer to next node in DLL
struct node *prev; // Pointer to previous node in DLL
}; */
void addNode(struct node **head_ref,int pos,int data)
{
struct node *temp ,*ptr ;
temp=(struct node*)malloc(sizeof(struct node*)) ;
temp->data=data ;
temp->next=NULL ;
temp->prev=NULL ;
if(pos==0)
{
temp->next=(*head_ref) ;
if((*head_ref)!= NULL)
(*head_ref)->prev=temp ;
(*head_ref)=temp ;
return ;
}
ptr=(*head_ref) ;
while(pos!=0)
{
ptr=ptr->next ;
pos=pos-1 ;
}
temp->next=ptr->next ;
if(ptr->next)
ptr->next->prev=temp ;
temp->prev=ptr ;
ptr->next=temp ;
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment