![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS206 Data Structures / 2017-Spring / Notebooks |
class Node {
String data;
Node next;
Node(String data) {
this.data = data;
}
}
class SortedLinkedList {
Node head;
void insert(Node node) {
// This is wrong... just inserts at front:
node.next = head;
head = node;
}
void print() {
Node current = head;
while (current != null) {
printf("\"%s\", ", current.data);
current = current.next;
}
}
void delete(String search) {
Node current = head;
Node previous = null;
while (current != null) {
if (current.data == search) { // need to delete this one!
if (previous == null) { // first one
head = current.next;
} else {
previous.next = current.next;
}
break;
}
previous = current;
current = current.next;
}
}
}
SortedLinkedList sll = new SortedLinkedList();
sll.insert(new Node("A. Ham"));
sll.print()
sll.insert(new Node("A. Burr"));
sll.print();
sll.insert(new Node("Peggy Schuyler"));
sll.print();
sll.delete("A. Burr");
sll.print();