Lists

  • Thread starter Thread starter Timothy V
  • Start date Start date
T

Timothy V

Hi,
I like doing things myself because it helps me to learn languages. I'm in a
little trouble at the moment which someone might be able to help me.

I'm trying to create a list class. I have a struct which points to another
struct and so on:

private struct node

{

public object item;

public node next;

}


The problem is, it won't let me compile because it says:
Struct member 'List.node.next' of type 'List.node' causes a cycle in the
structure layout

Is there a way around this?

Thanks in advance,

Timothy.
 
You said "I have a struct which points to another struct ...", but actually,
you've defined a struct which _contains_ another struct, and so on.
Obviously, that won't work, since you're going to end up with a data type of
infinite size.

The classic C approach to this problem would be to define "node *next"
instead, and dynamically create new nodes as you need them.

In C#, you would make node a reference type by declaring it "private class
node" instead of struct. If it helps, you can think of this as being
similar to the C++ "&node" Basically, this is internally represented as a
pointer, and every time you refer to a field, the pointer is implicitly
dereferenced.

HTH, Phil
 
Back
Top