数据结构之静态队列的java简单实现

1.构造列队节点

public class QueueNode<T>{



private T t;
private QueueNode<T> queueNode;//由于静态队列是基于数组的连续存储实现的,指针域部分可以去掉,保留数据域即可
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public QueueNode<T> getQueueNode() {
return queueNode;
}
public void setQueueNode(QueueNode<T> queueNode) {
this.queueNode = queueNode;
}

}

2.构建队列

public class StaticQueue<T> {

private int queueCapacity = 10;//默认队列为10个存储空间
private QueueNode<T>[] nodeArray;
private int queueSize;//队列的有效元素个数
private int fontIndex;//队首位置
private int endIndex;//队尾位置


/**
* 初始化栈
*/
@SuppressWarnings("unchecked")
public void init(){
this.nodeArray = new QueueNode[this.queueCapacity];
this.queueSize = 0;
this.fontIndex = 0;
this.endIndex = 0;
}

/**
* 入队
* @param node
* @return
*/
public boolean add(QueueNode<T> node) {
//如果队列处于刚初始化状态
if(this.fontIndex == this.endIndex) {
this.queueSize++;
this.nodeArray[this.fontIndex] = node;
this.endIndex++;
}else {
//判断当前列队尾节点是否是数组的最后一个节点
if((this.endIndex + 1) == this.queueCapacity) {
//如果当前列队尾节点处于数组的最后一个节点,列队首节点又处于数组的第一个节点位置没有出对 则表示列队满员
if(this.fontIndex == 0) {
System.out.println("十分抱歉,队伍已经满员了无法插入");
return false;
//如果没有满员则继续添加
}else{
this.nodeArray[endIndex] = node;
this.endIndex = 0;
this.queueSize++;
}
}else {//如果队列尾节点不处于数组最后一个节点

//如果队列尾节点的下一个节点是首节点则表示满员
if(this.endIndex+1 == this.fontIndex){
System.out.println("十分抱歉,队伍已经满员了无法插入");
return false;
}else {
this.nodeArray[this.endIndex] = node;
this.endIndex ++;
this.queueSize++;
}
}
}

return true;
}


/**
* 出队
* @return
* 这部分可以改成直接返回数据域 T
*/
public QueueNode<T> peak(){
if(this.endIndex ==this.fontIndex) {
System.out.println("队列无队员不能出队");
return null;
}else {
QueueNode<T> re = this.nodeArray[this.fontIndex];
this.fontIndex++;
if(this.fontIndex == this.queueCapacity) {
this.fontIndex=0;
}
this.queueSize--;
return re;
}
}

/**
* 返回当前队里的有效元素个数
* @return
*/
public int size() {
return this.queueSize;
}

/**

* @param capacity 初始化队里的存储空间大小
*/
public StaticQueue(int capacity) {
this.queueCapacity = capacity;
init();
}


public StaticQueue() {
init();
}
}

3.测试队列的出入队

public class TestStaticQueue {
public static void main(String[] args) {

Person person1 = new  Person("刘德华",18,'男');
Person person2 = new  Person("郑秀文",19,'女');

QueueNode<Person> node1 = new QueueNode<>();
node1.setT(person1);

QueueNode<Person> node2 = new QueueNode<>();
node2.setT(person2);

StaticQueue<Person> queue = new StaticQueue<>();
for(int i=0;i<20;i++) {
node1.getT().setAge(i);
queue.add(node1);
System.out.println("size --> "+queue.size()+"    i = "+i);

if(i%2 == 0) {
System.out.println(queue.peak().getT().toString()+"--->" +queue.size());
}
}

}
}

/**输出结果*/

size --> 1    i = 0
Person [name=刘德华, age=0, sex=男]--->0
size --> 1    i = 1
size --> 2    i = 2
Person [name=刘德华, age=2, sex=男]--->1
size --> 2    i = 3
size --> 3    i = 4
Person [name=刘德华, age=4, sex=男]--->2
size --> 3    i = 5
size --> 4    i = 6
Person [name=刘德华, age=6, sex=男]--->3
size --> 4    i = 7
size --> 5    i = 8
Person [name=刘德华, age=8, sex=男]--->4
size --> 5    i = 9
size --> 6    i = 10
Person [name=刘德华, age=10, sex=男]--->5
size --> 6    i = 11
size --> 7    i = 12
Person [name=刘德华, age=12, sex=男]--->6
size --> 7    i = 13
size --> 8    i = 14
Person [name=刘德华, age=14, sex=男]--->7
size --> 8    i = 15
size --> 9    i = 16
Person [name=刘德华, age=16, sex=男]--->8
size --> 9    i = 17
十分抱歉,队伍已经满员了无法插入
size --> 9    i = 18
Person [name=刘德华, age=18, sex=男]--->8
size --> 9    i = 19

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值