This is my code for the linked list (not the main). The method "addLast" gives me the following error and i'm not sure how to resolve it: "non-static variable cannot be referenced from a static context". It is talking about this line: return new Node(x,null); I'd appreciate any help as to how to resolve this issue. Thank you
public class LinkedList
{
private class Node
{
int item;
Node link;
public Node ()
{
item = Integer.MIN_VALUE;
link = null;
}
public Node (int x, Node p)
{
item = x;
link = p;
}
} //End of node class
private Node head;
public LinkedList()
{
head = null;
}
//adds a node to the start of the list with the specified data
//added node will be the first node in the list
public void addToStart(int x)
{
head = new Node(x, head);
}
//adds a number at end of list
public static Node addLast(Node header, int x)
{
// save the reference to the header so we can return it.
Node ret = header;
// check base case, header is null.
if (header == null) {
return new Node(x, null);
}
// loop until we find the end of the list
while ((header.link != null)) {
header = header.link;
}
// set the new node to the Object x, next will be null.
header.link = new Node(x, null);
return ret;
}
//displays the list
public void printList()
{
Node position = head;
while(position != null)
{
System.out.print(position.item + " ");
position = position.link;
}
System.out.println();
}
}
3 Answers 3
Here are two solutions:
Make Node
a static nested class:
private static class Node { ... }
Or, make the addLast
method an instance method:
public Node addLast(Node header, int x) { ... }
-
It doesn't need to be a static nested class, though it may make sense for it to be so. Making it non-static is irrelevant to the OPs problem.Chris Dodd– Chris Dodd2014年11月29日 21:34:38 +00:00Commented Nov 29, 2014 at 21:34
-
@ChrisDodd True, it's not absolutely necessary, but it seems more logical for
Node
to be independent ofLinkedList
.August– August2014年11月29日 21:39:50 +00:00Commented Nov 29, 2014 at 21:39 -
Depends on what you want to do with the list. Making it non-static allows the Node to refer back to the head of the list it is contained in, which may be useful for some algortihms.Chris Dodd– Chris Dodd2014年11月29日 21:44:55 +00:00Commented Nov 29, 2014 at 21:44
Remove the static
qualifier from addLast
-- it needs to be non-static to have a list to add to the end of. It also should not take (or return) a Node
, as Node
is a private nested class, so code outside this class doesn't know (or care) what a Node
is, so can't have one to pass.
public void addLast(int x) {
if (head == null) head = new Node(x, null);
else {
Node p = head;
while (p.link != null) p = p.link;
p.link = new Node(x, null); } }
-
I can't because addLast will have to be static in order for my main to access it.Eternal Punishment– Eternal Punishment2014年11月29日 21:40:26 +00:00Commented Nov 29, 2014 at 21:40
-
No, your main just needs a
LinkedList
to calladdLast
on --list = new LinkedList(); list.addLast(5); list.addLast(6);
...Chris Dodd– Chris Dodd2014年11月29日 21:47:33 +00:00Commented Nov 29, 2014 at 21:47 -
can you please explain the for loop?Eternal Punishment– Eternal Punishment2014年11月29日 21:49:48 +00:00Commented Nov 29, 2014 at 21:49
-
It scans to the end of the list. Rewritten as a
while
to make it clearerChris Dodd– Chris Dodd2014年11月29日 21:51:53 +00:00Commented Nov 29, 2014 at 21:51 -
so when i type in your code, for the line head = new Node (x); it gives me the following error : no suitable constructor found for Node(int)Eternal Punishment– Eternal Punishment2014年11月29日 21:52:53 +00:00Commented Nov 29, 2014 at 21:52
The answer is in the error line:
non-static variable cannot be referenced from a static context
Remove the static
for the method addLast
.
public Node addLast(Node header, int x)
{
....
}