Solved

recursion to iteration

Posted on 1997-12-03
3
208 Views
Last Modified: 2010-05-18
/*structure definition*/
struct data{  char partno[6];
              float cost;
              int quan;
              };
struct node{  struct data dat;
              struct node * link;
           };

/*pre: hd points to a node(usually the first)
 post: the contents of all the nodes from that initial node pointed to by hd to the end of the listhave been printed*/

void print_list(struct node *hd)

{if(hd!= NULL)
      {printf("%s %f %d\n", hd ->dat.partno,  hd>dat.cost,  hd ->dat.quan);
       print_list(hd ->link);
       }
}
     this is a recursive function for printing the elements on a list.  How can i write a new implementation of the function that does not use recursion but iteration instead.
0
Comment
Question by:ch4
  • 2
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256500
while( hd != NULL ){
        printf("%s %f %d\n", hd ->dat.partno, hd>dat.cost, hd->dat.quan);
        hd = hd ->link;
}
0
 
LVL 10

Accepted Solution

by:
rbr earned 150 total points
ID: 1256501
use
void print_list (struct node *hd)
{
   while (hd != NULL) {
          printf ("%s %f %d\n",hd->dat.partno,hd->dat.cost,hd->dat.quan);
      hd=hd->link
   }
}
0
 
LVL 10

Expert Comment

by:rbr
ID: 1256502
All rbr has done is regurgitate ozo's answer. Give the points
to ozo.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now