In: Computer Science
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linked list with a new node and new data. Assume that the list's head pointer is called head_ptr and the data for the new node is called entry
public boolean insertInMiddle(int data){
boolean isInserted = false;
Node node = new Node(data);
Node temp = head;
index = 10;
int i=0;
if(index >= 0 && index <= 20)){
isInserted = true;
if(index == 0){
if(head_ptr !=null){
node.nextNode = head_ptr;
head_ptr.prevNode = node;
head_ptr = node;
}else{
head_ptr = node;
tail=node;
}
}else{
while(i<index){
temp = temp.nextNode;
i++;
}
if(temp == null){
node.nextNode = temp;
node.prevNode = tail;
node.prevNode.nextNode = node;
tail=node;
}else{
node.nextNode = temp;
node.prevNode = temp.prevNode;
temp.prevNode = node;
node.prevNode.nextNode = node;
}
}
}
return isInserted;
}