Please Help! A sorted singly-linked list is another kind of linked list in which
ID: 653789 • Letter: P
Question
Please Help!
A sorted singly-linked list is another kind of linked list in which the nodes are kept in increasing sorted order based on the data value stored at the nodes. The following diagram illustrates this. b) Impl the function erase which removes an existing element from the sorted ement singly-linked list. The function retums true is the element is found and remove or false d, Empty List otherwise. The functions must keep the sorted order in the list. Recall dealing with the case in which the element to erase is at the header. See Figure 2 on the Appendix to see the result of an erase on alist. 10 pts One Element List templateExplanation / Answer
struct node
{
int data;
struct node *next;
}*head;
int erase ( int num)
{
struct node *temp, *prev;
temp=head;
while (temp!=NULL)
{
if (temp->data==num)
{
if (temp==head)
{
head=temp->next;
free (temp);
return 1;
}
else
{
prev->next=temp->next;
free (temp);
return 1;
}
}
else
{
prev=temp;
temp= temp->next;
}
}
return 0;
}
void insert ( int num )
{
struct node *temp;
temp=( struct node *) malloc ( sizeof ( struct node));
temp->data=num;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}