java:inner_classes
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:inner_classes [2020/03/23 08:40] – gthanos | java:inner_classes [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| <code java LinkedList.java> | <code java LinkedList.java> | ||
| - | |||
| /* A linked list with sentinels at head and tail. | /* A linked list with sentinels at head and tail. | ||
| */ | */ | ||
| public class LinkedList< | public class LinkedList< | ||
| private Node< | private Node< | ||
| + | int size; | ||
| | | ||
| public LinkedList() { | public LinkedList() { | ||
| Line 20: | Line 20: | ||
| tail = new Node<> | tail = new Node<> | ||
| head.setNext(tail); | head.setNext(tail); | ||
| + | size = 0; | ||
| } | } | ||
| | | ||
| Line 67: | Line 68: | ||
| | | ||
| // append in the end | // append in the end | ||
| - | public | + | public |
| Node< | Node< | ||
| tail.getPrev().setNext(plus); | tail.getPrev().setNext(plus); | ||
| tail.setPrev(plus); | tail.setPrev(plus); | ||
| + | size++; | ||
| + | return true; | ||
| + | } | ||
| + | | ||
| + | public boolean add(int index, E elem) { | ||
| + | if(index> | ||
| + | return false; | ||
| + | Node< | ||
| + | for(int i=0; curr != tail && i<index; i++) { | ||
| + | curr = curr.next; | ||
| + | } | ||
| + | Node< | ||
| + | curr.prev.next = plus; | ||
| + | curr.prev = plus; | ||
| + | size++; | ||
| + | return true; | ||
| } | } | ||
| | | ||
| Line 93: | Line 110: | ||
| } | } | ||
| return -1; | return -1; | ||
| + | } | ||
| + | | ||
| + | int size() { | ||
| + | return size; | ||
| } | } | ||
| } | } | ||
| Line 106: | Line 127: | ||
| public static final int RANGE = 8 * SIZE; | public static final int RANGE = 8 * SIZE; | ||
| | | ||
| - | | + | static void print(LinkedList< |
| - | Random rand = new Random( new Date().getTime()); | + | |
| - | | + | |
| - | for(int i=0; i<SIZE; i++) { | + | |
| - | list.add(rand.nextInt(RANGE)); | + | |
| - | } | + | |
| Iterator< | Iterator< | ||
| | | ||
| Line 119: | Line 135: | ||
| } | } | ||
| System.out.println(); | System.out.println(); | ||
| + | } | ||
| + | | ||
| + | public static void main(String[] args) { | ||
| + | Random rand = new Random( new Date().getTime()); | ||
| + | LinkedList< | ||
| + | for(int i=0; i<SIZE; i++) { | ||
| + | int value = rand.nextInt(RANGE); | ||
| + | int pos = rand.nextInt(list.size()+1); | ||
| + | System.out.format(" | ||
| + | list.add(pos, | ||
| + | print(list); | ||
| + | } | ||
| + | print(list); | ||
| | | ||
| // Remove all elements less than RANGE/2 | // Remove all elements less than RANGE/2 | ||
| System.out.println(" | System.out.println(" | ||
| - | it = list.iterator(); | + | |
| while(it.hasNext()) { | while(it.hasNext()) { | ||
| int value = it.next(); | int value = it.next(); | ||
| Line 131: | Line 160: | ||
| } | } | ||
| } | } | ||
| + | print(list); | ||
| | | ||
| - | // Print list | ||
| - | it = list.iterator(); | ||
| - | while(it.hasNext()) { | ||
| - | System.out.print(it.next()+" | ||
| - | } | ||
| - | System.out.println(); | ||
| } | } | ||
| } | } | ||
java/inner_classes.1584952856.txt.gz · Last modified: 2020/03/23 08:40 by gthanos
