java实现简单队列与循环队列

节点类

package com.Test;
/**
 * 节点类
 * @author
 *
 */
public class Node {

	Node next;//指针
	Object  obj;//元素
	public Node(Node next, Object obj) {
		super();
		this.next = next;
		this.obj = obj;
	}
	public Node() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	public Object getObj() {
		return obj;
	}
	public void setObj(Object obj) {
		this.obj = obj;
	}
	
	
	
	
}


1.简单队列 数组实现

/**
 * 顺序队列(会出现假溢出 头指针和尾指针都指向队尾) 线性实现
 * @author 
 *
 */
public class ArrayQueue {

	
	private String[]  data;
	private int maxsize;//队列长度
	private int font;//指向队列头指针
	private int rear;//指向队列尾指针
	public ArrayQueue(int maxsize) {
		
		this.maxsize = maxsize;
		//初始化队列
		data=new String[this.maxsize];
		//头指针尾指针赋值 赋值-1 指向队列头前	
		this.font=-1;
		this.rear=-1;
	}
	public ArrayQueue() {
		
	}
	//判断队列是否为空
	public boolean isNull() {

		return this.font==this.rear;
	}
	//判读队列是否已满
	public boolean isFull() {
		
		return this.rear==this.maxsize-1;
	}
	
	//入队
	public boolean push(String name) throws Exception {
		if (isFull()) {
			throw new Exception("队列满");
		}
		data[++rear]=name;
		return true;
		
	}
	//出队
	public String pop() throws Exception {
		if (isNull()) {
			throw new Exception("队列为空");
		}
		String name=data[++font];
		data[font]=null;
		return name;
	}
	
	
	
	
	
	
}
//简单队列实现
/**
 * 顺序队列(会出现假溢出 头指针和尾指针都指向队尾) 线性实现
 * @author 
 *
 */
public class ArrayQueue {

	
	private String[]  data;
	private int maxsize;//队列长度
	private int font;//指向队列头指针
	private int rear;//指向队列尾指针
	public ArrayQueue(int maxsize) {
		
		this.maxsize = maxsize;
		//初始化队列
		data=new String[this.maxsize];
		//头指针尾指针赋值 赋值-1 指向队列头前	
		this.font=-1;
		this.rear=-1;
	}
	public ArrayQueue() {
		
	}
	//判断队列是否为空
	public boolean isNull() {

		return this.font==this.rear;
	}
	//判读队列是否已满
	public boolean isFull() {
		
		return this.rear==this.maxsize-1;
	}
	
	//入队
	public boolean push(String name) throws Exception {
		if (isFull()) {
			throw new Exception("队列满");
		}
		data[++rear]=name;
		return true;
		
	}
	//出队
	public String pop() throws Exception {
		if (isNull()) {
			throw new Exception("队列为空");
		}
		String name=data[++font];
		data[font]=null;
		return name;
	}
	
	
}
//测试
public static void main(String[] args) throws Exception {

		ArrayQueue arrayQueue = new ArrayQueue(6);
		ArrayQueue1 arrayQueue1 = new ArrayQueue1(6);
//		for (int i = 0; i < 6; i++) {
//			
//			
//			if (i==6) {
//				String pop = arrayQueue.pop();
//				System.out.println(pop);
//			}
//			arrayQueue.push("zs"+i);
//			
//		}
//		
//		for (int i = 0; i < 6; i++) {
//			System.out.println(arrayQueue.pop());
//		}
		
	}
}

2.简单队列 链式实现

public class LinkQueue {

	Node front;//头指针
	Node rear;//尾指针
	Integer size;
	public LinkQueue() {
		this.front=this.rear=null;
		this.size=0;
	}
	
	public boolean isEmpty() {
		
		return this.size==0?true:false;
	}
	
	//入队
	public boolean addQueue(String str) {
		if (isEmpty()) {
			front=new Node(null,str);
			rear=front;
			size++;
			return true;
		}
		Node node = new Node(null,str);
		rear.setNext(node);
		size++;
//		rear=node
		rear=rear.getNext();
		return true;
	} 
	
	
	//出队
	public String popQueue() {
		if (isEmpty()) {
			
			return "为空";
		}
		String str = (String) front.getObj();
		front=front.getNext();
		size--;
		return str;
	}
	
	

}
//测试
public class LinkedTest {
	public static void main(String[] args) {
		
		LinkQueue linkQueue = new LinkQueue();
		linkQueue.addQueue("1");
		linkQueue.addQueue("2");
		System.out.println(linkQueue.popQueue());
		System.out.println(linkQueue.popQueue());
		System.out.println(linkQueue.popQueue());
		
	}
	
}

3.循环队列

public class ArrayQueue1 {
	private String[] data;
	private int maxsize;// 队列长度
	private int font;// 指向队列头指针
	private int rear;// 指向队列尾指针

	public ArrayQueue1(int maxsize) {

		this.maxsize = maxsize;
		// 初始化队列
		data = new String[this.maxsize];
		// 头指针尾指针赋值 赋值-1 指向队列头前
		this.font = 0;
		this.rear = 0;
	}

	public ArrayQueue1() {

	}

	// 判断队列是否为空
	public boolean isNull() {

		return this.font == this.rear;
	}

	// 判读队列是否已满
	public boolean isFull() {

		return (this.rear + 1) % maxsize == this.font;
	}

	// 入队
	public boolean push(String name) throws Exception {
		if (isFull()) {
			throw new Exception("队列满");
		}
		data[rear] = name;
		rear=(rear+1)%maxsize;
		return true;

	}

	// 出队
	public String pop() throws Exception {
		if (isNull()) {
			throw new Exception("队列为空");
		}
		
		String name = data[this.font];
		data[font] = null;
		this.font=(this.font+1)%maxsize;
		return name;
	}

}
//测试
public class Test {

	public static void main(String[] args) throws Exception {

		ArrayQueue1 arrayQueue1 = new ArrayQueue1(6);

		for (int i = 0; i < 5; i++) {

			arrayQueue1.push("zs" + i);

		}

		for (int i = 0; i < 3; i++) {
			System.out.println(arrayQueue1.pop());
		}
		for (int i = 0; i < 3; i++) {

			arrayQueue1.push("zsdad" + i);

		}
		for (int i = 0; i < 3; i++) {
			System.out.println(arrayQueue1.pop());
		}
	}
}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值