Linked List Inner Class - java

I am fairly new to Java and I need to make a linked list. I was told that I need to make a class called "Node" to store each element. I've written the class:
public class Node()
{
public T data;
public Node next;
public Node(T data, Node next)
{
this.data = data;
this.next = next;
}
public Node getNext()
{
return next;
}
public T getData()
{
return data;
}
}
However, I am not allowed to make a separate class, it has to be within the LinkedList class. I understand how to make an inner class of something like an imported iterator, but the "Node" is not imported. How would I do this?

public class Linked{
class Node{
int value;
Node next;
Node(int data,Node next){
value=data;
this.next=next;
}
Node getNext(){
return next;}
}
//Rest of the linked list methods
}
This is called an inner class which is nothing but a class nested inside another class. When we know that a class has no existence outside the main class we nest that class inside the main class i.e as we know that this node class cannot be used outside anywhere in the program thus we made this class an inner class.

Related

Tree Subclass with node subclass?

I'm writing this in java.
I've got a BinaryTree Class. It has functions that use the Node class.
I've written a subclass of Binary Tree (TraversalTree) and I've got a subclass of the Node class (OrderNode).
How do I get the TraversalTree functions that inherit from BinaryTree to use the subclass OrderNode??
public class BinaryTree {
public Node root; // starting node of the tree
public Node getRoot() {
return root;
}
}
public class Node {
private int value;
private Node left, right, parent;
//constructor
public Node(int value) {
this.value = value; // data value to be stored in node
left = null; // left child
right = null; // right child
parent = null; // parent node
}
}
So here is the subclass OrderNode
public class OrderNode extends Node {
public int preOrder,postOrder, inOrder;
public OrderNode(int value) {
super(value);
}
}
And then in this subclass of BinaryTree, TraversalTree. I want it to have the subclass OrderNode replace all the Node calls in the BinaryTree functions.
public class TraversalTree extends BinaryTree {
}
I've tried to search this a bunch but I'm hitting a wall. Perhaps I haven't been able to find the right search terms. But either way I can't seem to find any info on this.
You can use super to access the super variables and assign sub class object in super class reference variables.
class TraversalTree extends BinaryTree {
public TraversalTree(OtherNode otherNode){
super.node=otherNode;
}
public OtherNode getRoot(){
return (OtherNode)super.getRoot();
}
}
This should replace all node with otherNode.

Java generic constructor

Node( T itemArg, Node<T> nextArg ) {
this.item = itemArg;
this.next = nextArg;
}
Is this the right way to represent generic constructor
This is correct, as long as the parameter is specified in the class definition.
EDIT: Ensure item is an instance of T, and next is an instance of Node<T>.
public class Node<T> {
T item;
Node<T> next;
//your constructor
}

Why methods returns object of interface type?

I am trying to understand the way, how interface works. After doing some research, I found that interfaces are used to specify what a class must do.
I implemented a method(first())in outer class which will return element of Position<E> instance, but the main point where I get confused is, first() uses a method getNext() from Node class which returns Node<E> object, so why I am able to return Position<E> object instead of Node<E> and I can even return Node<E> object from first() method.
private static class Node<E> implements Position<E> {// Inner Class
private E element;
Node<E> previous;
Node<E> next;
Node(E element, Node<E> previous, Node<E> next) {
this.element = element;
this.previous = previous;
this.next = next;
}
#Override
public E getElement() throws IllegalStateException {
if (next == null)
throw new IllegalStateException("Position no longer valid");
return element;
}
private Node<E> getNext() {
return next;
}
}
Outer class method
#Override
public Position<E> first() {
return header.getNext();
}
Since Node<E> implements Position<E>, each instance of Node<E> is also an instance of Position<E> (or, to be exact, an instance of a class that implements Position<E>).
Therefore you can return an instance of Node<E> in a method whose return type is Position<E>.
why I am able to return Position object instead of Node
Because, Node implements Position and that implies Node is a type of Position. So, Position can hold any class instance, who implemented it. In your case, it is Node class, referring to getNext method, which returns Node type object
private Node<E> getNext() {
return next;
}

Generic Iterator implementation in java

I have the following design:
I have an Abstract class Instance,
I have a class Library that extends Instance and
I have a class File that also extends Instance
I've created my own linked list implementation and it's defined as follows:
public class List<T extends Instance> implements Iterable {
//some other code here
public Iterator iterator(){
return new ListIterator(this);
}
now I've created a class
public class ListIterator<T extends Instance> implements Iterator<T> {
private List thisList;
private Node current;
public ListIterator(List l){
thisList=l;
current=thisList.head.next;
}
#Override
public boolean hasNext() {
if(current==null)
return false;
return false;
}
#Override
public T next() {
Node temp=current;
current=current.next;
return temp.data;
}
}
Where Node is
public class Node<T extends Instance> {
public Node<T> next;
public Node<T> prev;
public T data;
public Node(T data,Node prev, Node next){
this.data=data;
this.prev=prev;
this.next=next;
}
}
so my problem is as follows: the line return temp.data rises an error:
Type mismatch - cannot convert from Instance to T.
What is wrong with this code?
I'd say that Node.data is a reference to an Instance object? If that is the case, the compiler can't automatically change an Instance to a T, because even though T is an Instance object (T extends Instance), any given Instance might not be a T.
The Java Generics tutorial explains it: http://docs.oracle.com/javase/tutorial/extra/generics/subtype.html
Also, in your List<T> class, you should be specifying Iterator and ListIterator as generic using Iterator<T> and ListIterator<T>, or else the compiler won't be able to handle the generics properly. Your Node reference also needs to be generic: Node<T>
Hence you should be using
private Node<T> current;
and
public T next() {
Node<T> temp=current;
current=current.next;
return temp.data;
}
The compiler will usually warn you when you're using a raw type for a generic class.
Did no one notice the bug:
public boolean hasNext() {
if(current==null)
return false;
return false;
}
This is an invariant. Unless I am missing something, the iterator will very quickly return 0 elements!

Java Generics 'Incompatible Type' Compile-Time Error

For a CS class I am writing a linked list implementation of a linked list interface created by my professor. The assignment requires us to use generics for the list. What I have created, I think, is pretty standard.
public class MyLinkedList<T> implements ADTListInterface {
...
private class Node<T> {
Node<T> head;
Node<T> prev;
public Node(int max) {
...
}
public void shift() {
...
Node<T> newNode = new Node<T>(this.max);
newNode.prev = head.prev;
...
}
}
...
}
At compile time following error is generated:
MyLinkedList.java:111: incompatible types
found : MyLinkedList<T>.Node<T>
required: MyLinkedList<T>.Node<T>
newNode.prev = head.prev;
This error has me very confused. Can anyone explain to me what the issue is?
Here is probably the problem:
private class Node<T> {
The <T> is causing extra problems. Because Node is an inner class, it doesn't need to declare its generic type again.
You should declare the Node class like below:
public class MyLinkedList<T> implements ADTListInterface {
...
private class Node {
Node head;
Node prev;
public Node(int max) {
...
}

Categories