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