| 52 | } |
| 53 | |
| 54 | private boolean check() { |
| 55 | if (n < 0) { |
| 56 | return false; |
| 57 | } else if (n == 0) { |
| 58 | if (first != null) return false; |
| 59 | if (last != null) return false; |
| 60 | } else if (n == 1) { |
| 61 | if (first == null || last == null) return false; |
| 62 | if (first != last) return false; |
| 63 | if (first.next != null) return false; |
| 64 | } else { |
| 65 | if (first == null || last == null) return false; |
| 66 | if (first == last) return false; |
| 67 | if (first.next == null) return false; |
| 68 | if (last.next != null) return false; |
| 69 | |
| 70 | // check internal consistency of instance variable n |
| 71 | int numberOfNodes = 0; |
| 72 | for (Node x = first; x != null && numberOfNodes <= n; x = x.next) { |
| 73 | numberOfNodes++; |
| 74 | } |
| 75 | if (numberOfNodes != n) return false; |
| 76 | |
| 77 | // check internal consistency of instance variable last |
| 78 | Node lastNode = first; |
| 79 | while (lastNode.next != null) { |
| 80 | lastNode = lastNode.next; |
| 81 | } |
| 82 | if (last != lastNode) return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | public static void main(String[] args) { |
| 89 | LinkedQueue queue = new LinkedQueue(); |