LinkedList/LinkedList.java
2026-01-14 08:56:37 +01:00

115 lines
2.1 KiB
Java

public class LinkedList {
private Node first;
private Node last;
private Node current;
private int size;
public LinkedList() {}
public void toFirst() {
current = first;
}
public void toLast() {
current = last;
}
public Node getFirst() {
return first;
}
public Node getLast() {
return last;
}
public void toNext() {
if (hasAccess()) {
current = current.getNextNode();
}
}
public boolean isEmpty() {
if (first == null && last == null) {
return true;
}
return false;
}
public boolean hasAccess() {
if (current == null) {
return false;
}
return true;
}
public void append(Object content) {
Node newNode = new Node(content);
if (isEmpty()) {
first = newNode;
last = newNode;
current = newNode;
} else {
last.setNextNode(newNode);
last = newNode;
}
size++;
}
public int getSize() {
return size;
}
public void insert(Object content, Object prev) {
toFirst();
while(current.getContent() != prev && hasAccess()) {
toNext();
}
if (hasAccess()) {
Node newNode = new Node(content);
newNode.setNextNode(current.getNextNode());
current.setNextNode(newNode);
size++;
}
}
public void remove() {
if (hasAccess()) {
if (first == last) {
first = null;
last = null;
current = null;
return;
}
Node newCurrent = current.getNextNode();
Node pointer = first;
while(pointer.getNextNode() != current) {
pointer = pointer.getNextNode();
}
current = newCurrent;
pointer.setNextNode(current);
}
size--;
}
public void concat(LinkedList weaponList) {
if (!isEmpty()) {
last.setNextNode(weaponList.getFirst());
last = weaponList.getLast();
size = weaponList.getSize();
return;
}
first = weaponList.getFirst();
last = weaponList.getFirst();
current = first;
size += weaponList.getSize();
}
public Object getCurrentContent() {
return current.getContent();
}
}