PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/antlr-2.7.5/antlr/collections/impl/LList.java

http://github.com/bamboo/boo
Java | 134 lines | 73 code | 18 blank | 43 comment | 16 complexity | feb6471ef9f949f3173a5fe3b1c38b8e MD5 | raw file
Possible License(s): GPL-2.0
  1. package antlr.collections.impl;
  2. /* ANTLR Translator Generator
  3. * Project led by Terence Parr at http://www.jGuru.com
  4. * Software rights: http://www.antlr.org/license.html
  5. *
  6. * $Id: //depot/code/org.antlr/release/antlr-2.7.5/antlr/collections/impl/LList.java#1 $
  7. */
  8. import antlr.collections.List;
  9. import antlr.collections.Stack;
  10. import java.util.Enumeration;
  11. import java.util.NoSuchElementException;
  12. import antlr.collections.impl.LLCell;
  13. /**A Linked List Implementation (not thread-safe for simplicity)
  14. * (adds to the tail) (has an enumeration)
  15. */
  16. public class LList implements List, Stack {
  17. protected LLCell head = null, tail = null;
  18. protected int length = 0;
  19. /** Add an object to the end of the list.
  20. * @param o the object to add
  21. */
  22. public void add(Object o) {
  23. append(o);
  24. }
  25. /** Append an object to the end of the list.
  26. * @param o the object to append
  27. */
  28. public void append(Object o) {
  29. LLCell n = new LLCell(o);
  30. if (length == 0) {
  31. head = tail = n;
  32. length = 1;
  33. }
  34. else {
  35. tail.next = n;
  36. tail = n;
  37. length++;
  38. }
  39. }
  40. /**Delete the object at the head of the list.
  41. * @return the object found at the head of the list.
  42. * @exception NoSuchElementException if the list is empty.
  43. */
  44. protected Object deleteHead() throws NoSuchElementException {
  45. if (head == null) throw new NoSuchElementException();
  46. Object o = head.data;
  47. head = head.next;
  48. length--;
  49. return o;
  50. }
  51. /**Get the ith element in the list.
  52. * @param i the index (from 0) of the requested element.
  53. * @return the object at index i
  54. * NoSuchElementException is thrown if i out of range
  55. */
  56. public Object elementAt(int i) throws NoSuchElementException {
  57. int j = 0;
  58. for (LLCell p = head; p != null; p = p.next) {
  59. if (i == j) return p.data;
  60. j++;
  61. }
  62. throw new NoSuchElementException();
  63. }
  64. /**Return an enumeration of the list elements */
  65. public Enumeration elements() {
  66. return new LLEnumeration(this);
  67. }
  68. /** How high is the stack? */
  69. public int height() {
  70. return length;
  71. }
  72. /** Answers whether or not an object is contained in the list
  73. * @param o the object to test for inclusion.
  74. * @return true if object is contained else false.
  75. */
  76. public boolean includes(Object o) {
  77. for (LLCell p = head; p != null; p = p.next) {
  78. if (p.data.equals(o)) return true;
  79. }
  80. return false;
  81. }
  82. // The next two methods make LLQueues and LLStacks easier.
  83. /** Insert an object at the head of the list.
  84. * @param o the object to add
  85. */
  86. protected void insertHead(Object o) {
  87. LLCell c = head;
  88. head = new LLCell(o);
  89. head.next = c;
  90. length++;
  91. if (tail == null) tail = head;
  92. }
  93. /**Return the length of the list.*/
  94. public int length() {
  95. return length;
  96. }
  97. /** Pop the top element of the stack off.
  98. * @return the top of stack that was popped off.
  99. * @exception NoSuchElementException if the stack is empty.
  100. */
  101. public Object pop() throws NoSuchElementException {
  102. Object o = deleteHead();
  103. return o;
  104. }
  105. // Satisfy the Stack interface now.
  106. /** Push an object onto the stack.
  107. * @param o the object to push
  108. */
  109. public void push(Object o) {
  110. insertHead(o);
  111. }
  112. public Object top() throws NoSuchElementException {
  113. if (head == null) throw new NoSuchElementException();
  114. return head.data;
  115. }
  116. }