Lists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Timothy V

    #1

    Lists

    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.


  • Phil S

    #2
    Re: Lists

    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


    "Timothy V" <tripix@msn.com > wrote in message
    news:OfSOyM8RDH A.304@tk2msftng p13.phx.gbl...[color=blue]
    > Hi,
    > I like doing things myself because it helps me to learn languages. I'm in[/color]
    a[color=blue]
    > 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.
    >
    >[/color]


    Comment

    Working...