Solved

Printing out my whole linked list

Posted on 2003-12-01
6
374 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:Kdo
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 45

Accepted Solution

by:
Kdo 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:Kdo
ID: 9852831

:)

Glad to help.

Kent
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 opening and reading files in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now