3 Replies - 362 Views - Last Post: 09 November 2012 - 08:23 AM Rate Topic: -----

#1 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Binary Tree/Struct Problem

Posted 09 November 2012 - 07:58 AM

Hello,

I'm just starting to get a binary tree on the go. I've got a problem already which I can't get my head round. I swear that I'm doing this correctly but not according to the compiler. I'm getting the error 'unknown type name 'node''./

#include <stdio.h>
#include <stdlib.h> 
struct Node
{
    int input;
    Node * left
    Node * right;
};



Is This A Good Question/Topic? 0
  • +

Replies To: Binary Tree/Struct Problem

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Binary Tree/Struct Problem

Posted 09 November 2012 - 08:15 AM

In C (unlike C++) the name of a struct is not a valid type name by itself - it has to be prefaced with struct to become a type name.

So to declare a variable containing a Node pointer, write struct Node* varname;.
Was This Post Helpful? 0
  • +
  • -

#3 ivaylo9603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 05-November 12

Re: Binary Tree/Struct Problem

Posted 09 November 2012 - 08:19 AM

There is a syntax error in the posted code. It should either have a semi-colon after the declaration of pointer left, or both pointers- left and right should both be declared after the datatype in a single line, separated with commas as follows:
Node *left, *right;
//or 
Node* left;
Node* right;


Sent from my GT-P3110

This post has been edited by ivaylo9603: 09 November 2012 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Binary Tree/Struct Problem

Posted 09 November 2012 - 08:23 AM

In C, use typedef to make life easier.

e.g.
typedef struct NodeStruct {
    int input;
    struct NodeStruct *left, *right;
} Node;

typedef struct {
    Node *head;
} List;



Note, you must use the full struct declaration in the node struct, since you haven't hit the typedef yet. After that, you can just use the typedef'd name.

Also, for linked lists, make your life easier and use that struct for it. Passing pointers to pointers or passing back head pointers is unnecessary confusion.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1