Thread: Reverse a linked list, probably a simple fix.

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    Reverse a linked list, probably a simple fix.

    Hi everyone, I have coded a linked list that is to be reversed by directions of my instructor. I for some reason cannot get the reverse portion to print. I know it is something simple i missed. I do not get any compile errors. Could someone help me out? I really appreciate it. Thank You in advance.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int value;
        struct node *next;
    };
    
    struct node * reverse(struct node * original, struct node * reversed);
    struct node *add_to_list_1(struct node *list, int n);
    void traverse(struct node * first);
    struct node *delete_first(struct node *first);
    
    int main()
    {
        struct node * first = NULL;
        struct node * reversed = NULL;
    
        first = add_to_list_1(first, 15);
        first = add_to_list_1(first, 13);
        first = add_to_list_1(first, 11);
        first = add_to_list_1(first, 9);
        first = add_to_list_1(first, 7);
        first = add_to_list_1(first, 5);
    
        printf("Linked List:\n");
        traverse(first);
        return 0;
    
        first = reverse(first, reversed);
        printf("Linked List Reversed:\n");
        traverse(first);
        return;
    
    }
    
    struct node * reverse(struct node * first, struct node * reversed)
    {
        struct node *p;
        p = first;
        while (p!= NULL)
        {
            reversed = add_to_list_1(reversed, p-> value);;
            p = delete_first(p);
    
        }
        return reversed;
    }
    
    struct node *add_to_list_1(struct node *list, int n)
    {
        struct node *new_node;
    
        new_node = malloc(sizeof (struct node));
        new_node -> value = n;
        new_node -> next = list;
        return new_node;
    }
    
    void traverse(struct node * first)
    {
        struct node * p;
        p = first;
        while (p!= NULL)
        {
            printf("Value = %d\n", p->value);
            p = p->next;
    
    
        }
        printf("------------\n");
        return;
    }
    
    struct node *delete_first(struct node *first)
    {
    
        struct node *p;
        p = first;
        first = first->next;
        free (p);
        return first;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
        printf("Linked List:\n");
        traverse(first);
        return 0;
    
        first = reverse(first, reversed);
        printf("Linked List Reversed:\n");
        traverse(first);
        return;
    Try removing the return 0 from the middle of main.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by anduril462 View Post
    Try removing the return 0 from the middle of main.
    I did that but it was not the problem. Still haven't figured it out yet.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    removing that one line worked fine for me, and your list printed in reverse order just fine. Does removing that return 0 change the problem (i.e. seg fault, random order, etc)?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by anduril462 View Post
    removing that one line worked fine for me, and your list printed in reverse order just fine. Does removing that return 0 change the problem (i.e. seg fault, random order, etc)?
    Hehe, I just removed the 0, not the whole line. Thanks a lot!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sorry but in my opinion you are not exactly doing what was asked. You were most likely asked to reverse a list, however you are in fact destructively creating a reversed copy of a list.

    You do not malloc or free while simply reversing a list.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. how do i reverse a circular double linked list
    By kobra_swe in forum C Programming
    Replies: 13
    Last Post: 04-08-2008, 04:20 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM