Thread: i want to print my linked list in the reverse order please help

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    i want to print my linked list in the reverse order please help

    i want to print my list in the reverse..ie..the last item first and first one last..
    i am new to this ..i have created the list and printed it but couldnt do that in reverse order ..please help
    Code:
    #include<alloc.h>
    #define NULL 0
    
    
    
    struct linked_list
    {
    int number;
    struct linked_list *next;
    };
    
    typedef linked_list node;
    
    void main()
    {
     void create(node *);
     void print(node *);
    
     clrscr();
     node *head;
     head=(node*)malloc(sizeof(node));
     create(head);
     print(head);
     getch();
     }
    
     void create(node *p)
     {
     printf("Enter the number : \n");
     scanf("%d",&p->number);
     if(p->number==999)
          {
           p->next=NULL;
          }
     else
         {
          p->next=(node*)malloc(sizeof(node));
          create(p->next);
         }
     return;
      }
    
    void print(node *p)
    {
     if(p->next!=NULL)
       {
        printf("%d-->",p->number);
        print(p->next);
        }
    
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You could just print to a string and the output the reverse of that string.

    Check out sprintf().
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    recursive will be your best friend

    try calling the function print before you printf the actual value of the list

    here:

    Code:
    void print(node *p)
    {
     if(p->next!=NULL)
       {
        printf("%d-->",p->number);
        print(p->next);
        }
    
    }

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    go with the doubly linked list. If u use doubly linked list you struct would look like
    Code:
    struct linked_list
    {
    struct linked_list *Back;   // <-- this will hold the address of the previous node from the current node
    int number;
    struct linked_list *front;   // <- This will hold the address of the preceding node from the current node
    };
    ssharish2005

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's no need to use recursion if you don't need it. A loop would do as well.
    Code:
    node *start, *p;
    for(p = start; p; p = p->next) {
        /* print p */
    }
    Besides, your code has a bug.
    Code:
    print(NULL);
    Code:
    struct linked_list
    {
    struct linked_list *Back;   // <-- this will hold the address of the previous node from the current node
    int number;
    struct linked_list *front;   // <- This will hold the address of the preceding node from the current node
    };
    The "next" pointer is by convention called "next" as the OP has in his/her original program. But you could call it something else if you wanted to.

    Code:
    #include<alloc.h>
    #define NULL 0
    alloc.h is a non-standard, antiquated header file. You should get a newer compiler. If you'd included <stddef.h> (or <stdio.h> for pre-standard compilers) you'd have seen that NULL was already defined.

    Don't use void main() but rather int main() -- see the FAQ.

    getch(), clrscr(), etc are generally in <conio.h> with the compilers that have them (both are non-standard). I don't see why you need to clear the screen and you could pause your program with getchar()s or some other alternative as suggested in the FAQ "How do I keep my Windows console from closing?"

    You should free memory that you malloc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    hmm, i've got a suggestion too:

    1.) declare four pointers to node, i.e. head, tail, p1, p2 and let them all point to the front of your linked list. if the list is NULL or empty, then do sumthin bout it.

    2.) then, traverse the list untill u reach the end using p1, and using p2 as a trailing pointer.
    Code:
    while(p1->next != NULL) {
       p2=p1;
       p=p->next;
    }
    3.) assign pointer tail to p1 so tail now points to the "last node". print the element of that node.
    then let tail point to the node before it, which is p2.

    4.) let p1 point to the head, and loop steps 2.) and 3.) while head != tail, each time printing the element of tail.

    5.) finally, if tail and head both point to the same node, print it, and your program ends.

    quite simple enough. you can also use this technique to reverse the order of your singly linked lists hehehe
    It is not who I am inside but what I do that defines me.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks
    There's no need to use recursion if you don't need it. A loop would do as well.
    Code:
    node *start, *p;
    for(p = start; p; p = p->next) {
        /* print p */
    }
    How exactly is this loop printing the list in reverse order again?


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

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I thought that any time you wanted to transverse a linked list it was always best to use recursion. Isn't that the base for binary searches?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It may not always be best, but it's usually pretty simple to write. Also, since this list obviously isn't circular (how could you print it in reverse if it didn't have an end?), recursion should be just fine. Of course, I hate single linked lists, because they're a pain to use compared to double linked lists. But with home work I suspect they don't have much choice.

    I'd use the recursive method, because looping to print a list in reverse order is too much of a pain to be worth the effort.


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

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Recursion in general should be avoided on linked lists since the stack will grow O(n) and it takes longer when a simple loop will suffice. In this case I'm sure it won't matter though.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM