The following code constructs a doubly linked list of integers. The next field points to the next node in the list, while the previous field points to the previous node. This code constructs the list:
NULL <== 0 <==> 1 <==> 2 <==> 3 <==> 4 ==> NULL
Which of the following code snippets will insert the value 101 into the list between the nodes with 2 and 3? e.g. the list should contain:
NULL <== 0 <==> 1 <==> 2 <==> 101 <==> 3 <==> 4 ==> NULL
class IntNode
{
public:
int num;
IntNode *next, *previous;
IntNode();
IntNode(int n);
};
IntNode::IntNode()
{
next = NULL;
previous = NULL;
num = -1;
}
IntNode::IntNode(int n)
{
next = NULL;
previous = NULL;
num = n;
}
// Main class
int main()
{
IntNode *head = NULL, *tail = NULL;
IntNode *temp = NULL;
// Construct linked list
for (int i=0; i<5; i++) {
temp = new IntNode(i);
if (head==NULL) {
head = temp;
temp->previous = tail;
tail = temp;
}
else {
tail->next = temp;
temp->previous = tail;
tail = temp;
}
}
// CODE TO INSERT WOULD GO HERE
// Destroy list
while (head != NULL) {
temp = head;
head = head->next;
delete temp;
}
return 0;
}