473,569 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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 18096
arne
315 Recognized Expert Contributor
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
zahidkhan
22 New Member
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
raghuveer
5 New Member
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 Contributor
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
raghuveer
5 New Member
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 Recognized Expert Moderator Expert
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
2154
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
7557
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
1482
by: asm_fool | last post by:
dear group, /*Linked list in C*/ #include<stdio.h> #include<stdlib.h> struct node {
14
1848
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 (sorry for google) http://groups.google.com/group/comp.lang.c/browse_thread/thread/346d99317e6b772b/578ac16a2ed4ed35?hl=en#578ac16a2ed4ed35
22
8015
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 solution. Being that recursion is frowned upon in embedded software, the answer was not what the interviewer expected, but alas it was correct. I...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.