Hi I'm trying to make these 2 methods work, but even though I tried pretty much everything I can think of, it's still not working. Please tell me how to fix it!
void add(Anything value): Adds a node containing newValue to the end of the list.
void addAfter(int index, Anything value): Adds a Node node containing newValue after the node of index index(assume index start in 0).
Here's my code: ( The methods above appear at the bottom )
public class Node<Anything>
{
private Anything data;
private Node next;
Node(Anything a, Node<Anything> n)
{
data = a;
next = n;
}
public Anything getData()
{
return this.data;
}
public Anything setData(Anything newData)
{
Anything oldData = this.data;
this.data = newData;
return oldData;
}
public void setNext(Node<Anything> newNext)
{
this.next = newNext;
}
public Node<Anything> getNext()
{
return this.next;
}
}
------------------------------------------
public class CS2LinkedList<Anything>
{
private Node<Anything> first;
private Node<Anything> last;
public CS2LinkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first == null);
}
public void addFirst(Anything d)
{
Node<Anything> temp = first;
first = new Node<>(d,temp);
}
public void clear()
{
first = null;
}
public boolean contains(Anything value)
{
for (Node curr = first; curr != null; curr = curr.getNext())
{
if (value.equals(curr.getData())){
return true;
}
}
return false;
}
public String toString()
{
StringBuilder result = new StringBuilder(); //String result = "";
for (Node curr = first; curr != null; curr = curr.getNext())
result.append(curr.getData() + "->"); //result = result + curr.data + "->";
result.append("[null]");
return result.toString(); //return result + "[null]";
}
public int size()
{
int size = 0;
for (Node curr = first; curr != null; curr = curr.getNext()){
size++;
if (first==null){
size = 0;
}
}
return size;
}
public Anything getFirst()
{
if (first!=null){
return first.getData();
}
else{
System.out.println("Sorry, the list is empty.");
return null;
}
}
public Anything getLast()
{
if (first!= null){
for(Node curr = first; curr != null; curr = curr.getNext()){
first = curr;
}
return first.getData();
//FIX: list2's size decreases by 1 after executing list2.getLast()
}
else{
System.out.println("Sorry, the list is empty.");
return null;
}
}
public void add(Anything value){
if (first==null){
first = new Node<>(value,first);
}
Node<Anything> next = new Node<>(value, first);
first.setNext(null);
last = next;
}
public void addAfter(int index, Anything value)
{
return;
}
}
A bit long answer, tried to explain every bit of problem and give the solution. Have patience!
You're code is exactly doing what you have told it to do, adding new nodes in reverse . It is adding the new node
as the first
and set the current first
as new node's next
while addFirst()
is called.
But with the current add()
method you've mentioned, you'll always get a linked list not more than one
element by calling the printing toString()
method, although you have the list containing not more than two nodes. Let's look at your code to understand this:
public void add(Anything value){
if (first==null){
first = new Node<>(value,first);
}
Node<Anything> next = new Node<>(value, first);
first.setNext(null);
last = next;
}
So what you are doing is, you're checking whether the first
is null, which means, whether the current list is empty. If true
then initialize first
with new value (say "x"
) and it's next as current first
, which is null
. So we get [first(x)] -> [null]
as our current list.
Next you're initializing a next
node with the value
and first
as it's next. Then you are setting the next of first
as null
and assigning next
to the last
. So far we've got:
[next(x)] -> [first(x)] -> [null] //where last node is next(x)
Now if you want to add(y)
, another next
will be initialized with the current first
as the next of this new node. Then again redundant making of null
of the next
of the first
. And again assigning this new node to last
. Now we've got:
[next(y) -> [first(x)] -> [null]
So, here we go! You are losing the current last
by assigning the new node to the last
, and this last
will have the first
as it's next, always, how many nodes you want to add, it follows the same path.
Hence, you'll get a linked list of two
elements, always, with this add()
method.
But why the toString() gives only [first(x)] -> [null]
as linked list?
Well, look at the toString()
function, it starts from first
, runs until first.next == null
. And, you have first.next == null
, always, if you add nodes with this add()
method. Therefore, such behavior of the toString()
.
What's the solution?
Set both of the first
and last
of the linked list in the constructor as null during the initialization. In the add()
method, initialize the first
for the first time with the value
and last
. And assign it to the last
. return
after that as we are done adding the first node. Remove the first.setNext(null)
statement, initialize the new node
with value and null
. Make the next
of the current last
as new node
. and assign the new node as the last. Here is the code:
public void add(Anything value) {
if (first == null) {
first = new Node<>(value, last);
last = first;
return;
}
Node<Anything> next = new Node<>(value, null);
last.setNext(next);
last = next;
}
Now if you print the toString()
, you'll get:
node1->node2->node3->[null]
Hope you got your answer and desired solution!
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.