# Simple solutions for complex problems on single linked list..

Discussion in 'C' started by dharmaraj.guru, Nov 6, 2007.

1. ### dharmaraj.guruNew Member

Joined:
Oct 23, 2007
Messages:
16
0
Trophy Points:
0
Listed here are few common complex operations of single linked list that is to be needed by certain applications. These operations should be done in single traversal for improved performance.

Here are few of them and their solutions..

1. Reverse the list.
2. Find n-th node from tail end.
3. Find middle node of the list.

1. Reverse the list
Logic :
a. Get each node from front end till last.
b. Add them into another list at front.​

2. Find n-th node from tail end.
a. Have two pointers pointer1 and pointer2.
b. Point all of them to head initilally.
c. Move only pointer1 for n nodes.
d. Move both pointers for the rest nodes of list.
e. At the end of traversal, pointer2 will be pointing to n-th node from tail.​

3. Find middle node of the list.
a. Have two pointers pointer1 and pointer2.
b. Point all of them to head initilally.
c. Move only pointer1 by one node.
d. Move pointer2 by two nodes till the end of list.
e. At the end of traversal, pointer1 will be pointing to middle node from tail.​

a. copy the contents of next node's data to current node's data.
b.Store the next node's next node at current node's link.​

In all cases, we have to take care of boundary conditions as well as memory leaks.
Here is the code..

Code:
```#include <stdio.h>

struct node
{
int data;
struct node* next;
};

// Functions for allocating/deallocating memory
struct node* create_node(int data);

// Utility functions that can be used in reverse_list
struct node* insert_at_front(struct node* head, struct node* new_node);
struct node* insert_at_end(struct node* head, struct node* new_node);

// Delete a node without having the head pointer..
void delete_this_node(struct node* cur_node);

// Find nth node from tail in single traversal..
struct node* find_nth_from_end(struct node* head, int n);

// Find middle node of the list in single traversal..

// Dump the list/node..
void dump_node(struct node* pNode);
void copy_node(struct node* src, struct node* dest);

// Main function
int main(void)
{
struct node *list = NULL, *temp_node = NULL;
int i = 0;

// Create list with 10 members...
for(i = 0; i <= 10; i++) {
temp_node = create_node(i);
list = insert_at_end(list, temp_node);
}

// Add 5 more in front...
for(i = 14; i >= 11; i--) {
temp_node = create_node(i);
list = insert_at_front(list, temp_node);
}

dump_list(list);

// To reverse a single linked list in single traversal.......
/*
list = reverse_list(list);
*/

// Delete a particular node without having the head pointer
/*
temp_node = list;

for(i = 0; i <= 15; i++) {

if(i == 4) {
delete_this_node(temp_node);
break;
}

temp_node = temp_node->next;
}
dump_list(list);
*/

// To find nth node from the end of list in single traversal....
/*
temp_node = find_nth_from_end(list, 10);

dump_node(temp_node);
*/

// To find the middle node of list in single traversal......

/*
temp_node = find_middle_node(list);

dump_node(temp_node);

list = reverse_list(list);

dump_list(list);

temp_node = find_middle_node(list);

dump_node(temp_node);
*/
return 0;
}

// Allocate memory and store the data
struct node* create_node(int data)
{
struct node *new_node = NULL;

new_node = (struct node*)malloc(sizeof(struct node));

new_node->data = data;
new_node->next = NULL;

return new_node;
}

// Deallocate the memory
void free_this_node(struct node* pNode)
{
free(pNode);
pNode = NULL;

return;
}

// Insert a node at front end
struct node* insert_at_front(struct node* head, struct node* new_node)
{
if(new_node) {
return new_node;
}
else
return NULL;
}

// Insert a node at tail end
struct node* insert_at_end(struct node* head, struct node* new_node)
{
struct node *temp = NULL;

while(temp->next)
temp = temp->next;

temp->next = new_node;
}
else {
}

}

// Remove a node from front end..
{
}
else
return NULL;
}

// Reverse the list in single traversal..
{
struct node *result = NULL, *temp_head = NULL, *temp = NULL;

temp->next = NULL;

result = insert_at_front(result, temp);
}

return result;
}

// Delete a node without the help of head node..
void delete_this_node(struct node* cur_node)
{
struct node *temp = NULL;
if(cur_node && cur_node->next) {

copy_node(cur_node, cur_node->next);
temp = cur_node->next;
cur_node->next = cur_node->next->next;

free_this_node(temp);
}
else if(cur_node) {
free_this_node(cur_node);
}
else
return;
}

// Find n-th node from tail end in single traversal...
struct node* find_nth_from_end(struct node *head, int n)
{
struct node *temp = NULL, *result = NULL, *temp_head = NULL;
int i = 0;

for(i = 0; i < n; i++) {
printf("Out of Range\n");
return NULL;
}
temp = temp->next;
}

temp = temp->next;
result = result->next;
}

return result;
}

// Find middle node of list in single traversal...
{
struct node *temp_head = NULL, *temp = NULL, *result = NULL;

while(temp && temp->next) {

if(temp->next->next) {
temp = temp->next->next;
result = result->next;
}
else
temp = temp->next;
}

return result;
}

// Dump the list...
{
struct node *temp = NULL;

printf("\n");

while(temp) {
printf("%d ", temp->data);
temp = temp->next;
}
}
else {
printf("Empty List");
}

printf("\n");
}

// Copy data in b/w two nodes...
void copy_node(struct node* src, struct node* dest)
{
src->data = dest->data;

return;
}

// Dump a node
void dump_node(struct node* pNode)
{
if(pNode)
printf("\nNode details : Addr : %x, data : %d\n", pNode, pNode->data);
else
printf("\n Node is NULL \n");

return;
}
```
Uncomment the relevant snippet from main for testing it.

Joined:
Jul 12, 2004
Messages:
15,285
364
Trophy Points:
83
3. ### rc410New Member

Joined:
Nov 30, 2007
Messages:
3
0
Trophy Points:
0
Well this is good program

Joined:
Jul 12, 2004
Messages:
15,285
364
Trophy Points:
83
5. ### ducuytranBanned

Joined:
Dec 16, 2007
Messages:
5
0
Trophy Points:
0
Occupation:
student
Location:
Ác Nhân Cốc
SO Great! It's a part of my syllabus. Thank you, thankz so much!

6. ### strsubNew Member

Joined:
Aug 30, 2007
Messages:
2
0
Trophy Points:
0
Location:
indonesia
very interesting

7. ### mr.anandcNew Member

Joined:
May 6, 2008
Messages:
18
0
Trophy Points:
0
Interesting.. very simple solutions...

8. ### vijay_visanaNew Member

Joined:
Aug 3, 2008
Messages:
1
0
Trophy Points:
0
Occupation:
System Analyst
Location:
Uk
here is one more way to reverse singly linked list
{
PNODE pTop = NULL;
if( pNode->next)
else
pTop = pNode;
pNode->next=prevNode;
return pTop;
}

one more question
how to reverse block in singly linked list
like
1->2->3->4->5->6->7->8->9->10->11->12
let us take block size as 3
so reversed list should be like this
3->2->1->6->5->4->9->8->7->12->11->10

vijay