Search This Blog

Sunday, September 21, 2014

WAP to detect loop in a singly linked list

1:  void loop_detection()  
2:  {  
3:    list_t *slow = start;  
4:    list_t *fast = start;  
5:    while(slow != NULL && fast != NULL && fast->next != NULL)  
6:    {  
7:      slow = slow->next;  
8:      fast = fast->next->next;  
9:      if ( slow == fast)  
10:      {  
11:        printf("Loop detected\n");  
12:        return;  
13:      }  
14:    }  
15:    printf("No loop\n");  
16:    return;  
17:  }  

Find middle node of given singly linked list

1:  list_t * middle_node()  
2:  {  
3:    list_t *slow = start;  
4:    list_t *fast = start;  
5:    while(fast != NULL && fast->next != NULL)  
6:    {  
7:      slow = slow->next;  
8:      fast = fast->next->next;  
9:    }  
10:    printf("Middle Node is : %d\n",slow->data);  
11:    return slow;  
12:  }  

WAP to find common data position in two different given Singly Linked lists.

1:  void find_common_nodes_position(list_t *list1,list_t *list2)  
2:  {  
3:    int pos1=0,pos2=0;  
4:    list_t *temp1 = NULL;  
5:    list_t *temp2 = NULL;  
6:    for(pos1=1,temp1=list1;temp1;temp1=temp1->next,pos1++)  
7:    {  
8:      for(pos2=1,temp2 = list2;temp2;temp2 = temp2->next,pos2++)  
9:      {  
10:        if ( temp1->data == temp2->data)  
11:        {  
12:          printf("Data %d common at %d in list1 and %d at list2\n",  
13:                          temp1->data,pos1,pos2);  
14:        }  
15:      }  
16:    }  
17:    return;  
18:  }  

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:  }  

WAP to add all node value within a given range in SLL

1:  int sum_of_nodes(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:    {  
8:      if ( i >= pos1 && i <= pos2)  
9:      {  
10:        sum += temp->data;  
11:      }  
12:    }  
13:    return sum;  
14:  }  

WAP to find nth node from the last in a singly linked list

list_t *nth_node_from_last(int pos_from_last)
{
    int len_of_ll = 0;
    list_t *temp = NULL;
    for(len_of_ll = 0,temp = start;temp;temp=temp->next,len_of_ll++);
    // Length of LL is obtained
    len_of_ll -= pos_from_last;
    if ( len_of_ll <= 0)
    {
        printf("Position from last is out side the linked list range");
        return NULL;
    }
    for(temp = start;len_of_ll;temp=temp->next,len_of_ll--);
    return temp;
}

WAP to delete a node of singly linked list if only address on that node is given

1:  void delete_node(list_t *node)  
2:  {  
3:    list_t *temp = node;  
4:    if (NULL == temp->next)  
5:    {  
6:      // Last node  
7:      free(temp);  
8:      return;  
9:    }  
10:    // Any node other than last  
11:    temp = temp->next;  
12:    node->data = temp->data;  
13:    node->next = temp->next;  
14:    free(temp);  
15:    return;  
16:  }  

WAP to delete node in singly linked list when position and start pointer is given. Without using extra pointer

1:  typedef struct list_  
2:  {  
3:     struct list_ *next;  
4:     int data;  
5:  }list_t;  
6:  void delete_list(int pos,list_t *node)  
7:  {  
8:    if ( NULL == node)  
9:    {  
10:      printf("Position not found\n");  
11:      return;  
12:    }  
13:    if ( pos == 1)  
14:    {  
15:      while ( node->next->next != NULL )  
16:      {  
17:        node->data = node->next->data;  
18:        node=node->next;  
19:      }  
20:      node->data = node->next->data;  
21:      free(node->next);  
22:      node->next = NULL;  
23:      return;  
24:    }  
25:    delete_list(--pos,node->next);  
26:    return;  
27:  }  
28:  main()  
29:  {  
30:      delete_list(pos,start);  
31:  }   

WAP to reverse a singly linked list

1:  typedef struct abc  
2:  {  
3:    int a;  
4:    struct abc *next;  
5:  }abc;  
6:  M-1 : Using three pointers  
7:  void revrse_list()  
8:  {  
9:    abc *temp = NULL,*temp1 = NULL, *temp2 = NULL;  
10:    temp = start;  
11:    temp1 = temp->next;  
12:    temp2 = temp1->next;  
13:    temp->next = NULL;  
14:    while(temp1->next != NULL)  
15:    {  
16:      temp = temp1;  
17:      temp1 = temp2;  
18:      temp2 = temp2->next;  
19:      temp1->next = temp;  
20:    }  
21:    start = temp1;  
22:  }  
23:  M-2 : Using recursive  
24:  abc *reverse_list(abc *node)  
25:  {  
26:    abc *temp = NULL;  
27:    if ( node->next == NULL)  
28:    {  
29:      start = node;  
30:      return node;  
31:    }  
32:    temp = reverse_list(node->next);  
33:    temp->next = node;  
34:    return node;  
35:  }  
36:  main()  
37:  {  
38:    abc *temp = NULL;  
39:    temp = reverse_list(start);  
40:    temp->next = NULL;  
41:  }