Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Printing out my whole linked list

Posted on 2003-12-01
6
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 46

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1000 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 46

Expert Comment

by:Kent Olsen
ID: 9852831

:)

Glad to help.

Kent
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

604 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