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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghuveer
    New Member
    • Sep 2006
    • 5

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

    i want to print my linked list in the reverse order ie..last item first and first item last..this is what i have so far
    Code:
    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);
        }
    
    }
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by raghuveer
    i want to print my linked list in the reverse order ie..last item first and first item last..this is what i have so far
    Code:
    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);
        }
    
    }

    What about using a doubly-linked list (instead of a simple-linked one)? This would allow you to traverse your list in either direction. Just add another member *prev to your node struct and set it to the correct values when you remove or add an element.

    If you really want to use the simple-linked list, you may introduce a marker which denotes the last element you printed. When traversing the list, you compare p->next to this marker and when its the same (or p->next equals NULL), p points to the element you want to print now. Print it and set the marker to it.

    arne

    Comment

    • zahidkhan
      New Member
      • Sep 2006
      • 22

      #3
      Originally posted by arne
      What about using a doubly-linked list (instead of a simple-linked one)? This would allow you to traverse your list in either direction. Just add another member *prev to your node struct and set it to the correct values when you remove or add an element.

      If you really want to use the simple-linked list, you may introduce a marker which denotes the last element you printed. When traversing the list, you compare p->next to this marker and when its the same (or p->next equals NULL), p points to the element you want to print now. Print it and set the marker to it.

      arne

      Replace your print function with this
      void print(node *p)
      {
      if(p!=NULL)
      {
      print(p->next);
      printf("%d-->",p->number);
      }
      }

      Comment

      • raghuveer
        New Member
        • Sep 2006
        • 5

        #4
        Originally posted by zahidkhan
        Replace your print function with this
        void print(node *p)
        {
        if(p!=NULL)
        {
        print(p->next);
        printf("%d-->",p->number);
        }
        }
        great!
        can u explain more in detail

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          It's a recursive approach. Basically, the recursive call saves all information on the stack. Stack is a LIFO architecture, so the last node is first on the stack, and first one printed. Then the rest are printed in reverse order. The first node is printed last.

          Comment

          • raghuveer
            New Member
            • Sep 2006
            • 5

            #6
            hello sir..
            that was great..i want to know more about recursive functions .Do u have anything for me.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by raghuveer
              hello sir..
              that was great..i want to know more about recursive functions .Do u have anything for me.
              A function is recursive if it calls itself. Generally they are used with linked lists (as shown) or in mathematical formulas where you generate the (n+1)th term from the nth term, i.e. factorial where n! = n * (n-1)!.

              You need to have a stop condition in a recursive function, that is a definate condition where the function wont call itself, in the example given the stop condition is p == NULL, which is a good one for a linked list.

              The danger of recursive functions is that you call it for a recurstion that is so deep (i.e. the function calls itself so many times)that you run out of stack space for the stack frame each function call creates. This results in a system crash normally (you can easily simulate this by writing a recursive function with no stop condition and seeing what it does).

              In your case this could happen if the list of numbers entered was too long (you have no limit on the length of the list).

              However since your create function is already recursive you would probably reach this condition while creating the list.


              It is normally possible to replace a recursive function with an iterative one (that is a function using a loop), however in the particular case of iterating backwards through a singly linked list it requires a double (nested) loop and so is quite inefficient.

              The advantage of an iterative function is that you wont run out of stack space while running it (unless of course you are already knee deep in function calls). You create function could fairly easily be changed from a recurse to an iterative function.

              Comment

              Working...