节点类
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());
}
}
}