Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,619 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,016 people online right now. Registration is fast and FREE... Join Now!




Recursive Print

 
Reply to this topicStart new topic

Recursive Print, Help needed with code..

eudos
post 28 Aug, 2006 - 11:23 AM
Post #1


D.I.C Head

**
Joined: 23 Apr, 2006
Posts: 58


My Contributions


hello

I am trying to print a link list that contains only numbers. i have to print this list recursivly. on ordered and in revers order.. here is my code below

CODE


void printList(List list) {

     if ((list->next) != NULL){
          printf("\tList element: %d\n", list->value);

          printList(list->next);
          }
}


the problem i am facing is that the first number in the list is printed twice and the last number is not printed at all..

can anubodu plz figure out the problem..plz post if u have any question..
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 28 Aug, 2006 - 02:41 PM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Rather than checking if the next item in the list is NULL, check if the current item is NULL. This will solve the problem of it not printing the last item.
CODE


void printList(List list) {

     if ((list) != NULL){
          printf("\tList element: %d\n", list->value);

          printList(list->next);
          }
}


How are you passing the List to your function?

Is it like this:
CODE

printList(myList);
User is offlineProfile CardPM

Go to the top of the page

eudos
post 31 Aug, 2006 - 06:54 AM
Post #3


D.I.C Head

**
Joined: 23 Apr, 2006
Posts: 58


My Contributions


Thanks for the help smile.gif ..I have managed to print the link list recursively using the following code..that is in forward order..
CODE

void printForwards(List list) {
      printf("\tList element: %d\n",list->value );
    
     if ((list->next) == NULL){
          return;
     }else{
          printForwards(list->next);
     }

}


Next thing i am trying to do is print the same list in reverse order recursively..

I am passing a pointer to the first element in the list and the last element points to NULL. so since it is reverse order..i have to start printing from the bottom..

Can any body tell me plz.. how can i do this recursivly?
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:19AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month