
package assign;

public class Queue {
	private Node head, tail;
	public Queue(){
		head=null; tail=null;
	}
	public boolean put(Object key, Object value){
		if (readNode(head,key)!=null) return false;
		Node aNode=new Node(key,value);
		if(head == null) head = aNode;
		else tail.next=aNode;
		tail=aNode;
		return true;
	}
	public Node[] toArray(){
		int arraylength=count();
		Node[] resultArray=new Node[arraylength];
		Node act=head;
		for (int i=0;i<arraylength;i++){
			resultArray[i]=act;
			act=act.next;
		}
		return resultArray;
	}
	public void delNode(Object key){
		Node act=readNode(head,key);
		if (act==null)return;
		else if (act==head)head=act.next;
		else readprev(head,key).next=act.next;
	}
	public Object readvalue(Object key){
		Node aNode=readNode(head,key);
		if (aNode==null) return null;
		else return aNode.value;
	}
	private Node readNode(Node act, Object key){
		if (act==null) return null;
		if (act.key.equals(key)) return act;
		return readNode(act.next, key);
	}
	private Node readprev(Node act, Object key){
		if (act==null) return null;
		else if (act.next.key.equals(key))return act;
		else return readprev(act.next, key);
	}
	public int count(){
		if (head==null)return 0;
		return count(head,1);
	}
	private int count(Node act, int i){
		if (act.next==null) return i;
		else return count(act.next,++i);
	}
}
