数据结构<十一>: 线程结构 队列的两种实现

一个队列的抽象数据类型


package com.mo.queue;

/**
 * 队列的抽象数据结构
 *
 */
public interface Queue {
	/**
	 *	入队 
	 */
	void append(Object obj) throws Exception;
	/**
	 *	出队 
	 */
	Object delete() throws Exception;
	/**
	 * 取队头
	 */
	Object getFront() throws Exception;
	/**
	 * 是否为空
	 */
	boolean isEmpty();
}




数组实现的队列


package com.mo.queue;

/**
 * 	顺序循环队列(数组实现)
 */
public class SeqQueue implements Queue {

	private final static int DEFAULTSIZE = 10;//默认创建的数组大小
	private int front;//队头的下标 
	private int rear;//队尾的下标
	private int count;//队中的元素个数
	private int maxSize;//队中能保存的最大元素个数
	Object[] data;//保持队列元素的数组
	
	public SeqQueue() {
		init(DEFAULTSIZE);
	}
	public SeqQueue(int size) {
		init(size);
	}
	
	private void init(int size) {
		maxSize = size;
		data = new Object[size];
		count = 0;
		front = 0;
		rear = 0;
	}
	
	// 入队 --> 加一个元素在队尾,rear要加一,count要加一
	public void append(Object obj) throws Exception {
		if(count == maxSize) throw new Exception();
		data[rear] = obj;
		rear = (rear + 1) % maxSize;
		count++;
	}

	// 出队 --> 从队头删除一个元素,front要减一,count减一
	public Object delete() throws Exception {
		if(count == 0) throw new Exception();
		Object obj = data[front];
		front = (front + 1) % maxSize;
		count--;
		return obj;
	}

	public Object getFront() throws Exception {
		if(count == 0) throw new Exception();
		return data[front];
	}

	public boolean isEmpty() {
		return count == 0;
	}
	
	public static void main(String[] args) throws Exception {
		SeqQueue seqQueue = new SeqQueue();
		for(int i = 0;i < 10; i++) {
			//入队
			seqQueue.append(i + 1);
		}
		while(!seqQueue.isEmpty()) {
			//出队
			System.out.print(seqQueue.delete());
		}
	}

}



链表实现的队列


package com.mo.queue;

import com.mo.linList.Node;

/**
 * 队列,列表实现 
 */
public class LinQueue implements Queue {

	private Node front;//队头
	private Node rear;//队尾
	private int count;//计算器
	
	public LinQueue() {
		front = rear = null;
		count = 0;
	}
	
	public void append(Object obj) throws Exception {
		Node newNode = new Node(obj,null);
		if(rear != null) {
			//在本来的队尾连接新的队尾
			rear.setNext(newNode);
		}
		//队中还没有元素
		if(front == null) {
			front = newNode;
		}
		//设置当前队尾
		rear = newNode;
		count++;
	}
	public Object delete() throws Exception {
		if(count == 0) throw new Exception();
		Object obj = front.getData();
		front = (Node)front.getNext();
		count--;
		return obj;
	}
	
	public Object getFront() throws Exception {
		return front.getData();
	}
	public boolean isEmpty() {
		return count == 0;
	}
	
	public static void main(String[] args) throws Exception {
		LinQueue linQueue = new LinQueue();
		for(int i = 0; i < 10; i++) {
			//入队
			linQueue.append(i + 1);
		}
		System.out.println("从队中取元素" + linQueue.getFront());
		System.out.println("从队中取元素" + linQueue.getFront());
		while(!linQueue.isEmpty()) {
			//出队
			System.out.print(linQueue.delete());
		}
	}
	
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值