A linked list can be defined as a self-referential structure that has a pointer
ID: 3726504 • Letter: A
Question
A linked list can be defined as a self-referential structure that has a pointer to a structure as a member element as shown below: struct node char data 40]; struct node next Here the struct template node is a linked list which has a member variable named data of char array type and a pointer to a variable named next of type node. In the attached test file named “LinkedListString test.c", an empty node "head, is created. struct node* head = NULL; At the beginning of insertion of any node, the list looked like follows: head NULL After insertion of seven nodes, the linked list now looks like the following: Stuart To You need to implement the following functions: Function to count the number of elements in the linked list int listCount(struct node *head){ int count = 0; return count;} 2 pts Function to reverse the linked list void reverseList(struct node** head ref)a 6 pts *Function to delete a particular element in a linked list*/ void deleteElement(struct node **currP, char *value) *Function to delete all elements in a Linked List void listAllDelete(struct node **currP) ts 4 pts As you can see, listCount will count the number of nodes in the linked list, reverseList will reverse the nodes in the linked list, deleteElement will delete a particular node with the given value for data, and lastly, listAllDelete will delete all the nodes in a lineked list. After you complete writing the functions as mentioned above and uncomment the different function calls in main, when you run the program, it gives the following output:Explanation / Answer
int listCount(struct node *head)
{
int count = 0;
// point trab to the head of the list
struct node *trav = head;
// iterate through the list
while(trav)
{
count++;
// go to the next node
trav = trav->next;
}
return count;
}
void listAllDelete( struct node **currP )
{
while( *currP )
{
// point current node by temp
struct node *temp = *currP;
// move to next node
currP = currP->next;
// free the memory of the node
free(temp);
}
}
void deleteElement( struct node **head_ref, char *value )
{
// if list is empty
if( *head_ref == NULL )
return;
// if the node to be deleted is the head node
if( !strcmp((*head_ref)->data, value) )
// make second node the new head
*head_ref = (*head_ref)->next;
else
{
// point trav to the head of the list
struct node *trav = *head_ref;
// loop through the list
while(trav->next)
{
// if the next node is the node to be deleted
if( !strcmp(trav>data, value) )
{
// remove the node
trav->next = trav->next->next;
return;
}
// move to the next node
trav = trav->next;
}
}
}
void reverseList(struct node **head_ref)
{
// point to the previous node
struct node* pre = NULL;
// point trav to the head of the list
struct node* trav = *head_ref;
// point to the next node
struct node* forw = NULL;
// loop through the list
while (trav)
{ate the pointers
// upd
forw = trav->next;
trav->next = pre;
pre = trav;
trav = forw;
}
// update the head of the list
*head_ref = pre;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.