Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

recursion to iteration

Posted on 1997-12-03
3
Medium Priority
?
217 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 85

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 450 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

877 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