| 17 | System.out.println(result); |
| 18 | } |
| 19 | public static int test_lists() { |
| 20 | int result = 0; |
| 21 | // create a list of integers (Li1) from 1 to SIZE |
| 22 | LinkedList Li1 = new LinkedList(); |
| 23 | for (int i = 1; i < SIZE+1; i++) { |
| 24 | Li1.addLast(new Integer(i)); |
| 25 | } |
| 26 | // copy the list to Li2 (not by individual items) |
| 27 | LinkedList Li2 = new LinkedList(Li1); |
| 28 | LinkedList Li3 = new LinkedList(); |
| 29 | // remove each individual item from left side of Li2 and |
| 30 | // append to right side of Li3 (preserving order) |
| 31 | while (! Li2.isEmpty()) { |
| 32 | Li3.addLast(Li2.removeFirst()); |
| 33 | } |
| 34 | // Li2 must now be empty |
| 35 | // remove each individual item from right side of Li3 and |
| 36 | // append to right side of Li2 (reversing list) |
| 37 | while (! Li3.isEmpty()) { |
| 38 | Li2.addLast(Li3.removeLast()); |
| 39 | } |
| 40 | // Li3 must now be empty |
| 41 | // reverse Li1 |
| 42 | LinkedList tmp = new LinkedList(); |
| 43 | while (! Li1.isEmpty()) { |
| 44 | tmp.addFirst(Li1.removeFirst()); |
| 45 | } |
| 46 | Li1 = tmp; |
| 47 | // check that first item is now SIZE |
| 48 | if (((Integer)Li1.getFirst()).intValue() != SIZE) { |
| 49 | System.err.println("first item of Li1 != SIZE"); |
| 50 | return(0); |
| 51 | } |
| 52 | // compare Li1 and Li2 for equality |
| 53 | if (! Li1.equals(Li2)) { |
| 54 | System.err.println("Li1 and Li2 differ"); |
| 55 | System.err.println("Li1:" + Li1); |
| 56 | System.err.println("Li2:" + Li2); |
| 57 | return(0); |
| 58 | } |
| 59 | // return the length of the list |
| 60 | return(Li1.size()); |
| 61 | } |
| 62 | } |