Link to home
Start Free TrialLog in
Avatar of ch4
ch4

asked on

recursion to iteration

/*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.
Avatar of ozo
ozo
Flag of United States of America image

while( hd != NULL ){
        printf("%s %f %d\n", hd ->dat.partno, hd>dat.cost, hd->dat.quan);
        hd = hd ->link;
}
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rbr
rbr

All rbr has done is regurgitate ozo's answer. Give the points
to ozo.