1

I need to implement a Node class, where the basic methods are: getItem(), getNext(), setItem() and setNext(). I want the nodes to be able to store at least the default integer range in Java as the “item”; the “next” should be a reference or pointer to the next Node in a linked list, or the special Node NIL if this is the last node in the list.I also want to implement a two-argument constructor which initializes instances with the given item (first argument) and next node (second argument) , I've kind of hit a brick wall and need some guidance about implementing this , any ideas ?

I have this so far:

class Node {


public Node(Object o, Node n) {

}
public static final Node NIL = new Node(Node.NIL, Node.NIL);

public Object getItem() {
    return null;
}
public Node getNext() {
    return null;
}
public void setItem(Object o) {

}
public void setNext(Node n) {

}
}
J CENA
  • 51
  • 1
  • 7
  • 1
    You might want to take a look at this one: http://stackoverflow.com/questions/31544501/creating-a-node-class-in-java – Diana Amza Jan 30 '17 at 09:00

4 Answers4

1

While implementing the custom LinkedList/Tree, we need Node. Here is demo of creating Node and LinkedList. I have not put in all the logic. Just basic skeleton is here and you can then add more on yourself.

enter image description here

0

I can give you a quick hint on how to do that:

Class Node{

    //these are private class attributes, you need getter and setter to alter them.
    private int item;
    private Node nextNode;

    //this is a constructor with a parameter
    public Node(int item)
    {
        this.item = item;
        this.nextNode = null;
    }

    // a setter for your item 
    public void setItem(int newItem)
    {
        this.item = newItem;
    }

    // this is a getter for your item
    public int getItem()
    {
        return this.item;
    }   

}

You can create a Node object by calling:

Node newNode = Node(2);

This is not a complete solution for your problem, the two parameter constructor and the last node link are missing, but this should lead you in the correct direction.

Stefan Lindner
  • 321
  • 1
  • 10
  • 20
0

Below is a simple example of the Node implementation, (i renamed Item to Value for readability purpose). It has to be implemented somehow like this, because methods signatures seems to be imposed to you. But keep in mind that this is definely not the best way to implement a LinkedList.

public class Node {
    public static final Node NIL = null;
    private Integer value;
    private Integer next;

    public Node(Integer value, Node next) {
        this.value = value;
        this.next = next;
    }

    public Integer getValue() {
        return this.value;
    }
    public Node getNext() {
        return this.next;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public boolean isLastNode() {
        return this.next == Node.NIL || Node;
    }
}


public class App {
    public static void main(String[] args) {
        Node lastNode = new Node(92, Node.NIL);
        Node secondNode = new Node(64, lastNode);
        Node firstNode = new Node(42, secondNode);

        Node iterator = firstNode;
        do () {
            System.out.println("node value : " + iterator.getValue());
            iterator = iterator.getNext();
        } while (iterator == null || !iterator.isLastNode());
    }
}
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
  • `Node.NIL` is an object, not a method call. As such, it won't result in a stack overflow. – Chai T. Rex Jan 30 '17 at 09:17
  • @ChaiT.Rex yes it is `Node NIL = new Node(Node.NIL, Node.NIL)`. Look closer, the constructor is going to call itself. To build properly, `Node.NIL` required `Node.NIL`. And it will result in a stackoverflow. – Anthony Raymond Jan 30 '17 at 09:19
  • I've actually run the code. It doesn't result in a stack overflow. You should probably run the code as well rather than guessing. – Chai T. Rex Jan 30 '17 at 09:21
  • Hum... right, i can't figure out why it does work, but, i'll edit my answer. Thanks. – Anthony Raymond Jan 30 '17 at 09:24
0

The node class that will be implemented changes according to the linked list you want to implement. If the linked list you are going to implement is circular, then you could just do the following:

public class Node {
    
    int data;
    Node next = null;
    
    public Node(int data){
        this.data = data;
    }
}

Then how are you going to implement the next node?

You are going to do it in the add method of the circularLinkedList class. You can do it as follows:

import java.util.*;

public class CircularLinkedList {
    
    public CircularLinkedList() {}
    
    public Node head = null;
    public Node tail = null;
    
    public void add(int data) {
        
        Node newNode = new Node(data);
        
        if(head == null) {
            head = newNode;

        }
        else {
        
            tail.next = newNode;
            
        }
        
        tail = newNode;
        tail.next = head;
    }
    
    public void displayList() {
        System.out.println("Nodes of the circular linked list: ");  
        
        Node current = head;
        
        if(head == null) {
            System.out.println("Empty list...");
        }
        
        else {
            
            do {
                
                System.out.print(" " + current.data);
                current = current.next;
                
            }while(current != head);
            System.out.println();
        }
        
    }
}