Solved

Printing out my whole linked list

Posted on 2003-12-01
6
375 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Having a file "symlink" to a memory location (Windows) 6 169
Adjust Mfcapp 29 160
C dll call freezes 5 94
Using popen() and gunzip() to open file in HTTPServer 6 50
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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now