import java.io.Serializable;import java.util.*;public class MyLinkedList implements List, Serializable, Cloneable { /** * */ private static final long serialVersionUID = 1L; ListNode head; int size; //inner class for ListNode private class ListNode { private Object data; private ListNode next; private ListNode(Object d) { this.data = d; this.next = null; } private ListNode (Object d, ListNode next){ this.data = d; this.next = next; } private ListNode() { } } public MyLinkedList() { this.head = new ListNode(null); this.size = 0; } public void addFirst(Object data){ this.head = new ListNode(data, head); this.size++; } public void addLast (Object data){ if(isEmpty()) addFirst(data); else { ListNode cur = this.head; while(cur.next != null){ cur = cur.next; } cur.next = new ListNode(data,null); this.size++; } } @Override public boolean equals(Object o){ if(o == null){ throw new NullPointerException(“NULL parameter passed on call to equals(Object)”); } if(o == this){ return true; } if(o instanceof MyLinkedList){ MyLinkedList list = (MyLinkedList)o; if(this.size == 0 && list.size() == 0){ return true; } if(this.size == list.size()){ boolean ret = true; ListNode cur, cur2; cur = this.head.next; cur2 = list.head.next; for(; cur!= null && cur2!= null; cur = cur.next, cur2 = cur2.next){ if(cur.data.equals(cur2.data) != true){ ret = false; } }// end for return ret; }// end if(size == size) else{ return false; } }// end if(instanceof) return false; }// end equals() @Override public Iterator iterator() { return new MyLinkedListIterator(this.head); } /* @Override public int hashCode() { int value; return value; }*/ @Override public int size() { return this.size; } @Override public boolean isEmpty() { return size == 0; } @Override public boolean contains(Object o) { Iterator it = this.iterator(); while(it.hasNext()) { Object temp = it.next(); if(temp != null && o != null && temp.equals(o)) { return true; } else if(temp == null && o == null) { return true; } } return false; } @Override public Object toArray() { throw new UnsupportedOperationException(); } @Override public Object toArray(Object a) { throw new UnsupportedOperationException(); } @Override public boolean remove(Object o) { for(ListNode prev = this.head, walk = this.head.next; walk != null; prev = walk,walk = walk.next){ if (walk.data.equals(o)) { //should override equals in your class Object prev.next = walk.next; this.size –; return true; } }//end for return false; } @Override public boolean containsAll(Collection c) { for(Object e : c) { if (!contains(e)) return false; } return true; } @Override public boolean addAll(Collection c) throws NullPointerException { if ( c == null ) { throw new NullPointerException(“Collection passed in is null!”); } Iterator itr= c.iterator(); while(itr.hasNext()){ add(itr.next()); this.size ++; } return true; } @Override public boolean addAll(int index, Collection c) { // TODO Auto-generated method stub if (c == null) { throw new NullPointerException(); } /* if (index < 0 || index > size) { throw new IndexOutOfBoundsException(); } ListNode temp = new ListNode(c); for(int i = 0; i thisIt = this.iterator(); if(c == null){ throw new NullPointerException(); } while(thisIt.hasNext()) { if( c.contains(thisIt.next()) ) { try{ thisIt.remove(); this.size –; changed = true; }catch(UnsupportedOperationException ex) { throw new UnsupportedOperationException(); } } }//end of while return changed; } @Override public boolean retainAll(Collection c) { // TODO Auto-generated method stub if (c==null) throw new NullPointerException(); if (this.head == null) return true; ListNode retainList = null; ListNode temp = this.head; while(temp != null){ if(c.contains(temp)){ if (retainList == null){ retainList = temp; } else{ retainList.next= temp; retainList = retainList.next; } } temp = temp.next; } head = retainList; return true; } @Override public void clear() { // TODO Auto-generated method stub head = null; size = 0; } @Override public Object get(int index) throws IndexOutOfBoundsException{ if(index < 0 || index >= size()) { throw new IndexOutOfBoundsException(“Provided index is out of bounds! ” + index); } ListNode walk = head; for(int cur = 0; cur < index; walk = walk.next, cur ++){ //empty loop body } return walk.data; } @Override public Object set(int index, Object element) { // TODO Auto-generated method stub if (index < 0 || index > size) throw new IndexOutOfBoundsException(“Provided index is out of bounds! ” + index); ListNode temp = new ListNode(element); ListNode cur = this.head.next; ListNode prev = this.head; if (index == 0){ prev.next = temp; temp.next = cur.next; System.out.println(“the result will show this will change the index 1, due to dummy node is at index 0!”); } else{ for(int i = 1; i < index ; i++){ prev = prev.next; cur = cur.next; } prev.next = temp; temp.next = cur.next; } /* for (int i = 0; i < index; i++) { curr = curr.next; } curr = */ return null; } @Override public void add(int index, Object element) { // TODO Auto-generated method stub ListNode temp = new ListNode(element); ListNode curr = head; if (index == 0){ temp.next = head; this.head = temp; } else{ for(int i = 1; i < index ; i++){ curr=curr.next; } temp.next = curr.next; curr.next = temp; } this.size++; } @Override public Object remove(int index) throws IndexOutOfBoundsException { if(index < 0 || index >= size()) { throw new IndexOutOfBoundsException(“Provided index is out of bounds! ” + index); } ListNode walk = head.next; ListNode prev = head; for(int cur = 0; cur < index; cur ++){ prev = walk; walk = walk.next; } this.size --; prev.next = walk.next; return walk.data; } @Override public int indexOf(Object o) { // TODO Auto-generated method stub ListNode curr = head.next; for (int i = 0; i < size; i++){ if( o == null){ if(curr.data == null){ return i; } } else{ if(o.equals(curr.data)){ return i+1; } } curr = curr.next; } return -1; } @Override public int lastIndexOf(Object o) { // TODO Auto-generated method stub ListNode curr = head.next; int lastindex = 0; for (int i = 0; i < size; i++){ if( o == null){ if(curr.data == null){ return i; } } else{ if(o.equals(curr.data)){ lastindex = i+1; } } curr = curr.next; } if(lastindex == 0) return -1; return lastindex; } @Override public ListIterator listIterator() { // TODO Auto-generated method stub return null; } @Override public ListIterator listIterator(int index) { // TODO Auto-generated method stub return null; } @Override public List subList(int fromIndex, int toIndex) { // TODO Auto-generated method stub ListNode sublist = new ListNode(); return null; } @Override //this method is equivalent to addLast() public boolean add(Object e) { ListNode walk = head; for(;walk.next != null; walk = walk.next){ //empty loop body } //make new node ListNode anode = new ListNode(e); walk.next = anode; this.size ++; //increment size return true; } public class MyLinkedListIterator implements Iterator { private ListNode cur; private MyLinkedListIterator ( ListNode start ) { this.cur = start.next; //with dummy node } public boolean hasNext() { return cur != null; } public Object next() throws NoSuchElementException { if(hasNext()) { Object data = cur.data; cur = cur.next; return data; } throw new NoSuchElementException(); } public void remove() { throw new UnsupportedOperationException(); } }//end of Iterator @Override public String toString() { String result = “( “; for (ListNode node = this.head.next; node != null; node = node.next) { result += node.data + “–>”; } return result + “)”; }}

x

Hi!
I'm William!

Would you like to get a custom essay? How about receiving a customized one?

Check it out