Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the code for addIntToEndOfList and addIntToStartOfList. There are clues in

ID: 3868283 • Letter: W

Question

Write the code for addIntToEndOfList and addIntToStartOfList. There are clues in the file to help you.

// Precondition: A valid linked-list that may possibly be empty
// and an integer value
// Postcondition: A new node with provided value is added to the
// the end of the list. The head and tail pointers are appropriately
// updated
void addIntToEndOfList(LinkedList *list, int value) {
assert(list!=NULL); // if list is NULL, we can do nothing.

Node *p; // temporary pointer

// TODO:
// (1) Allocate a new node. p will point to it.

p = NULL; // THIS IS PLACE-HOLDER LINE OF CODE. DELETE IT AND REPLACE IT.

// (2) Set p's data field to the value passed in
  
// (3) Set p's next field to NULL


if (list->head == NULL) {

// (4) Make both head and tail of this list point to p
  
  
} else {

// Add p at the end of the list.   

// (5) The current node at the tail? Make it point to p instead of NULL

// (6) Make the tail of the list be p now.

}

}

// Precondition: A valid linked-list on the heap and an integer value
// Postcondition: A new node is created with provided value and is
// added to the start of the list. The head and tail pointers are
// appropriately updated
void addIntToStartOfList(LinkedList *list, int value) {
assert(list!=NULL); // if list is NULL, we can do nothing.

// Add code for this.
// HINTS:
// You will need to allocate a new Node.
// You will need two cases just as in addIntToEndOfList,
// one for when list->head is NULL and another for when it is not.
// You need to consider how to make sure that list->head
// so that it points to the new node that you allocated.
// And you will need to make sure that when you are done,
// that if the new node is now the ONLY thing on the list,
// that tail points to it also,
// and that the new node is pointing to NULL.
// Otherwise, you'll need to be sure that when you are done
// the head points to the new node you added, and that
// the new node points to the node that *used* to be the head.
// The order in which you do things matters.

}

Explanation / Answer

// Below is the code snippet in which you can you can add node at any position in the list

/* Insert Node at a given position in a linked list The linked list will not be empty and position will always be valid First element in the linked list is at position 0 Node is defined as struct Node { int data; struct Node *next; } */

Node* InsertNth(Node *head, int data, int position)

{

// Complete this method only

// Do not write main function.

Node *cur = new Node,*temp = new Node;

temp->data = data; temp->next = NULL;

if(head == NULL)

{

head = temp;

}

else

{

int i=0; cur = head;

while(cur!=NULL)

{

if(position == 0)

{

temp->next = cur;

head= temp;

}

else if(i==position-1)

{

temp->next = cur->next;

cur ->next = temp;

}

else

{ }i++;

cur= cur->next;

}

}

return head;

}