Java Visualizer
(beta: report a bug)

Write your Java code here:

71
 
1
public class PKListDemo {
2
     
3
   static class Item<T> {
4
      T key;
5
      Item next;
6
      Item prev;
7
8
      // Konstruktor
9
      Item(T k, Item p) {
10
         key = k;
11
         next = p;
12
         prev = null;
13
      }
14
   }
15
   
16
  static class PKList<T> {
17
     
18
     public Item head;
19
     
20
     // Konstruktor
21
     public PKList() {
22
       head = null;
23
     }
24
     
25
     public boolean isEmpty() {
26
       return (head == null);
27
     }
28
     
29
     public Item search (T k) {
30
       Item x = head;
31
       while (x != null && x.key != k) {
32
         x = x.next;
33
       }
34
       return x;
35
     }
36
     
37
     public Item insert (T k) {
38
       Item x = new Item<T>(k, head);
39
       if (head != null) {
40
         head.prev = x;
41
       }
42
       head = x;
43
       return x;
44
     }
45
     
46
     public void delete (Item k) {
47
       if (k.prev == null) {
48
         // k ist head
49
         head = k.next;
50
       } else {
51
         // k.prev != null
52
         k.prev.next = k.next;
53
       }
54
       if (k.next != null) {
55
         k.next.prev = k.prev;
56
       }
57
     }
58
  }
59
  
60
   public static void main(String[] args) {
61
      int[] A = {4, 8, 15, 16, 23, 42};
62
      PKList<Integer> list = new PKList<>();
63
      
64
      for (int i = 0; i < A.length; i++) {
65
        list.insert(A[i]);
66
      }
67
      
68
      Item item = list.search(16);
69
      list.delete(item);
70
   }  
71
}
args:
(also visualizes consumption of 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

The visualizer supports StdIn, StdOut, most other stdlib libraries, Stack, Queue, and ST.