java实现队列
实现:
package 队列;
import javax.management.RuntimeErrorException;
public class MyQueue<E> {
private Object[] data = null;
private int maxSize;//队列容量
private int front;//对列头
private int rear;//队列尾
public MyQueue() {
this(10);
}
public MyQueue(int init) {
if(init<0) {
throw new RuntimeException("初始化大小不能小于0:"+init);
}else {
maxSize = init;
data = new Object[maxSize];
front = rear = 0;
}
}
/**
* 判断是否队列为空
* @return
*/
public boolean empty(){
return rear==front;
}
/**
* 入队
* @param e
* @return
*/
public boolean add(E e){
if(rear== maxSize-1){
throw new RuntimeException("队列已满,无法插入新的元素!");
}else{
data[rear++]=e;
return true;
}
}
/**
* 查看队列首位
* @return
*/
public E peek(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
return (E) data[front];
}
}
/**
* 出队
* @return
*/
public E poll(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
E temp = (E) data[front]; //保留队列的front端的元素的值
data[front++] = null; //释放队列的front端的元素
return temp;
}
}
/**
* 队列长度
* @return
*/
public int length(){
return rear-front;
}
public static void main(String[] args) {
MyQueue<Integer> q = new MyQueue<>();
q.add(5);q.add(6);q.add(7);
System.out.println(q.length());
q.poll();
}
}
顺序表实现循环队列:
package 队列;
import java.util.Arrays;
public class MyLoopQueue<E> {
public Object[] data = null;
private int maxSize; // 队列容量
private int rear;// 队列尾,允许插入
private int front;// 队列头,允许删除
private int size=0; //队列当前长度
public MyLoopQueue() {
this(10);
}
public MyLoopQueue(int init) {
if (init >= 0) {
this.maxSize = init;
data = new Object[init];
front = rear = 0;
} else {
throw new RuntimeException("初始化大小不能小于0:" + init);
}
}
/**
* 判断是否队列为空
* @return
*/
public boolean empty(){
return size==0;
}
/**
* 入队
* @param e
* @return
*/
public boolean add(E e){
if(size== maxSize){
throw new RuntimeException("队列已满,无法插入新的元素!");
}else{
data[rear]=e;
rear = (rear + 1) % maxSize;
size++;
return true;
}
}
/**
* 查看队列首位
* @return
*/
public E peek(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
return (E) data[front];
}
}
/**
* 出队
* @return
*/
public E poll(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
E temp = (E) data[front]; //保留队列的front端的元素的值
data[front] = null; //释放队列的front端的元素
front = (front+1)%maxSize;
size--;
return temp;
}
}
/**
* 队列长度
* @return
*/
public int length(){
return size;
}
/**
* 清空循环队列
*/
public void clear(){
Arrays.fill(data, null);
size = 0;
front = 0;
rear = 0;
}
public static void main(String[] args) {
MyLoopQueue<Integer> q = new MyLoopQueue<>();
q.add(5);q.add(6);q.add(7);
System.out.println(q.length());
q.poll();
System.out.println(q.length());
q.clear();
System.out.println(q.length());
}
}
链式队列实现
package 队列;
public class LinkQueue<E> {
// 链栈的节点
private class Node<E> {
E e;
Node<E> next;
public Node() {
}
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
}
private Node front;// 队列头,允许删除
private Node rear;// 队列尾,允许插入
private int size; //队列当前长度
public LinkQueue() {
front = null;
rear = null;
}
//判空
public boolean empty(){
return size==0;
}
//插入
public boolean add(E e){
if(empty()){ //如果队列为空
front = new Node(e,null);//只有一个节点,front、rear都指向该节点
rear = front;
}else{
Node<E> newNode = new Node<E>(e, null);
rear.next = newNode; //让尾节点的next指向新增的节点
rear = newNode; //以新节点作为新的尾节点
}
size ++;
return true;
}
//返回队首元素,但不删除
public Node<E> peek(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
return front;
}
}
//出队
public Node<E> poll(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
Node<E> value = front; //得到队列头元素
front = front.next;//让front引用指向原队列头元素的下一个元素
value.next = null; //释放原队列头元素的next引用
size --;
return value;
}
}
//队列长度
public int length(){
return size;
}
public static void main(String[] args) {
LinkQueue<Integer> q = new LinkQueue<>();
q.add(5);q.add(6);q.add(7);
System.out.println(q.length());
q.poll();
System.out.println(q.length());
}
}
所有内容均个人编辑(除特别标注外),如有错误,欢迎指正!
.
网友评论