Following code has the start of a class creating a doubly-linked list (but NOT i
ID: 3863045 • Letter: F
Question
Following code has the start of a class creating a doubly-linked list (but NOT implementing List). You will need to complete two of the methods which are needed to add data to the list:
addBetween() -- should add a new Entry ("node") containing elem so that it appears BETWEEN prior and follower
addToFront() -- should add a new Entry ("node") containing elem so that it appears AT THE HEAD of the list
______________________________________________________________________________________________________
Explanation / Answer
public void addBetween(Entry prior, E elem, Entry follower)
{
/*1. check if the given prev_node is NULL */
if (Entry prior== NULL)
{
printf("the given previous node cannot be NULL"
return;
}
/* 2. allocate new node */
struct node* new_node =(struct node*) malloc(
/* 3. put in the data */
new_node->data = E elem;
/* 4. Make next of new node as next of prev_node */
new_node->next = Entry prior->next;
/* 5. Make the next of prev_node as new_node */
Entry prior->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = Entry prior;
/* 7. Change previous of new_node's next node */
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
private void addToFront(E elem)
{