1 package antlr.collections.impl; 2 3 9 10 import antlr.collections.List; 11 import antlr.collections.Stack; 12 13 import java.util.Enumeration ; 14 import java.util.NoSuchElementException ; 15 16 import antlr.collections.impl.LLCell; 17 18 21 public class LList implements List, Stack { 22 protected LLCell head = null, tail = null; 23 protected int length = 0; 24 25 26 29 public void add(Object o) { 30 append(o); 31 } 32 33 36 public void append(Object o) { 37 LLCell n = new LLCell(o); 38 if (length == 0) { 39 head = tail = n; 40 length = 1; 41 } 42 else { 43 tail.next = n; 44 tail = n; 45 length++; 46 } 47 } 48 49 53 protected Object deleteHead() throws NoSuchElementException { 54 if (head == null) throw new NoSuchElementException (); 55 Object o = head.data; 56 head = head.next; 57 length--; 58 return o; 59 } 60 61 66 public Object elementAt(int i) throws NoSuchElementException { 67 int j = 0; 68 for (LLCell p = head; p != null; p = p.next) { 69 if (i == j) return p.data; 70 j++; 71 } 72 throw new NoSuchElementException (); 73 } 74 75 76 public Enumeration elements() { 77 return new LLEnumeration(this); 78 } 79 80 81 public int height() { 82 return length; 83 } 84 85 89 public boolean includes(Object o) { 90 for (LLCell p = head; p != null; p = p.next) { 91 if (p.data.equals(o)) return true; 92 } 93 return false; 94 } 95 97 100 protected void insertHead(Object o) { 101 LLCell c = head; 102 head = new LLCell(o); 103 head.next = c; 104 length++; 105 if (tail == null) tail = head; 106 } 107 108 109 public int length() { 110 return length; 111 } 112 113 117 public Object pop() throws NoSuchElementException { 118 Object o = deleteHead(); 119 return o; 120 } 121 123 126 public void push(Object o) { 127 insertHead(o); 128 } 129 130 public Object top() throws NoSuchElementException { 131 if (head == null) throw new NoSuchElementException (); 132 return head.data; 133 } 134 } 135 | Popular Tags |