![]() | Java Visualizer (beta: report a bug) |
Write your Java code here:
public class PKListDemo {
static class Item<T> {
T key;
Item next;
Item prev;
// Konstruktor
Item(T k, Item p) {
key = k;
next = p;
prev = null;
}
}
static class PKList<T> {
public Item head;
// Konstruktor
public PKList() {
head = null;
}
public boolean isEmpty() {
return (head == null);
}
public Item search (T k) {
Item x = head;
while (x != null && x.key != k) {
x = x.next;
}
return x;
}
public Item insert (T k) {
Item x = new Item<T>(k, head);
if (head != null) {
head.prev = x;
}
head = x;
return x;
}
public void delete (Item k) {
if (k.prev == null) {
// k ist head
head = k.next;
} else {
// k.prev != null
k.prev.next = k.next;
}
if (k.next != null) {
k.next.prev = k.prev;
}
}
}
public static void main(String[] args) {
int[] A = {4, 8, 15, 16, 23, 42};
PKList<Integer> list = new PKList<>();
for (int i = 0; i < A.length; i++) {
list.insert(A[i]);
}
Item item = list.search(16);
list.delete(item);
}
}
args
:
StdIn
)
basic examples | (Default) | Variables | CmdLineArgs | StdIn | ControlFlow | Sqrt | ExecLimit | Strings
method examples | PassByValue | Recursion | StackOverflow
oop examples | Rolex | Person | Complex | Casting
data structure examples | LinkedList | StackQueue | Postfix | SymbolTable
java feature examples | ToString | Reflect | Exception | ExceptionFlow | TwoClasses