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); } } 
  • What are you not LinkedList ? - Alex78191
  • I have a task - I need to write the list myself, I wrote a part - to add an element, but how to delete it - I don't know - Space Digi

1 answer 1

 public remove(Node<R> e) { e.previous.next = e.next; e.next.previous = e.previous; e.next=null; } 

The original implementation of LinkedList.

  • But how can I call this bump from the tester, what should I pass to the parameters? - Space Digi
  • @HackBrain I don't know what the tester is. - Alex78191
  • Well, I have a second class tester called with the method of main - Space Digi
  • @HackBrain is obvious that you need to pass the element Node<R> - Alex78191
  • How do I pass it from the tester class ??? - Space Digi