
public class LinkedList implements List, Cloneable {
	//constructor
	public LinkedList(){

		head = null;
		tail = null;
		count = 0;
	}

	//accessors
	synchronized public int getCount() {
		return count;
	}

	synchronized public boolean isEmpty() {
		if (count == 0) return true;
		    else return false;
    }

	synchronized public Object elementAt(int pos) {
		if (pos <= count){
			Node result = this.head;
			for (int i=1;i<pos;i++){
				result = result.next;
		    }
			return result.element;
	    }else{
		   return null;}

	}
	synchronized public Object elementAtHead(){
		return this.head.element;
	}
	synchronized public Object elementAtTail(){
		return this.tail.element;
	}

	synchronized public Node nodeAt(int pos) {
		if (pos <= count){
			Node result = this.head;
			for (int i=1;i<pos;i++){
				result = result.next;
		    }
		    return result;
	    }else{
		    return null;}
    }

	//canonical form methods
	synchronized public boolean equals(Object other) {
		if (other != null && other instanceof List){
			List otherList = (List) other;
			if (this.getCount() == otherList.getCount()){
				for (int i = 0; i<this.getCount();i++){
					Object thisElement = this.elementAt(i);
					Object otherElement = otherList.elementAt(i);
					if (thisElement == null){
						if (otherElement != null) {
							 return false;
					    }
				    }else {
						if (!thisElement.equals(otherElement)){
							  return false;
					    }
				    }
			      }
				  return true;
			 }
	    }
		return false;
	}

	synchronized public int hashCode(){
        int sum = 0;
        int i = 0;
        Node node = head;
        while (i < 4 && node!=null) {
			if (node.element!=null){
				sum <<=8;
				sum |= node.element.hashCode() & 0xFF;
		    }
		    node = node.next;
	    }
	    return sum;
	}
	synchronized public Object clone(){
		return null;
	}
	synchronized public String toString() {
		StringBuffer s = new StringBuffer();
		int i = 0;
		for (Node node = head; node != null; node = node.next, i ++) {
			s.append("[" + i + "] = " + node.element + "\n");
	    }
		return s.toString();
    }

	//modifiers
	synchronized public void insertElementAt(Object element, int pos) {
		Node newNode = new Node(element);
        Node temp = nodeAt(pos);
		temp.prev.next = newNode;
		newNode.prev = temp.prev;
		newNode.next = temp;
		temp.prev = newNode;
		count++;
	}

	synchronized public void insertAtHead(Object element) {
		Node newNode = new Node(element);
		newNode.prev = null;
		newNode.next = this.head;
		if (count>0){
		   this.head.prev = newNode;
	    }else{
		   this.tail = newNode;
	    }
		this.head = newNode;
		count++;
	}

	synchronized public void insertAtTail(Object element) {
		Node newNode = new Node(element);
		newNode.next = null;
		newNode.prev = this.tail;
		if (count>0){
		   this.tail.next = newNode;
	    }else{
			this.head = newNode;
	    }
		this.tail = newNode;
		count++;
	}

	synchronized public Object removeHead () {
		Node temp;
		temp = this.head;
		if (count==1){
			this.head=null;
			this.tail=null;
	    }
		else{
		  this.head = temp.next;
		  temp.next.prev = null;
    	}
		count--;
		return temp;
	}
	synchronized public Object removeTail () {
		Node temp;
		temp = this.tail;
		if (count==1){
			this.head=null;
			this.tail=null;
	    }
		else{
		    this.tail = temp.prev;
		    this.tail.next=null;
	    }
		count--;
		return temp;
	}
	synchronized public Object removeElementAt(int pos) {
		Node temp;
		if (pos <=count){
		   temp = nodeAt(pos);
		   temp.prev.next = temp.next;
		   temp.next.prev = temp.prev;
		   count--;
		   return temp;
	    }
		else{
		   return null;
	    }
	}

	//fields
	protected Node head, tail;
	protected int count;

	//auxiliary inner classes
	static protected class Node {
		public Node(Object element){
			this.element = element;
	    }
		Object element;
		Node next, prev;
    }

    public static void main(String args[]) {
		//LinkedList l = new LinkedList();
		//l.insertAtHead(new Integer(1));
		System.out.println("test begin");
		LinkedList l = new LinkedList();
		l.insertAtHead(new Integer(1));
		l.insertAtHead(new Integer(2));
		l.insertAtTail(new Integer(3));
		l.insertAtTail(new Integer(4));

		l.insertElementAt(new Integer(5),3);
		l.insertElementAt(new Integer(6),3);
		l.insertElementAt(new Integer(7),3);

		System.out.println("Fist pass");
		System.out.println(l);

		l.removeHead();
		l.removeTail();
		l.removeElementAt(2);

		System.out.println("Second pass");
		System.out.println(l);
    }
}