where to put the nodes into the array?
hi guys
im implementing a basic singly linked list structure (Java class) it should allow the addition and deletion of a single (element in the linked list) at a specified position i. we can assume that each element is int.
there is a code (I have couple of similar but this is better than at hers so far)
3)
public class Node{
public int data;
Node next;
public Node getNextNode(Node n){
return n;
}
public Node(int data){
this.setData(data);
}
public Node(int data,Node next){
this.setData(data);
this.next=next;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
this is to create the node, but struggling with where to put the nodes into the array?
any advice
thx
Re: where to put the nodes into the array?
You need to add some methods if you physically want to keep track of all the Nodes. First of all, you need a LinkedList which accepts Nodes' so you can keep track of every single node and a variable to keep track of the index of that certain node.
Code :
LinkedList<Node> allNodes = new LinkedList<Node>();
int index;
Then, inside your constructor, you want to add a Node to the list of nodes.
Code :
allNodes.add(this);
index = allNodes.size()-1;
Then, if you want to keep track of that certain index, you can make a method that returns the index.
Code :
public int getIndex(){
return index;
}
And that will return the index which holds all of that data.
Re: where to put the nodes into the array?
Re: where to put the nodes into the array?
Quote:
Originally Posted by
Norm
Honestly, I think it is the same problem. Same type of problem, and same user who's posting it.
Re: where to put the nodes into the array?
Re: where to put the nodes into the array?
Quote:
Originally Posted by
SupportIsPower
You need to add some methods if you physically want to keep track of all the Nodes. First of all, you need a LinkedList which accepts Nodes' so you can keep track of every single node and a variable to keep track of the index of that certain node.
Code :
LinkedList<Node> allNodes = new LinkedList<Node>();
int index;
Then, inside your constructor, you want to add a Node to the list of nodes.
Code :
allNodes.add(this);
index = allNodes.size()-1;
Then, if you want to keep track of that certain index, you can make a method that returns the index.
Code :
public int getIndex(){
return index;
}
And that will return the index which holds all of that data.
is it adding an array list?
Re: where to put the nodes into the array?
What is the "it" you refer to?
Re: where to put the nodes into the array?
Quote:
Originally Posted by
Norm
What is the "it" you refer to?
why u just go somewhere and have fun?!
Re: where to put the nodes into the array?
If you'd write a full sentence it would help those trying to help you.
I'll take your suggestion. This thread is getting to be a waste of time.
Re: where to put the nodes into the array?
Quote:
Originally Posted by
Norm
If you'd write a full sentence it would help those trying to help you.
I'll take your suggestion. This thread is getting to be a waste of time.
yeap, there is too much question than suggestion. luck of academic literature