- #1
Robben
- 166
- 2
Homework Statement
I am implementing a singly linked list but when I am in my main method, to add into my singly linked list, nothing happens.
Homework Equations
[/B]
None
The Attempt at a Solution
I am trying to implement a singly linked list.
My singly linked list, where I implemented a class named linkedlist, that I defined (this implementation of linkedlist is not the java class linkedlist, it is my own version of linkedlist):
Code:
public class SinglyLinkedList<E> implements LinkedList<E> {
public class Node<E> {
public Node<E> next;
public E element;
public Node(E element) {
this.element = element;
}
public Node (E element, Node<E> next) {
this.element = element;
this.next = next;
}
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setElement(E element) {
this.element = element;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
public Node<E> head;
public Node<E> tail;
public int total;
public SinglyLinkedList() {
this.head = null;
this.tail = null;
total = 0;
}
public void add(E element) {
Node<E> singlyAdd = new Node<E>(element);
if (tail == null) {
head = singlyAdd;
tail = singlyAdd;
} else {
tail.setNext(singlyAdd);
tail = singlyAdd;
}
total++;
}
}
But when I go to my main method to add into my SinglyLinkedList, it doesn't add anything. I am wondering what is wrong with my code?
Last edited: