473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
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
6 18084
arne
315 Expert 100+
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
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
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
D_C
293 100+
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
hello sir..
that was great..i want to know more about recursive functions .Do u have anything for me.
Oct 12 '06 #6
Banfa
9,065 Expert Mod 8TB
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

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

Similar topics

7
by: dam_fool_2003 | last post by:
friends, I wanted to learn the various ways of inserting a single list. so: Method 1: #include<stdlib.h> #include<stdio.h> struct node { unsigned int data; struct node *next;
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
8
by: asm_fool | last post by:
dear group, /*Linked list in C*/ #include<stdio.h> #include<stdlib.h> struct node {
14
by: fool | last post by:
Dear group, Thanks for all those who helped previously. Now I get some value printed. Is that the value of the list getting printed? Am I really polluted the list? Previous thread link...
22
by: joshc | last post by:
In an interview for an embedded software position recently I was asked to write code, in C, for printing the contents of a linked list backwards. After a few minutes I came up with the recursive...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.