Question:

Consider a linked list of integers defined by the following class definition. ?

by  |  earlier

0 LIKES UnLike

) Consider a linked list of integers defined by the following class definition.

class Node

{

int data;

Node next;

}

Write a recursive method that accepts the head of the list as a parameter and displays the values of the list in forward order.

 Tags:

   Report

3 ANSWERS


  1. The class definition is faulty. It won't compile. You can't use Node inside the definition of Node.

    Think about what would be allocated when you have a declaration:

    Node head;

    You'd have head.data and head.next. head.next is of type Node so the runtime would have to allocate head.next.data and head.next.next. head.next.next is of type Node so the runtime would have to allocate head.next.next.data and head.next.next.next. head.next.next.next is of type Node so it would have to allocate head.next.next.next.data and head.next.next.next.next. Etc.

    So there is no correct answer to the question; mine is the only correct answer; I get 10 points and you get extra credit when you tell your ignorant instructor that his assignment won't even build as written.


  2. something like:

    void listValue ( Node head )

    {

      printf("%s", head.data);

      if ( head.next != null )

      {

        printf(",");

        listValue(head.next);

      }

      return;

    }

    >> The comment about it not compiling is as ignorant as he claims the teacher is... Declaring a type does not actually instantiate the type.  It just defines what type the variable should hold, so it will in fact compile.  -10 for him, +10 for me.

  3. printList (Node n) {

    if (n != NULL) {

    System.out.println(n.data);

    printList(n.next);

    }

    }

    You can reverse the order of the println and the printList if I misunderstood what order you meant.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions