//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 addIntToEnd0fList LinkedList * list, int value) { assert (list! = NULL);//if list is NULL, we can do nothing. Node *pi//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. } }
Expert Answer
Here is the solution to this incomplete code
void addIntToEndOfList(LinkedList *list,int value)
{
assert(list!=NULL);
Node *p;
Node *head;
Node *tail;
node *newnode = (node*)malloc(sizeof(node));
p->newnode;
p->value = val;
p->next = NULL;
if(list->head == NULL)
{
head-> p;
tail->p;
}
else
{
node *current = head;
while(current->next != NULL)
{
if(current->next == NULL)
{
current->next = p;
printf(” p node added at the end of the list”);
}
current = current->next;
}
}
}