T
Taria
Hello all,
I'm still pretty new to java and I'm having trouble understanding how
to reference a node.
I've written 2 classes, a node class with mutator and accessor methods
and a linked list class. I need to write a method within the linked
list class that will insert a new object into the linked list.
This is where I'm stuck. The stuff below won't compile ....I
understand the logic that I need to do but I'm having understanding
how to reference a node, the compiler is complaining the variable curr
is private in the Node class (but the professor wants us to declare
all variables private for more robust code so I have to do that).
I'm totally confused. I'm stuck as to how to go about solving this
problem or what to do to understand this concept a bit
better....anyone out there have ideas?
ANY help is appreciated,
Marion
public class LinkedList{
/**
* Construct the list
*/
private Node head;
private int length;
public LinkedList() {
this.head = null;
this.length = 0;
}
/**
* Test if the list is empty
* @return true if empty
*/
public boolean isEmpty(){
return head == null;
}
/**
* Get the data at position specified
* @param position is the location queried
* @return data at the position specified in the list
*/
public Object getItemAtPosition(int position) {
if (position<0 || position >= this.length){
return null;
}
Node curr = this.head;
for (int i=0;i<position;i++){
curr = curr.getNext();
}
return curr.getData();
}
public boolean insert (Object data,int position){
if (position<0 || position > this.length ){
return false;
}
// this part I'm confused at too...I don't know what data to
assign
// the getItemAtPosition(position) to, but what I want to do is
advance
// the node pointer to the parameter position that is passed to
this method.
getItemAtPosition(position);
// create the new node
Node temp = new Node(data);
// make the new node's next field to the current next field
temp.next = curr.next;
// make current next field point to the new node
curr.next = temp.next;
return true;
}
}
Then my node class is:
public class Node {
private Object data;
private Node next;
public Node(Object data){
this.data = null;
}
public Node(Object data,Node next){
this.data = data;
this.next = next;
}
public void setNext(Node next){
this.next = next;
}
public void setData(Object data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public Object getData(){
return this.data;
}
}
I'm still pretty new to java and I'm having trouble understanding how
to reference a node.
I've written 2 classes, a node class with mutator and accessor methods
and a linked list class. I need to write a method within the linked
list class that will insert a new object into the linked list.
This is where I'm stuck. The stuff below won't compile ....I
understand the logic that I need to do but I'm having understanding
how to reference a node, the compiler is complaining the variable curr
is private in the Node class (but the professor wants us to declare
all variables private for more robust code so I have to do that).
I'm totally confused. I'm stuck as to how to go about solving this
problem or what to do to understand this concept a bit
better....anyone out there have ideas?
ANY help is appreciated,
Marion
public class LinkedList{
/**
* Construct the list
*/
private Node head;
private int length;
public LinkedList() {
this.head = null;
this.length = 0;
}
/**
* Test if the list is empty
* @return true if empty
*/
public boolean isEmpty(){
return head == null;
}
/**
* Get the data at position specified
* @param position is the location queried
* @return data at the position specified in the list
*/
public Object getItemAtPosition(int position) {
if (position<0 || position >= this.length){
return null;
}
Node curr = this.head;
for (int i=0;i<position;i++){
curr = curr.getNext();
}
return curr.getData();
}
public boolean insert (Object data,int position){
if (position<0 || position > this.length ){
return false;
}
// this part I'm confused at too...I don't know what data to
assign
// the getItemAtPosition(position) to, but what I want to do is
advance
// the node pointer to the parameter position that is passed to
this method.
getItemAtPosition(position);
// create the new node
Node temp = new Node(data);
// make the new node's next field to the current next field
temp.next = curr.next;
// make current next field point to the new node
curr.next = temp.next;
return true;
}
}
Then my node class is:
public class Node {
private Object data;
private Node next;
public Node(Object data){
this.data = null;
}
public Node(Object data,Node next){
this.data = data;
this.next = next;
}
public void setNext(Node next){
this.next = next;
}
public void setData(Object data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public Object getData(){
return this.data;
}
}