tree

B

Bill Cunningham

I have this code.

struct node
{ char *name;
double price;
int count;
int vol;
struct node *rchild;
struct node *lchild;
};

The idea is to create a tree that contains information about stock names and
prices as well as volume. The count int type is to give the node a number. I
understand there is alot more that goes with a tree. Like adding and
deleting nodes, but this is how far I've got. I will need memory allocation
also. I know the sematics of what I am to do but like the skill on how to go
about it. Does anyone have any ideas?

Bill
 
B

Bill Cunningham

Oops. I meant for this to be posted to comp.programming. I was browsing clc
when I sent to post sorry. Please excuse this not being totally on topic.
And kanr2 does seem to be helping me here.

Thanks
Bill
 
G

Guest

Oops. I meant for this to be posted to comp.programming. I was browsing clc
when I sent to post sorry. Please excuse this not being totally on topic.
And kanr2 does seem to be helping me here.

there's an addtree() function on p141. Why can't you copy it and
make the necessary mods? To avoid malloc() you *could* use an array
of struct nodes. So where are you stuck?
 
B

Bill Cunningham

there's an addtree() function on p141. Why can't you copy it and
make the necessary mods? To avoid malloc() you *could* use an array
of struct nodes. So where are you stuck?

I copy te entirety of the code text and it just doesn't work. I get
error this and error that. I still have the .news file you posted and it
helps. I get what kandr2 is saying in theory but I don't know enough to code
it. Copying the code gives me errors.

Bill
 
B

Bill Cunningham

there's an addtree() function on p141. Why can't you copy it and
make the necessary mods? To avoid malloc() you *could* use an array
of struct nodes. So where are you stuck?

I think there might be a scope problem here too. Functions like addtree
are declared and not defined until later. I have tried putting all the
function declarations into one header with the tnode struct definition.
Would these need to be declared with extern or not?

Bill
 
G

Guest

there's an addtree() function on p141. Why can't you copy it and
make the necessary mods? To avoid malloc() you *could* use an array
of struct nodes. So where are you stuck?

    I copy [the] entirety of the code text and it just doesn't work.[/QUOTE]

you've been around long enough to know "it just doesn't work"
isn't acceptable.
I get error this and error that.

nor this
I still have the .news file you posted and it
helps. I get what kandr2 is saying in theory but I don't know enough to code
it. Copying the code gives me errors.

well, yes the field names are different!

Can you get the K&R code to compile? No changes at all.

Standard Bill rules

0. fix your layout
1. post your code
2. post your input data
3. post your output
4. explain why you don't like 3
5. check return values
6. RTFM
7. don't guess (see 6)

you seem to be in breach of (at least) 1, 2, 3 and 4
 
G

Guest

    I think there might be a scope problem here too. Functions like addtree
are declared and not defined until later. I have tried putting all the
function declarations into one header with the tnode struct definition.
Would these need to be declared with extern or not?

try putting everything in one file to start with. No .h files (apart
from
standard ones). You are trying to do too much at once.

In your file have the tree structure an addtree() a displaytree()
and a test in main.
 
B

Bill Cunningham

In your file have the tree structure an addtree() a displaytree()
and a test in main.

I don't have them defined in main, if that's what you mean.
 
B

Bill Cunningham

Can I try this? Declare all the functions except main in a header with
the tnode struct as is. Then right separate C files for each function and
include the header tree.h in all of them compile them into .o files and then
write main in its own c file ? This is what I've been trying.

Bill
 
G

Guest

In your file have the tree structure an addtree() a displaytree()
and a test in main.

    I don't have them defined in main, if that's what you mean.

put the test code in main
 
G

Guest

    Can I try this? Declare all the functions except main in a header with
the tnode struct as is. Then right separate C files for each function and
include the header tree.h in all of them compile them into .o files and then
write main in its own c file ? This is what I've been trying.

Bill

can you get it to work as a single file?

I'm about to give up on you (again)

Why do I do this "I'm sure Bill is going to be different
*this* time" I say to myself. But he never is.
 
O

osmium

Can I try this? Declare all the functions except main in a header with
the tnode struct as is. Then right separate C files for each function and
include the header tree.h in all of them compile them into .o files and
then
write main in its own c file ? This is what I've been trying.

Bill

can you get it to work as a single file?

I'm about to give up on you (again)

Why do I do this "I'm sure Bill is going to be different
*this* time" I say to myself. But he never is.

LMAO. Been there, done that. See comp.programming
 
D

David Thompson

[put] function declarations into one header with the tnode struct definition.
Would these need to be declared with extern or not?

Using extern with a function declaration is superfluous.
Yes. (Also with a function definition, which wasn't asked.)
Using extern with the definition of a structure type (this is really a
declaration but the standard calls it a definition so...) is a syntax
error.

Maybe almost.

If we mean (declaration of) a typedef for a struct type
typedef struct /* maybe tag */ { stuff... } mystruct_t;
then adding extern would be a constraint violation, which strictly
speaking isn't a syntax error but is treated the same as one.

If we mean just a struct type
struct tag_needed_to_be_useful { stuff... } ;
then adding extern is not a syntax error or a constraint violation.
It is useless and misleading, and GCC for one warns, but it is legal.

Specifically, a declaration consisting only of declaration-specifiers
with no declarators (or initializers) is legal if the decl-specs
declare a tag or enum member(s). Thus the syntactically similar case
of 'extern int ;' IS a constraint violation.
extern can be used with the definition of a structure object
but you should not define objects in a header.

If in spite of all the advice not to, you actually have a [global] ... then you
DECLARE the object in the header with extern.

Yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top