Thread: Is it possible to 'reverse' a singly linked list?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Is it possible to 'reverse' a singly linked list?

    As the title says, I have a singly linked list which has stored all the elements in backwards, would I have to create a new List and restore the data into that from the original list?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    No. Just reverse the list. ( not the data, but the pointers)

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    How?

    Sorry I'm a bit weak when it comes to Lists.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Do you truly wish to physically reverse the actual list or are you simply talking about displaying the data in a reverse order? If you just wish to display the data in reverse order you might not have to do anything to the list itself... a simple way would be to write a recursive function that call itself until it gets to the end of the list and then does a print statement. If you really do need to reverse the list, then you can do basically the same thing... write a recursive function that loops through the list until the end and then calls the insert function to take the current node's value and insert it into a second (new) list.

    Or you can fix the problem at the source and not store the data in reverse order. Your insert function is storing items by creating a new node and setting the new node's next pointer to the start of the existing node. This results in a backwards list. What you want to do is set the last node's next pointer of the existing list to be equal to the new node you've just created (remember to set the new node's next pointer to NULL).

    You can also use a double-linked list instead of a single-linked list (previous and next pointers). Then it wouldn't really matter if the list was forwards or backwards... just start at either end and loop through until you are done. It's not to much extra work maintaining the additional pointers.

    Those are some ideas.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Quote Originally Posted by hk_mp5kpdw
    Or you can fix the problem at the source and not store the data in reverse order. Your insert function is storing items by creating a new node and setting the new node's next pointer to the start of the existing node. This results in a backwards list. What you want to do is set the last node's next pointer of the existing list to be equal to the new node you've just created (remember to set the new node's next pointer to NULL).
    .
    Ok I opted for this approach but am getting confused. Is it possible you can help me implement it in this code:

    Code:
    List *insertlist(char word, List * b)        
    {
      List *c = calloc(1, sizeof(List));
      c->x = word; 
     c->next = b;
      return c;
    }

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Pop the old list and push it onto a new list.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    No it can't be done. Is the short answer, that why doubley linked lists
    are used.
    The sensible thing to do would be to create a double linkedl ist in the
    first place, rather than stand on you head and jump through hoops to
    achieve the same thing.

  8. #8
    Registered User
    Join Date
    May 2006
    Location
    Troy
    Posts
    14
    Quote Originally Posted by esbo
    No it can't be done.
    This is not true; reversing a singly-linked list is simple to do.

    Code:
    node * reverse_in_place(node *list) {
        node *tail = NULL;
        node *tmp;
        while (list) {
            tmp = list->next;
            list->next = tail;
            list = tmp;
        }
        return tail;
    }
    Last edited by Rash; 05-08-2006 at 03:49 PM.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Can somebody help me just store the data in the correct order instead of messing around with it later on?

    Also another question, how do I access the end of the List, ie the tail?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make pointers that point to them. As far as making sure it's in the "correct order", simply pick how you want it stored, and break it down into rules to check when inserting. Simply loop through the list, starting at the head, and insert it wherever you need to.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Also another question, how do I access the end of the List, ie the tail?
    u can have a tail pointer which points to the end of the list or u have to traverse all the way from the head to the end of the list thats is

    Code:
    struct .... *temp;
    
    temp = head;
    
    while(temp->link != NULL)
    temp = temp->link;
    ssharish2005

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Rash
    This is not true; reversing a singly-linked list is simple to do.

    Code:
    node * reverse_in_place(node *list) {
        node *tail = NULL;
        node *tmp;
        while (list) {
            tmp = list->next;
            list->next = tail;
            list = tmp;
        }
        return tail;
    }
    If you only know the data from one element in the list
    you can never go back up the list to the start.
    If you wanted to go backwards you should make it a double
    linked list.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    How do you create a pointer to the tail?

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    there are two ways that u can this tail pointer to be.
    1. u can make the tail to grow towards the right as u keep on adding the node to the end of the list and updating the tail ppointer to the new node which is just been appended. keeping head const

    2. u can have head growing towards the left where the new node is added at the font of the list and updating the head pointer to point to the new node which is just been added. keeping tail const

    ssharish2005
    Last edited by ssharish2005; 05-08-2006 at 04:43 PM.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    eh??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM