链表

  1 public class MyLinkedList<E> extends MyAbstractList<E> {
  2   private Node<E> head, tail;
  3 
  4   /** Create a default list */
  5   public MyLinkedList() {
  6   }
  7 
  8   /** Create a list from an array of objects */
  9   public MyLinkedList(E[] objects) {
 10     super(objects);
 11   }
 12 
 13   /** Return the head element in the list */
 14   public E getFirst() {
 15     if (size == 0) {
 16       return null;
 17     }
 18     else {
 19       return head.element;
 20     }
 21   }
 22 
 23   /** Return the last element in the list */
 24   public E getLast() {
 25     if (size == 0) {
 26       return null;
 27     }
 28     else {
 29       return tail.element;
 30     }
 31   }
 32 
 33   /** Add an element to the beginning of the list */
 34   public void addFirst(E e) {
 35     Node<E> newNode = new Node<E>(e); // Create a new node
 36     newNode.next = head; // link the new node with the head
 37     head = newNode; // head points to the new node
 38     size++; // Increase list size
 39 
 40     if (tail == null) // the new node is the only node in list
 41       tail = head;
 42   }
 43 
 44   /** Add an element to the end of the list */
 45   public void addLast(E e) {
 46     Node<E> newNode = new Node<E>(e); // Create a new for element e
 47 
 48     if (tail == null) {
 49       head = tail = newNode; // The new node is the only node in list
 50     }
 51     else {
 52       tail.next = newNode; // Link the new with the last node
 53       tail = tail.next; // tail now points to the last node
 54     }
 55 
 56     size++; // Increase size
 57   }
 58 
 59 
 60   /** Add a new element at the specified index in this list
 61    * The index of the head element is 0 */
 62   public void add(int index, E e) {
 63     if (index == 0) {
 64       addFirst(e);
 65     }
 66     else if (index >= size) {
 67       addLast(e);
 68     }
 69     else {
 70       Node<E> current = head;
 71       for (int i = 1; i < index; i++) {
 72         current = current.next;
 73       }
 74       Node<E> temp = current.next;
 75       current.next = new Node<E>(e);
 76       (current.next).next = temp;
 77       size++;
 78     }
 79   }
 80 
 81   /** Remove the head node and
 82    *  return the object that is contained in the removed node. */
 83   public E removeFirst() {
 84     if (size == 0) {
 85       return null;
 86     }
 87     else {
 88       Node<E> temp = head;
 89       head = head.next;
 90       size--;
 91       if (head == null) {
 92         tail = null;
 93       }
 94       return temp.element;
 95     }
 96   }
 97 
 98   /** Remove the last node and
 99    * return the object that is contained in the removed node. */
100   public E removeLast() {
101     if (size == 0) {
102       return null;
103     }
104     else if (size == 1) {
105       Node<E> temp = head;
106       head = tail = null;
107       size = 0;
108       return temp.element;
109     }
110     else {
111       Node<E> current = head;
112 
113       for (int i = 0; i < size - 2; i++) {
114         current = current.next;
115       }
116 
117       Node<E> temp = tail;
118       tail = current;
119       tail.next = null;
120       size--;
121       return temp.element;
122     }
123   }
124 
125   /** Remove the element at the specified position in this list.
126    *  Return the element that was removed from the list. */
127   public E remove(int index) {
128     if (index < 0 || index >= size) {
129       return null;
130     }
131     else if (index == 0) {
132       return removeFirst();
133     }
134     else if (index == size - 1) {
135       return removeLast();
136     }
137     else {
138       Node<E> previous = head;
139 
140       for (int i = 1; i < index; i++) {
141         previous = previous.next;
142       }
143 
144       Node<E> current = previous.next;
145       previous.next = current.next;
146       size--;
147       return current.element;
148     }
149   }
150 
151   /** Override toString() to return elements in the list */
152   public String toString() {
153     StringBuilder result = new StringBuilder("[");
154 
155     Node<E> current = head;
156     for (int i = 0; i < size; i++) {
157       result.append(current.element);
158       current = current.next;
159       if (current != null) {
160         result.append(", "); // Separate two elements with a comma
161       }
162       else {
163         result.append("]"); // Insert the closing ] in the string
164       }
165     }
166 
167     return result.toString();
168   }
169 
170   /** Clear the list */
171   public void clear() {
172     head = tail = null;
173   }
174 
175   /** Return true if this list contains the element o */
176   public boolean contains(E e) {
177     Node<E> previous = head;
178 
179     for (int i = 1; i < size; i++) {
180         if(previous.element == e)
181             return true;
182         previous = previous.next;
183     } 
184     return false;
185   }
186 
187   /** Return the element from this list at the specified index */
188   public E get(int index) {
189       if(size == 0) return null;
190       else if(index < 0 || index >= size )return null;
191       else
192       {
193           Node<E> previous = head;
194           for(int i = 1; i <= index; i++)
195               previous = previous.next;
196           return previous.element;
197       }
198   }
199 
200   /** Return the index of the head matching element in this list.
201    *  Return -1 if no match. */
202   public int indexOf(E e) {
203      
204     Node<E> previous = head;
205     for(int i = 0; i < size; i++){
206         if(previous.element == e)
207             return i;
208         previous = previous.next;
209       }
210     return -1;
211   }
212 
213   /** Return the index of the last matching element in this list
214    *  Return -1 if no match. */
215   public int lastIndexOf(E e) {
216       int index = -1;
217       Node<E> previous = head;
218       for(int i = 0; i < size; i++){
219           if(previous.element == e)
220              index  = i;
221           previous = previous.next;
222       }
223       return index;
224   }
225 
226   /** Replace the element at the specified position in this list
227    *  with the specified element. */
228   public E set(int index, E e) {
229       if(index < 0 || index > size) return null;
230       if(index == 0) {
231           E old = head.element;
232           head.element = e;
233           return old;
234       }
235       else 
236           {
237               Node<E> newNode = new Node<E>(e);
238               Node<E> previous = head;
239               for(int i = 1; i < index; i++)
240                   previous = previous.next;
241               Node<E> temp = previous.next;
242               E old = temp.element;
243               newNode.next = temp.next;
244               previous.next = newNode;
245               return old;
246           }
247       
248   }
249 
250   private static class Node<E> {
251     E element;
252     Node<E> next;
253 
254     public Node(E element) {
255       this.element = element;
256     }
257   }
258 }

 

posted @ 2013-11-12 11:25  soul390  阅读(161)  评论(0编辑  收藏  举报