By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,837 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

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

P: 5
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
Expand|Select|Wrap|Line Numbers
  1. struct linked_list
  2. {
  3. int number;
  4. struct linked_list *next;
  5. };
  6.  
  7. typedef linked_list node;
  8.  
  9. void main()
  10. {
  11.  void create(node *);
  12.  void print(node *);
  13.  
  14.  clrscr();
  15.  node *head;
  16.  head=(node*)malloc(sizeof(node));
  17.  create(head);
  18.  print(head);
  19.  getch();
  20.  }
  21.  
  22.  void create(node *p)
  23.  {
  24.  printf("Enter the number : \n");
  25.  scanf("%d",&p->number);
  26.  if(p->number==999)
  27.       {
  28.        p->next=NULL;
  29.       }
  30.  else
  31.      {
  32.       p->next=(node*)malloc(sizeof(node));
  33.       create(p->next);
  34.      }
  35.  return;
  36.   }
  37.  
  38. void print(node *p)
  39. {
  40.  if(p->next!=NULL)
  41.    {
  42.     printf("%d-->",p->number);
  43.     print(p->next);
  44.     }
  45.  
  46. }
  47.  
Oct 11 '06 #1
Share this Question
Share on Google+
6 Replies


arne
Expert 100+
P: 315
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
Expand|Select|Wrap|Line Numbers
  1. struct linked_list
  2. {
  3. int number;
  4. struct linked_list *next;
  5. };
  6.  
  7. typedef linked_list node;
  8.  
  9. void main()
  10. {
  11.  void create(node *);
  12.  void print(node *);
  13.  
  14.  clrscr();
  15.  node *head;
  16.  head=(node*)malloc(sizeof(node));
  17.  create(head);
  18.  print(head);
  19.  getch();
  20.  }
  21.  
  22.  void create(node *p)
  23.  {
  24.  printf("Enter the number : \n");
  25.  scanf("%d",&p->number);
  26.  if(p->number==999)
  27.       {
  28.        p->next=NULL;
  29.       }
  30.  else
  31.      {
  32.       p->next=(node*)malloc(sizeof(node));
  33.       create(p->next);
  34.      }
  35.  return;
  36.   }
  37.  
  38. void print(node *p)
  39. {
  40.  if(p->next!=NULL)
  41.    {
  42.     printf("%d-->",p->number);
  43.     print(p->next);
  44.     }
  45.  
  46. }
  47.  

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
Oct 11 '06 #2

P: 22
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);
}
}
Oct 11 '06 #3

P: 5
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
Oct 11 '06 #4

100+
P: 293
D_C
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.
Oct 11 '06 #5

P: 5
hello sir..
that was great..i want to know more about recursive functions .Do u have anything for me.
Oct 12 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
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.
Oct 12 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.