



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
2
public class MyLinkedList { private Node first; private class Node { public Object data; public Node next; }
public MyLinkedList() { first = null; }
public Object addFirst() {
... } ... }
3
MyLinkedList mll = new MyLinkedList();
mll.addFirst("D");
mll.addFirst("C");
mll.addFirst("B");
mll.addFirst("A");
mll.print(); // prints out โAโ, โBโ, โCโ, โDโ
4
public void printHelper(Node n) { if (n == null ) { return ; } else { String s = (String) n.data; // sloppy System. out .println(s); printHelper(n.next); } }
public void print() { printHelper(first); }
7
MyLinkedList mll = new MyLinkedList();
mll.addFirst("D");
mll.addFirst("C");
mll.addFirst("B");
mll.addFirst("A");
mll.removeAlternate(); // results in a list with โAโ, โCโ
8
9
public void removeAlternateHelper(Node n) {
if ( n == null ) { return ; } else { Node next = n.next; if ( next == null ) { return ; } else { n.next = next.next; removeAlternateHelper(next.next); } }
}
public void removeAlternate() {
removeAlternateHelper(first);
}