Supply the code for the three functions: (Complete the code) #include \"orderedL
ID: 3554057 • Letter: S
Question
Supply the code for the three functions: (Complete the code)
#include "orderedList.h"
Node *orderedInsert(Node *p, int newval)
/* Allocates a new Node with data value newval
and inserts into the ordered list with
first node pointer p in such a way that the
data values in the modified list are in
nondecreasing order as the list is traversed.
*/
{
}
void printList(Node *p)
/* Prints the data values in the list with
first node pointer p from first to last,
with a space between successive values.
Prints a newline at the end of the list.
*/
{
}
void clearList(Node **p)
/* Deletes all the nodes in the list with
first node pointer *p, resulting in *p
having value NULL. Note that we are passing
a pointer by address so we can modify that
pointer.
*/
{
}
Explanation / Answer
#include "orderedList.h" #include #include #include Node *orderedInsert(Node *p, int newval) /* Allocates a new Node with data value newval and inserts into the ordered list with first node pointer p in such a way that the data values in the modified list are in nondecreasing order as the list is traversed. */ { Node *temp = malloc(sizeof(Node)); assert(temp != NULL); temp->data = newval; if(p == NULL) { p = temp; p->next = NULL; return p; } Node *prev; prev = p; while(prev->next != NULL && prev->next->data next; } temp->next = prev->next; prev->next = temp; return p; } void printList(Node *p) /* Prints the data values in the list with first node pointer p from first to last, with a space between successive values. Prints a newline at the end of the list. */ { Node *prev; prev = p; if(p == NULL) return; printf("%d ", prev->data); while(prev ->next != NULL) { prev = prev->next; printf("%d ", prev->data); if(prev == NULL) break; } printf(" "); return; } void clearList(Node **p) /* Deletes all the nodes in the list with first node pointer *p, resulting in *p having value NULL. Note that we are passing a pointer by address so we can modify that pointer. */ { Node *temp = *p; while(*p != NULL) { *p = (*p)->next; free(temp); temp = *p; } return; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.