Solved

Printing out my whole linked list

Posted on 2003-12-01
6
381 Views
Last Modified: 2010-04-15
I have hard coded some Structs into my program for testing purposes.
They are defined as follows:

typedef struct Node {
      int nodetype; /*1 = question, 2 = object*/
        int nodenumber;
      char object[25];
      char question[25];
      struct Node *YES_ptr;
      struct Node *NO_ptr;
      } Node;


I have a function nodePrint (struct Node *node_ptr)
that prints out the information of the current node

I am now trying to write a recursive method that reads through all the nodes starting from the root node. In my case the root node is "question1",

E.G for a tree set out like
      0      
    /   \
    1    2
  /  \
3     4
    /  \
   5    6

And I called treePrint on node 0, i want to print out
3 5 6 4 1 2 0

void treePrint(struct Node *node_ptr) {
 
  if (node_ptr == NULL)
    return; /*If this is a leaf node finish*/
  else {
    treePrint(node_ptr->YES_ptr); /*Follow the yes nodes*/
    treePrint(node_ptr->NO_ptr); /*Follow the no nodes*/
    nodePrint(node_ptr); /*Print out node info*/
  }
}

My code compiles fine but when calling hte treePrint method I get a segmentation fault.

Appreciate any help
0
Comment
Question by:welsh_boy
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9852450
void treePrint(struct Node *node_ptr)
{
  if (node_ptr == NULL)
    return; /*If this is a leaf node finish*/

  treePrint(node_ptr->YES_ptr); /*Follow the yes nodes*/
  treePrint(node_ptr->NO_ptr); /*Follow the no nodes*/
  nodePrint(node_ptr); /*Print out node info*/
}

Your treePrint() function is basically sound.  (I modified it slightly above to remove the unnecessary "else".)

I would suggest that the cause of the segmentation fault lies elsewhere.  Perhaps a bad pointer?

Kent
0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9852451
That code looks good to me.  Does nodePrint() work?
0
 

Author Comment

by:welsh_boy
ID: 9852584
I have called nodePrint on all the nodes that I have hard coded.

Here is an example of 2 of the nodes

  horse.nodetype = 2;
  horse.nodenumber = 3;
  horse.YES_ptr = NULL;
  horse.NO_ptr = NULL;
  strcpy(horse.object, "Horse");

  question2.nodetype = 1;
  question2.nodenumber = 4;
  question2.YES_ptr = &horse;
  question2.NO_ptr = &btDongle;
  strcpy(question2.question, "Is It Alive?");

If i print out all the nodes using nodePrint they all print out fine and there is no sign of a segmentation fault, but as soon as I execute the treePrint part of the code it prints out one leaf node then I get a segmentation fault
0
Industry Leaders: 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!

 
LVL 45

Accepted Solution

by:
Kent Olsen earned 250 total points
ID: 9852638

Try printing the tree in prefix style instead of postfix.  If you'll flush the output buffer from within nodePrint(), you'll know exactly which node is causing the failure.

New function enclosed:

void treePrint(struct Node *node_ptr)
{
  if (node_ptr == NULL)
    return; /*If this is a leaf node finish*/

  nodePrint(node_ptr); /*Print out node info*/
  treePrint(node_ptr->YES_ptr); /*Follow the yes nodes*/
  treePrint(node_ptr->NO_ptr); /*Follow the no nodes*/
}


Kent
0
 

Author Comment

by:welsh_boy
ID: 9852821
cheers Kent, that code found the dodgy pointer

thanks, its fixed now

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9852831

:)

Glad to help.

Kent
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

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 122
How to design, and implement simple interface 5 140
What is atomic operation? 6 67
Why  my code (program) build with old compiler? 11 113
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.

726 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