link

I've formed a link list to contain data,the structure is
[code]
class NODE
{
public String data;
public NODE pre,next,sub;
void NODE()
{
data="";
pre=null;
next=null;
sub=null;
}
}
[/code]
It is like a link that each node has three points
[code]
pre-data-next
|
sub

a link is like:
data1------data2-------data8-------data9
|
data3-------data4-------data5
|
data6------data7

[/code]
I've formed a link like that by programming, but now I don't know how to output the data in order like the numbers above. Really confused by how to form the cycle. Hope anyone whos interested in this or had experience in this type of link structure can give me a hand.

Comments

  • : I've formed a link list to contain data,the structure is
    : [code]
    : class NODE
    : {
    : public String data;
    : public NODE pre,next,sub;
    : void NODE()
    : {
    : data="";
    : pre=null;
    : next=null;
    : sub=null;
    : }
    : }
    : [/code]
    : It is like a link that each node has three points
    : [code]
    : pre-data-next
    : |
    : sub
    :
    : a link is like:
    : data1------data2-------data8-------data9
    : |
    : data3-------data4-------data5
    : |
    : data6------data7
    :
    : [/code]
    : I've formed a link like that by programming, but now I don't know how to output the data in order like the numbers above. Really confused by how to form the cycle. Hope anyone whos interested in this or had experience in this type of link structure can give me a hand.
    :
    :
    For a linked list to work, each node must contain the next link. In your case only the first node can contain a link to the sub and/or next node. You should create a class, which holds the data and the link(s). Then you can chain them together into a list.
  • [code]
    : : pre-data-next
    : : |
    : : sub
    : : null null
    : : a link is like: | |
    : : null---data1------data2-------data8-------data9---null
    : : | |
    : : null null---data3-------data4-------data7---null
    : : | |
    ull
    : : null data5------data6
    | |
    null null
    : :
    : : [/code]

    Hello. Thank you so much for replying to my problem. But Im sorry that I guess I didn't tell my problem clearly. I have formed a list like that already, I read the data from a text file. I have chained the data together into a list, and each of my nodes contains the next link, the previous link and sub link which is pointing at the head of its sublink.
    but I don't know how to travese the list I formed in order, for example now I wanna read the data in this link list in order, how to create a condition to control the read function cycle, in what condition to break the cycle? examples like below, lets say the first node of my link is 'head', then programs like these won't work

    [code]
    NODE slide=new NODE();
    slide=head;
    if(slide.sub!=null)
    {
    System.out.println(slide.sub.data);
    slide=slide.sub;
    }
    [/code]
    or
    [code]
    while(slide!=null)
    {
    System.out.println(slide.data);
    slide=slide.next;
    }
    [/code]

    The problem that I think is I don't know how many layers are there before I read the link, so I am not sure if I tell clearly or not this time, any thoughts? I really need and appreciate your help.

  • [b][red]This message was edited by zibadian at 2006-9-11 23:49:6[/red][/b][hr]
    : [code]
    : : : pre-data-next
    : : : |
    : : : sub
    : : : null null
    : : : a link is like: | |
    : : : null---data1------data2-------data8-------data9---null
    : : : | |
    : : : null null---data3-------data4-------data7---null
    : : : | |
    ull
    : : : null data5------data6
    : | |
    : null null
    : : :
    : : : [/code]
    :
    : Hello. Thank you so much for replying to my problem. But Im sorry that I guess I didn't tell my problem clearly. I have formed a list like that already, I read the data from a text file. I have chained the data together into a list, and each of my nodes contains the next link, the previous link and sub link which is pointing at the head of its sublink.
    : but I don't know how to travese the list I formed in order, for example now I wanna read the data in this link list in order, how to create a condition to control the read function cycle, in what condition to break the cycle? examples like below, lets say the first node of my link is 'head', then programs like these won't work
    :
    : [code]
    : NODE slide=new NODE();
    : slide=head;
    : if(slide.sub!=null)
    : {
    : System.out.println(slide.sub.data);
    : slide=slide.sub;
    : }
    : [/code]
    : or
    : [code]
    : while(slide!=null)
    : {
    : System.out.println(slide.data);
    : slide=slide.next;
    : }
    : [/code]
    :
    : The problem that I think is I don't know how many layers are there before I read the link, so I am not sure if I tell clearly or not this time, any thoughts? I really need and appreciate your help.
    :
    :
    You can always count the number of layers:
    [code]
    int layerCount = 0;
    while (slide != null) {
    layerCount++;
    slide = slide.sub;
    }
    [/code]
    That way you know the number of layers.
  • : [b][red]This message was edited by zibadian at 2006-9-11 23:49:6[/red][/b][hr]
    : : [code]
    : : : : pre-data-next
    : : : : |
    : : : : sub
    : : : : null null
    : : : : a link is like: | |
    : : : : null---data1------data2-------data8-------data9---null
    : : : : | |
    : : : : null null---data3-------data4-------data7---null
    : : : : | |
    ull
    : : : : null data5------data6
    : : | |
    : : null null
    : : : :
    : : : : [/code]
    : :
    : : Hello. Thank you so much for replying to my problem. But Im sorry that I guess I didn't tell my problem clearly. I have formed a list like that already, I read the data from a text file. I have chained the data together into a list, and each of my nodes contains the next link, the previous link and sub link which is pointing at the head of its sublink.
    : : but I don't know how to travese the list I formed in order, for example now I wanna read the data in this link list in order, how to create a condition to control the read function cycle, in what condition to break the cycle? examples like below, lets say the first node of my link is 'head', then programs like these won't work
    : :
    : : [code]
    : : NODE slide=new NODE();
    : : slide=head;
    : : if(slide.sub!=null)
    : : {
    : : System.out.println(slide.sub.data);
    : : slide=slide.sub;
    : : }
    : : [/code]
    : : or
    : : [code]
    : : while(slide!=null)
    : : {
    : : System.out.println(slide.data);
    : : slide=slide.next;
    : : }
    : : [/code]
    : :
    : : The problem that I think is I don't know how many layers are there before I read the link, so I am not sure if I tell clearly or not this time, any thoughts? I really need and appreciate your help.
    : :
    : :
    : You can always count the number of layers:
    : [code]
    : int layerCount = 0;
    : while (slide != null) {
    : layerCount++;
    : slide = slide.sub;
    : }
    : [/code]
    : That way you know the number of layers.
    :
    Thanks so much! That gave me some clue, still need help though. Do you have any other suggestions about using recursion? My problem looks like some traverse binary tree problem, instead of 'binary', it has three.
  • : : [b][red]This message was edited by zibadian at 2006-9-11 23:49:6[/red][/b][hr]
    : : : [code]
    : : : : : pre-data-next
    : : : : : |
    : : : : : sub
    : : : : : null null
    : : : : : a link is like: | |
    : : : : : null---data1------data2-------data8-------data9---null
    : : : : : | |
    : : : : : null null---data3-------data4-------data7---null
    : : : : : | |
    ull
    : : : : : null data5------data6
    : : : | |
    : : : null null
    : : : : :
    : : : : : [/code]
    : : :
    : : : Hello. Thank you so much for replying to my problem. But Im sorry that I guess I didn't tell my problem clearly. I have formed a list like that already, I read the data from a text file. I have chained the data together into a list, and each of my nodes contains the next link, the previous link and sub link which is pointing at the head of its sublink.
    : : : but I don't know how to travese the list I formed in order, for example now I wanna read the data in this link list in order, how to create a condition to control the read function cycle, in what condition to break the cycle? examples like below, lets say the first node of my link is 'head', then programs like these won't work
    : : :
    : : : [code]
    : : : NODE slide=new NODE();
    : : : slide=head;
    : : : if(slide.sub!=null)
    : : : {
    : : : System.out.println(slide.sub.data);
    : : : slide=slide.sub;
    : : : }
    : : : [/code]
    : : : or
    : : : [code]
    : : : while(slide!=null)
    : : : {
    : : : System.out.println(slide.data);
    : : : slide=slide.next;
    : : : }
    : : : [/code]
    : : :
    : : : The problem that I think is I don't know how many layers are there before I read the link, so I am not sure if I tell clearly or not this time, any thoughts? I really need and appreciate your help.
    : : :
    : : :
    : : You can always count the number of layers:
    : : [code]
    : : int layerCount = 0;
    : : while (slide != null) {
    : : layerCount++;
    : : slide = slide.sub;
    : : }
    : : [/code]
    : : That way you know the number of layers.
    : :
    : Thanks so much! That gave me some clue, still need help though. Do you have any other suggestions about using recursion? My problem looks like some traverse binary tree problem, instead of 'binary', it has three.
    :
    Here's a basic start for recursively walking through an n-branched tree:
    [code]
    void treeWalk(Node tree, PerformInterface doSomething) {
    if (tree.child1 != null)
    treeWalk(tree.child1, doSomething);
    if (tree.child2 != null)
    treeWalk(tree.child2, doSomething);
    if (tree.child3 != null)
    treeWalk(tree.child3, doSomething);
    // etc.
    doSomething.perform(tree); // do something with a node
    }
    [/code]
    The order, in which the if-statements and the perform() call are, may be changed as needed. This code will do something with the lowest child first, then its siblings, then its parent, then the lowest children of its parent's siblings, etc. If you want to perform from the top down, then you need to switch the if-statements and the perform() call around. Obviously names and types are arbirtary and must be changed as needed. If the children are stored in arrays or Collections, then you can use a for-loop to walk through them.
    You can also code this from the node's standpoint:
    [code]
    class Node {

    void show() {
    // do show node
    }

    void showTree() {
    show();
    if (child1 != null)
    child1.showTree();
    if (child2 != null)
    child2.showTree();
    if (child3 != null)
    child3.showTree();
    // etc.
    }

    }
    [/code]
    This allows you to let the tree perform its tasks for itself, without the need for extra classes or interfaces. Obviously you can add parameters to the two methods as needed. You can also return certain values as needed, etc.
  • That works, thanks a lot!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories