How to create a list of your own? Basically, I wrote it so that I can add an element to the list, but how to delete - I can’t imagine, more precisely, I imagine that the element needs to reassign knowledge about the next element, but how?
public class MyList<Robot> { class Node<R> { R item; Node<R> next; Node<R> previous; int id; Node() { item = null; next = null; previous = null; } Node(R item, Node<R> next) { this.item = item; this.next = next; id++; } Node(Node<R> previous, R item, Node<R> next) { this.previous = previous; this.item = item; this.next = next; id++; } boolean nextEnd() { return (item == null && next == null); } boolean previousEnd() { return (previous == null && item == null); } } private Node<Robot> top = new Node<>(); public void put(Robot item) { top = new Node<>(top, item, top); } public Robot last() { Robot result = top.item; if (!top.nextEnd()) top = top.next; return result; } public void remove(Robot item) { //TODO } } class Test { public static void main(String[] args) { MyList<String> list = new MyList<>(); for (String s1 : "Hack Brain and code".split(" ")) list.put(s1); String s5; list.remove("Hack"); while ((s5 = list.last()) != null) System.out.println(s5); } }
LinkedList? - Alex78191