Solved

Printing out my whole linked list

Posted on 2003-12-01
6
380 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

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…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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.

856 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