Search This Blog

Sunday, September 21, 2014

WAP to add all nodes value in a given SLL between the range from last i.e add all nodes form 2nd to 4th position from last.

1:  int sum_of_nodes_from_last(int pos1,int pos2)  
2:  {  
3:    int sum = 0;  
4:    int i;  
5:    list_t *temp = NULL;  
6:    for(i=1,temp = start;temp;temp = temp->next,i++);  
7:    pos1 = i-pos1;  
8:    pos2 = i-pos2;  
9:    printf("i = %d\n",i);  
10:    for(i=1,temp = start;temp;temp = temp->next,i++)  
11:    {  
12:      if ( i >= pos2 && i <= pos1)  
13:      {  
14:        sum += temp->data;  
15:      }  
16:    }  
17:    return sum;  
18:  }  

No comments:

Post a Comment