Sunday, February 14, 2010

Simple Solutions for Complex Problems on Single Linked List..

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.
4. Delete a node whose address is known, without having the head node's address.

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.

4. Delete a node without having the head node's address

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

#include

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);

struct node* remove_from_front(struct node* head);
struct node* reverse_list(struct node* head);

// 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..
struct node* find_middle_node(struct node* head);

// Dump the list/node..
void dump_list(struct node* head);
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) {
new_node->next = head;
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;

if(head) {

temp = head;

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

temp->next = new_node;
}
else {
head = new_node;
}

return head;
}

// Remove a node from front end..
struct node* remove_from_front(struct node* head)
{
if(head) {
head = head->next;
return head;
}
else
return NULL;
}

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

temp_head = head;

while(temp_head) {

temp = temp_head;

temp_head = remove_from_front(temp_head);

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;

result = temp_head = temp = head;

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

while(temp_head) {
temp_head = temp_head->next;
temp = temp->next;
result = result->next;
}

return result;
}

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

temp_head = temp = result = head;

while(temp && temp->next) {

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

return result;
}

// Dump the list...
void dump_list(struct node* head)
{
struct node *temp = NULL;

printf("\n");

if(head) {

temp = head;

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.

No comments: