Thread: Traverse linked list backwards

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Traverse linked list backwards

    I've always set my struct definitions to something along the lines of

    Code:
    typedef struct node {
        char *text;
        struct node *next;
    } Line;
    But now I want to also add a previous pointer so that I can traverse the link not only forwards, but backwards:

    Code:
    typedef struct node {
        char *text;
        struct node *next;
        struct node *prev;
    } Line;
    The code I have for typical forward traversals works fine, but my new code is now:

    Code:
    Line *makeNode(char *text) {
        Line *new = NULL;
        
        new = malloc(sizeof(Line));
        if (new == NULL) {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        }
        new->text = strdup(text);
        new->next = NULL;
        new->prev = NULL;
        
        return new;
    }
    
    Line *joinList(Line *head1, Line *head2) {
        Line *cur = head1; //Line *cur = NULL; cur = head1;
        
        if (cur == NULL) {
            head1 = head2;
        } else {
            while (cur->next != NULL) {
                cur = cur->next;
            }
            cur->next = head2;
            head2->prev = cur;
        }
        
        return head1;
    }
    and when I test it with

    Code:
            for (cur = head; cur != NULL; cur = cur->next) {
                printf("%s", cur->text);
            }
            cur = cur->prev;
            while (cur != NULL) {
                printf("%s", cur->text);
                cur = cur->prev;
            }
    It prints out the list in the forward direction, but seg faults on the way back. What am I doing wrong?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    On line 4 of your last listing cur->prev will segfault since cur is NULL as per the condition of the previous for loop.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by oogabooga View Post
    On line 4 of your last listing cur->prev will segfault since cur is NULL as per the condition of the previous for loop.
    Oh yes! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List - Print Backwards
    By skier21 in forum C Programming
    Replies: 9
    Last Post: 07-27-2012, 06:11 PM
  2. Linked list printing out backwards
    By chickenlittle in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2011, 07:41 AM
  3. Replies: 5
    Last Post: 12-11-2010, 12:21 AM
  4. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  5. Traverse a linked list from end to start
    By pjodk in forum C Programming
    Replies: 4
    Last Post: 04-07-2002, 06:09 PM