简体   繁体   中英

how to create nodes of different characteristics using a single node class in java?

my node class is

class Node{

    protected int data;
    protected Node link;
    public Node(){
      link=null;
      data =0;
    }

    public Node(int d,Node n){
      data=d;
      link=n;  
    }

    public void setlink(Node n){
      link=n;
    }

    public void setData(int d){
      data=d;
    }

    public Node getlink(){
      return link;

    }

    public int getData(){
      return data;
    }
}

I want to create a node with 5 parts :

  1. to store row no.
  2. to store column no.
  3. to store value
  4. pointer to next row
  5. pointer to next column

You can use following class as node with required values:

static class Node
    {
        int rowNo;
        int columnNo;
        int value;
        Node next;
        int nextColumnNo;
        Node(int r,int c,int v) 
        {
            rowNo=r;
            columnNo=c;
            value=v;
            next=null;
            nextColumnNo=0;
        }
    }

And use the nodes as follow by linking together:

head = new Node(1,11,11);
Node second = new Node(2,22,22);
Node third = new Node(3,33,33);

head.next=second;
second.next=third;

head.nextColumnNo = second.columnNo;
second.nextColumnNo = third.columnNo;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM