配列とノード方式に基づくキューの実装


配列方式に基づくキューの実装
package com.study.link;
/**
 *  
 * @author Administrator
 *
 */
public class ArrayQueue {
	private Node[] nodeArray;
	private int last;
	public ArrayQueue(int length){
		nodeArray = new Node[length];
	}
	/**
	 *  
	 * @param n
	 */
	public void enqueue(Node node){
		nodeArray[last] = node;
		last++;
	}
	/**
	 *  
	 * @return
	 */
	public Node denqueue(){
		if(last>0){
			last--;
			Node node = nodeArray[last];
			return node;
		}
		return null;
	}
	public static void main(String[] args) {
		ArrayQueue queue = new ArrayQueue(10);
		queue.enqueue(new Node(1));
		queue.enqueue(new Node(2));
		queue.enqueue(new Node(3));
		queue.enqueue(new Node(4));
		queue.enqueue(new Node(5));
		while(queue.last>0){
			System.out.println(queue.denqueue().getValue());
		}
	}
	
}

ノードベース
package com.study.link;
/**
 *  
 *  
 * @author Administrator
 *
 */
public class NodeQueue {
	private Node first;
	private Node last;
	/**
	 *  
	 * @param n
	 */
	public void enqueue(Node node){
		if(first==null){
			first = node;
			last = node;
		}else{
			last.setNext(node);
			last = node;
		}
	}
	/**
	 *  
	 * @return
	 */
	public Node denqueue(){
		if(first == null){
            return null;
        }else{
            Node temp = new Node(first.getValue());
            first = first.getNext();
            if(last == temp){
            	last = first;
            }
            return temp;
        }   
	}
	
	public static void main(String[] args) {
		NodeQueue queue = new NodeQueue();
		queue.enqueue(new Node(1));
		queue.enqueue(new Node(2));
		queue.enqueue(new Node(3));
		queue.enqueue(new Node(4));
		queue.enqueue(new Node(5));
		queue.enqueue(new Node(6));
		queue.enqueue(new Node(7));
		queue.enqueue(new Node(8));
		queue.enqueue(new Node(9));
		while(queue.first!=null){  
            System.out.println(queue.denqueue().getValue());  
        }  
	}
}
package com.study.link;
/**
 *  
 *  
 * @author Administrator
 *
 */
public class Node {
	private Object value;
	private Node next;
	public Node(Object value){
		this.value = value;
		next = null;
	}
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
}