The main type of Linked List Queue that extends the base Node <T> in Java + Requesting Laymans Terms
Firstly, yes, this is for the assignment at school, but I'm not looking for someone to rewrite or change my code. My question is:
I am invited to write a class that creates a queue that extends Node (the latter, as shown below)
public class Node<T>{
protected T data;
protected Node<T> next;
}
I wrote (most likely, very rude) a method for this and a basic test program that stores integer types in a queue (hopefully). I don’t know all the professional jargon, I read the “generics” documentation, but I might have missed the key point, I read how the linked list works (their examples are more in the Node class, something I’m not allowed to edit this task), and also circular arrays, etc. when I run my code, I get an error that I did not expect from the types. I will post my corresponding code, can someone please explain what I did to get this (rather ... where would I not use in my code?)
public class Queue<T> extends Node<T> {
public Node base;
public Node end;
public void enqueue(T item) {
Node newItem = new Node();
newItem.data = item;
newItem.next = null;
if (isEmpty()) { //Sets to item count of 1
this.base = newItem; //Base becomes the new item
this.base.next = this.end; //Base Points Next as End
this.end.next = this.base; //End points to the one before it (base)
}
else { //Sets to item count above 1.
this.end.next.next = newItem; //The Last Item Now Points Next as the New Item (End is never counted as an item)
this.end.next = newItem; //End now points to the next final item.
}
}
public T dequeue() {
if (isEmpty()) {
return (null);
}
else {
T item = this.base.data;
if (this.base.next == this.end) {
this.base = null;
this.end = null;
}
else {
this.base = this.base.next;
}
return (item);
}
}
public int size() {
int count = 0;
for (Node node = base; node != null; node = node.next) {
count++;
}
return count;
}
public boolean isEmpty() {
return (base == null);
}
public Queue() {
this.base = null;
this.end = null;
}
}
Code TestQueue.java:
public class TestQueue {
public static void main(String args[]) {
QueueStuff<Integer> firstQueue = new QueueStuff<>();
firstQueue.enqueue (66);
firstQueue.enqueue (6);
firstQueue.enqueue (666);
firstQueue.enqueue (0);
firstQueue.enqueue (6666);
//firstQueue.printQueue();
}
}
And the error is this:
incompatible types.
T item = this.base.data;
^
required: T
found: Object
where T is a Type variable: T extends Object declared in class Queue