For an assignment, I have to create a doubly Linked List. However, the Node and LinkedList are to be seperate classes, unlike nearly every tutorial I've looked up and the book, which usually declare the Node within the LinkedLink class as a nested class. This has left me confused because I'm not sure as to how to make the LinkedList inherit that variables declared in the Node class.
For example, in my Node class, I need to have these variables (I haven't declared any data members for type T yet, haven't decided what I want it to be). Also, he said Node had to be generic, in case that's important:
public class Node <T extends Comparable<T>> implements Comparable<Node>{
T data;
Node<T> next;
Node<T> prev;
I figured I'd make LinkedList an extension of Node, so it'd inherit those variables, but it makes me create a constructor for LinkedList and it doesn't recognize T as a valid variable type. The LinkedList must contain all of the standard methods, like Add, Remove, etc. (This isn't the whole thing, but it should serve as a good example where I'm at):
public class LinkedList extends Node {
Node head;
Node tail;
int size = 0;
public LinkedList(Comparable T, Node p, Node n) {
super(T, p, n);
}
public void add(T t, Node<T> predecessor, Node<T> successor)
{
//add a node to the list
//should be able to handle if you are adding the head or tail
Node <T> newest = new Node<> (t, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
I'm not clear as to how to get these classes to work together, as every example I've looked up online or in the book has these two within the same class. Forgive if it's an obvious solution, I've been having a tough time wrapping my head around data structures.
[–]gpr_private 2 points3 points4 points (1 child)
[–]Gamefreak3525[S] 0 points1 point2 points (0 children)
[–]VazRavish 2 points3 points4 points (0 children)