In C Language Assume that the following type declaration is

In C Language

Assume that the following type declaration is available: typedef struct node {int key; struct node *next;} NODE, *PN0DE; Notice that each node of type NODE contains an integer key value and a pointer to the next node of a list. You are required to write a function compute with the following header: int compute (PNODE head) Here, the parameter head points to the first node of a list each of whose nodes is of type NODE. The specifications for the function compute are as follows. If the list is empty, then the function must return the value 0. If the list has only one node, then function must return the key value stored in that node. If the list has two or more nodes, then function must return the sum of the key values stored in the last two nodes of the list. Your need to show only the C code for the above function. You may use magic numbers and there is no need to include comments in your code.

Solution

int compute(PNODE head)
{
int result;
if(head==NULL)
result = 0;
if(head->next == NULL)
result = head->key;
else
{
PNODE prev;
PNODE temp = head;
  
while(temp->next != NULL)
{
prev = temp;
temp = temp->next;
}
  
result = prev->key+temp->key;
  
}
  
return result;
}

In C Language Assume that the following type declaration is available: typedef struct node {int key; struct node *next;} NODE, *PN0DE; Notice that each node of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site