java语言实现队列

队列的接口

package edu.lqstudy.chaptor.chaptor22.queue;

public interface Queue {
	/**插入一个元素到队列尾部*/
    public void enQueue(Object obj) throws Exception;
    /**删除队列头部的元素,并且返回其值*/
    public Object deQueue() throws Exception;
    /**获取队列头部的值,但是不删除*/
    public Object queueFront() throws Exception;
    /**判断队列是否为空*/
    public boolean isEmpty();
}

队列的实现方式

这里有两种实现方式,一种是顺序存储结构,另一种是链式存储结构
添加元素移动tail的值,删除元素移动head的值

顺序存储结构实现队列

package edu.lqstudy.chaptor.chaptor22.queue;
/**
 * 循环队列,head指向的位置没有值,这是为了区分队列满和空的必要妥协
 * 
 * @author hacker
 *
 */
public class SeqQueue implements Queue{

	private Object[] queue;//队列的数据区
	private int head = 0;//(head + 1)%size为队列的头部
	private int tail = 0;//队列的尾部
	private int size = 0;//queue数组的容量,队列的容量为size - 1
	
	/**
	 * 无参构造函数,队列的容量默认为10
	 * 
	 */
	SeqQueue(){
		size = 10 + 1;
		queue = new Object[size];
	}
	
	/**
	 * 构造函数,根据给定的n生成容量为n的队列,
	 * 
	 * @param int n
	 */
	SeqQueue(int n){
		queue = new Object[n + 1];
		size = n + 1;
	}
	@Override
	public void enQueue(Object obj) throws Exception {
		// TODO Auto-generated method stub
		if(isFull()){
			throw new Exception("队列已满");
		}
		else{
		    tail = (tail + 1)%size;
			this.queue[tail] = obj;
		}
	}

	@Override
	public Object deQueue() throws Exception {
		// TODO Auto-generated method stub
		Object obj = null;
		if(isEmpty()){
			throw new Exception("队列已空");
		}
		else{
			head = (head + 1)%size;
			obj = queue[head];
		}
		return obj;
	}

	@Override
	public Object queueFront() throws Exception {
		// TODO Auto-generated method stub
		
		Object obj = null;
		if(isEmpty()){
			throw new Exception("队列已空");
		}
		else{
			int index = (head + 1)%size;
			obj = queue[index];
		}
		return obj;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return tail == head;
	}
	
	/**
	 * 判断队列是否已满
	 * @return boolean
	 */
	public boolean isFull(){
		return (tail + 1)%size == head;//如果tail在向前走一步等于head,
		//说明队列已经饶了一圈,则已经满了
	}
    
	/**
	 * 返回队列中元素的个数
	 * @return int
	 */
	public int getLength(){
		return (size + tail - head)%size;//如果tail > head,值为tail - head
		//如果tail < head 值为size - (head - tail),都是元素的个数
	}
	
	/**
	 * 返回描述对象信息的String对象
	 * 格式为:
	 * 初始大小:(int)
	 * head值: (int)
	 * tail值: (int)
	 * size值:  (int)
	 * 
	 */
	public String toString(){
		String s = "初始大小" + getLength() + "\nhead值:" + 
	         head + "\ntail值: " + tail + "\nsize值: " + size;
		return s;//
	}
}


链式结果实现队列

package edu.lqstudy.chaptor.chaptor22.queue;
/**
 * 使用链式存储结构实现队列,实现了两个类:
 * Node类:结点类,相当于集合的元素
 * LinkQueue类:相当于存储元素的集合
 * @author hacker
 *
 */
public class LinkQueue implements Queue{
    private Node head;//指向队列头元素
    private Node tail;//指向队列尾元素
    private int len;//添加,删除数据时改变len的值
    
    /**构造函数*/
    LinkQueue(){
    	head = tail = null;
    	len = 0;
    }
	@Override
	public void enQueue(Object obj) throws Exception {
		// TODO Auto-generated method stub
	    Node n = new Node(obj);
		if(isEmpty()){
			head = tail = n;//如果添加元素到空队列,说明head值会改变
			//并且tail之前的值为null;
		}
		else{
			tail.setNext(n);//原结点指向新生成的结点
			tail = n;
		}
		len++;
			
	}

	@Override
	public Object deQueue() throws Exception {
		// TODO Auto-generated method stub
		Object ans = null;
		if(isEmpty())
			throw new Exception("从空队列中移除对象是不予许的");
		else{
			Node n = head;//获取头结点
			head = head.getNext();//将head指针指向下一个结点
			len--;
			if(isEmpty())//如果删除的是最后一个元素,说明tail的值也发生了变化
				tail = null;
			ans = n.getObj();
		}
		return ans;
	}

	@Override
	public Object queueFront() throws Exception {
		// TODO Auto-generated method stub
		Object obj = null;
		if(isEmpty())
			throw new Exception("从空队列中查找元素是不予许的");
		else{
			Node n = head;
			obj = n.getObj();
		}
		return obj;
		
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return len == 0;
	}
	
	public int length(){
		return len;
	}

}

class Node{
	private Object obj;//存储的对象
	private Node next = null;//下一个对象的指针
	
	public Node(){
		obj = null;
	}
	
	public Node(Object obj){
		this.obj = obj;
	}
	
	public Node(Object obj,Node next){
		this(obj);
		this.next = next;
	}
	
	public Object getObj(){
		return obj;
	}
	
	public Node getNext(){
		return next;
	}
	
	public void setNext(Node next){
		this.next = next;
	}
}




要使用Java语言实现队列,你可以利用Java的内置数据结构LinkedList来实现。以下是一个简单的示例代码: ```java import java.util.LinkedList; public class Queue { private LinkedList<Object> queue; public Queue() { queue = new LinkedList<>(); } public void enqueue(Object item) { queue.addLast(item); } public Object dequeue() { if (isEmpty()) { throw new RuntimeException("队列为空"); } return queue.removeFirst(); } public Object peek() { if (isEmpty()) { throw new RuntimeException("队列为空"); } return queue.getFirst(); } public boolean isEmpty() { return queue.isEmpty(); } public int size() { return queue.size(); } public static void main(String[] args) { Queue queue = new Queue(); queue.enqueue("A"); queue.enqueue("B"); queue.enqueue("C"); System.out.println("队列大小:" + queue.size()); System.out.println("队首元素:" + queue.peek()); while (!queue.isEmpty()) { System.out.println("出队元素:" + queue.dequeue()); } } } ``` 这个示例中,我们使用LinkedList来实现队列的基本操作。enqueue方法用于向队列尾部添加元素,dequeue方法用于从队列头部移除元素。peek方法返回队列头部的元素,但不会移除它。isEmpty方法用于判断队列是否为空,size方法返回队列的大小。 在main方法中,我们创建了一个队列对象,并添加了3个元素。然后我们输出队列的大小和队首元素,并依次从队列中移除元素并输出。运行这段代码,你会看到以下输出结果: ``` 队列大小:3 队首元素:A 出队元素:A 出队元素:B 出队元素:C ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值