This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Next revision Both sides next revision | ||
|
java:inner_classes [2019/04/02 20:20] gthanos |
java:inner_classes [2019/04/02 20:38] gthanos |
||
|---|---|---|---|
| Line 8: | Line 8: | ||
| <code java LinkedList.java> | <code java LinkedList.java> | ||
| + | |||
| + | /* A linked list with sentinels at head and tail. | ||
| + | */ | ||
| public class LinkedList< | public class LinkedList< | ||
| private Node< | private Node< | ||
| Line 27: | Line 30: | ||
| } | } | ||
| | | ||
| - | public Node< | + | public Node< |
| public void setNext(Node< | public void setNext(Node< | ||
| public Node< | public Node< | ||
| Line 37: | Line 40: | ||
| private class Iterator< | private class Iterator< | ||
| Node< | Node< | ||
| + | | ||
| public Iterator(Node< | public Iterator(Node< | ||
| curr = c; | curr = c; | ||
| Line 52: | Line 56: | ||
| curr.getPrev().setNext(curr.getNext()); | curr.getPrev().setNext(curr.getNext()); | ||
| curr.getNext().setPrev(curr.getPrev()); | curr.getNext().setPrev(curr.getPrev()); | ||
| - | curr = null; | + | curr = curr.getPrev(); |
| } | } | ||
| } | } | ||
| Line 97: | Line 101: | ||
| public class LinkedListUsage { | public class LinkedListUsage { | ||
| - | public static final int SIZE = 20; | + | public static final int SIZE = 16; |
| + | public static final int RANGE = 8 * SIZE; | ||
| | | ||
| public static void main(String[] args) { | public static void main(String[] args) { | ||
| Line 103: | Line 108: | ||
| LinkedList< | LinkedList< | ||
| for(int i=0; i<SIZE; i++) { | for(int i=0; i<SIZE; i++) { | ||
| - | list.add(rand.nextInt(8*SIZE)); | + | list.add(rand.nextInt(RANGE)); |
| } | } | ||
| Iterator< | Iterator< | ||
| + | | ||
| + | // Print list | ||
| + | while(it.hasNext()) { | ||
| + | System.out.print(it.next()+" | ||
| + | } | ||
| + | System.out.println(); | ||
| + | | ||
| + | // Remove all elements less than RANGE/2 | ||
| + | System.out.println(" | ||
| + | it = list.iterator(); | ||
| + | while(it.hasNext()) { | ||
| + | int value = it.next(); | ||
| + | // | ||
| + | if(value < RANGE/2) { | ||
| + | // | ||
| + | it.remove(); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | // Print list | ||
| + | it = list.iterator(); | ||
| while(it.hasNext()) { | while(it.hasNext()) { | ||
| System.out.print(it.next()+" | System.out.print(it.next()+" | ||
| Line 112: | Line 138: | ||
| } | } | ||
| } | } | ||
| + | |||
| </ | </ | ||