Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Simple solutions for complex problems on single linked list.. (http://www.go4expert.com/articles/simple-solutions-complex-single-linked-t7198/)

 dharmaraj.guru 6Nov2007 12:13

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.

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

`#include <stdio.h>struct node{    int data;    struct node* next;};// Functions for allocating/deallocating memorystruct node* create_node(int data);// Utility functions that can be used in reverse_liststruct 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 functionint 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 datastruct 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 memoryvoid free_this_node(struct node* pNode){    free(pNode);    pNode = NULL;    return;}// Insert a node at front endstruct 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 endstruct 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 nodevoid 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.

 shabbir 1Dec2007 17:58

Re: Simple solutions for complex problems on single linked list..

I have reported the article for Nominate your favorite article of the month for November 2007. Add your nominations as well.

 rc410 3Dec2007 14:57

Re: Simple solutions for complex problems on single linked list..

Well this is good program

 shabbir 17Dec2007 10:04

Re: Simple solutions for complex problems on single linked list..

Vote for the article for Article of the month for November 2007

 ducuytran 19Dec2007 08:09

Re: Simple solutions for complex problems on single linked list..

SO Great! It's a part of my syllabus. Thank you, thankz so much!

 strsub 24Dec2007 18:36

Re: Simple solutions for complex problems on single linked list..

:D very interesting

 mr.anandc 6May2008 16:47

Re: Simple solutions for complex problems on single linked list..

Interesting.. very simple solutions...

 vijay_visana 4Aug2008 04:52

Re: Simple solutions for complex problems on single linked list..

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

 All times are GMT +5.5. The time now is 04:10.