Subato

LinkedListRemoveSecondLast

Lösche das vorletzte Element

Gegeben sei eine einfach verkettete Liste mit separaten Verwaltungsknoten head und tail, wie aus der Veranstaltung bekannt.

Fügen Sie eine Methode removeSecondLast() hinzu, die das vorletzte Element der gegebenen Liste löscht - soweit existent. Die Knoten head und tail zählen hierbei natürlich nicht als Elemente der Liste.

Wenn kein vorletztes Element existiert, ist die Liste *nicht* zu ändern.


package de.hsrm.ads; public class LinkedListRemoveSecondLast { class Node { Node next; int obj; } Node head, tail; public LinkedListRemoveSecondLast() { head = new Node(); tail = new Node(); head.next = tail; } public void addFirst(int t) { Node n = new Node(); n.obj = t; n.next = head.next; head.next = n; } // prints the list to stout public void print() { Node n = head.next; while (n != tail) { System.out.println("> " + String.valueOf(n.obj)); n = n.next; } System.out.println(); } public void removeSecondLast() { // FIXME } public static void main(String[] args) { LinkedListRemoveSecondLast l = new LinkedListRemoveSecondLast(); l.addFirst(5); l.addFirst(1); l.addFirst(4); l.addFirst(3); l.addFirst(2); l.print(); // should be: 2 3 4 1 5 l.removeSecondLast(); l.print(); // should be: 2 3 4 5 } }
java
You are not logged in and therefore you cannot submit a solution.