使用数组实现固定长度的队列结构
2018-04-03 11:15
399 查看
使用数组实现固定长度的队列结构,很多人coding时,都会使用两个变量start和end作为指针,来表示队列的头和尾,然后判断end大于start,start一直追赶end的这种方式实现,这其中有些边界条件判断来判断去,end到达尾部然后回到头部,end又小于start等等情况。coding起来比较复杂。
我的实现呢,再添加一个变量size,用来表示当前队列还有几个元素,只要size大于数组长度就不能再入队,size小于0就不能再出队列了。
入队列规则:移动end变量,每次让end变量指向队列尾部的下一个位置,每次入队列时只用把元素放入到end的位置。如果到达数组尾部,但是队列并没有满(size不大于数组长度),那么让end等于0,继续循环。当然,size要加加
出队列规则:移动start变量,每次start变量就指向队列的头部,出队列时,就取出start变量位置的元素,start加加,当start到达数组尾部,但是队列并没有空(size不小于0),则让start回到数组首部,即start等于0,继续循环。当然,size要减减
代码:public class UsingArrayRealizeQueue {
int[] queue = new int[5];
int size = 0;// 表示队列中还有几个元素
int start =0;// 指向队列头
int end = 0;// 指向队列尾
// 入队列
public void put(int value){
if(size >= queue.length){
throw new ArrayIndexOutOfBoundsException("the size more than bounds");
}
queue[end++] = value;
size++;
if(end == queue.length){// 到达数组尾部,但是数组并没有满,再回到数组头部继续循环
end = 0;
}
}
// 出队列
public int poll(){
if(size <= 0){
throw new IllegalArgumentException("the size less than 0");
}
if(start == queue.length){// 到达数组尾部,但是数组并没有空,再回到数组头部继续循环
start = 0;
}
size--;
return queue[start++];
}
public static void main(String[] args) {
UsingArrayRealizeQueue queue = new UsingArrayRealizeQueue();
queue.put(1);
queue.put(2);
queue.put(3);
queue.put(4);
queue.put(5);
System.out.println(queue.poll());// 1
System.out.println(queue.poll());// 2
queue.put(6);
queue.put(7);
System.out.println(queue.poll());// 3
System.out.println(queue.poll());// 4
System.out.println(queue.poll());// 5
System.out.println(queue.poll());// 6
queue.put(8);
System.out.println(queue.poll());// 7
System.out.println(queue.poll());// 8
// System.out.println(queue.poll());
}
}测试结果:
我的实现呢,再添加一个变量size,用来表示当前队列还有几个元素,只要size大于数组长度就不能再入队,size小于0就不能再出队列了。
入队列规则:移动end变量,每次让end变量指向队列尾部的下一个位置,每次入队列时只用把元素放入到end的位置。如果到达数组尾部,但是队列并没有满(size不大于数组长度),那么让end等于0,继续循环。当然,size要加加
出队列规则:移动start变量,每次start变量就指向队列的头部,出队列时,就取出start变量位置的元素,start加加,当start到达数组尾部,但是队列并没有空(size不小于0),则让start回到数组首部,即start等于0,继续循环。当然,size要减减
代码:public class UsingArrayRealizeQueue {
int[] queue = new int[5];
int size = 0;// 表示队列中还有几个元素
int start =0;// 指向队列头
int end = 0;// 指向队列尾
// 入队列
public void put(int value){
if(size >= queue.length){
throw new ArrayIndexOutOfBoundsException("the size more than bounds");
}
queue[end++] = value;
size++;
if(end == queue.length){// 到达数组尾部,但是数组并没有满,再回到数组头部继续循环
end = 0;
}
}
// 出队列
public int poll(){
if(size <= 0){
throw new IllegalArgumentException("the size less than 0");
}
if(start == queue.length){// 到达数组尾部,但是数组并没有空,再回到数组头部继续循环
start = 0;
}
size--;
return queue[start++];
}
public static void main(String[] args) {
UsingArrayRealizeQueue queue = new UsingArrayRealizeQueue();
queue.put(1);
queue.put(2);
queue.put(3);
queue.put(4);
queue.put(5);
System.out.println(queue.poll());// 1
System.out.println(queue.poll());// 2
queue.put(6);
queue.put(7);
System.out.println(queue.poll());// 3
System.out.println(queue.poll());// 4
System.out.println(queue.poll());// 5
System.out.println(queue.poll());// 6
queue.put(8);
System.out.println(queue.poll());// 7
System.out.println(queue.poll());// 8
// System.out.println(queue.poll());
}
}测试结果:
相关文章推荐
- 用数组结构实现大小固定的栈和队列
- 使用数组实现固定大小的栈结构
- 数据结构与算法-----队列-使用数组(顺序结构)实现
- 使用数组实现的简单队列结构
- 使用JavaScript的数组实现数据结构中的队列与堆栈
- C 数据结构使用数组和链表实现栈
- 经典数据结构之数组实现的队列
- 自定义队列(优化了数组固定长度的弊端)以及集合中的一些重点内容
- 数据结构(Data structure):使用双向链表实现队列(C语言)
- 使用数组和范型来实现栈结构?
- 数据结构循环队列,数组实现,循环队列中如果不使用数据项计数字段items,而是通过front和rear计算队列是否为空或满或队列个数,那会很复杂
- 优先级队列:关键字小的优先处理。数组结构实现的优先级队列
- Python 使用循环数组实现队列 (基于class, 包含迭代器)
- 使用数组实现队列
- 使用JavaScript的数组实现数据结构中的队列与堆栈
- Java简单实现固定长度队列(FIFO)
- 数据结构:用数组实现的定容队列
- 使用数组实现双端队列
- java使用数组和链表实现队列示例
- 使用数组实现一个随机队列