AQueue——顺序表实现队列——JAVA
一、readme
···············主要实现用数组实现队列的线性结构(不是循环队列)·················
—————重点知道——先入先出,先从队尾添加,先从队头删出——————
主要实现:
- 入队(包含扩容)和 出队操作
- 其他操作都是大多和顺序表的操作高度相似;
备注:思路都写在文档注释里面了;
二、写代码啦
0、总体框架
/**
* 0、定义数据域:底层数组,长度,大小,头指针,尾指针;(用于操作,就是队列的一些特征的东西)
* 1、入队,包括扩容
* public void in(Object e)
* 2、出队
* public Object out()
* 3、遍历所有元素
* public void printAll()
* 4、测试
* public static void main(String[] args)
*/
1、先定义一下队列操作的数据域
/**
* 数据域:
* 底层数组values;
* 长度size;
* 大小length;
* 队尾指针tail;
* 队头指针head;
*/
Object[] values;
int size;
int length;
int tail;
int head;
2.入队操作(包含扩容)
/**
* 思路:
* 1、判断是否满了
* 2、扩容
* 3、values[tail++]=e;
* 4、size++
*/
//tail入队,head出队,tail的位置不存放元素,像PC一样,tail是下一次入队的位置,head是下一次出队的位置(一点理解)
public void in(Object e) {
//栈满
if (tail == length - 1) {
System.out.println("栈满");
//扩容
int oldLength = length;
int newLength = oldLength + (oldLength >> 1);//注意先进行移位运算,再进行加法运算,加法的优先级高于移位运算(错过)
Object[] newValues = new Object[newLength];
for (int i = 0; i < oldLength; i++) {
newValues[i] = values[i];
}
length = newLength;
values = newValues;
System.out.println("扩容完成");
}
values[tail++] = e;
size++;
}
3、出队
/**
* 思路:
* 1、判断是否为空
* 2、values[head++]=null;
* 3、size--
* 4、返回删除的元素
*/
public Object out() {
if (size == 0) {
System.out.println("栈空");
return null;
}
Object e = values[head];
values[head++] = null;
size--;
return e;
}
4、打印队列
//遍历所有元素
public void printAll() {
for (int i = head; i < tail; i++) {
System.out.println(values[i]);
}
}
三、测试
//测试
public static void main(String[] args) {
Queue queue = new Queue();
queue.in(1);
queue.in(2);
queue.in(3);
queue.in(4);
queue.in(5);
queue.in(6);
queue.in(7);
queue.in(8);
queue.in(9);
queue.in(10);
queue.in(11);
queue.in(12);
queue.in(13);
queue.in(14);
queue.in(15);
queue.in(16);
queue.in(17);
queue.in(18);
queue.in(19);
queue.in(20);
queue.in(21);
queue.in(22);
queue.in(23);
queue.in(24);
queue.in(25);
queue.in(26);
queue.out();
queue.out();
queue.out();
System.out.println(queue.length);
System.out.println(queue.head);
System.out.println(queue.tail);
queue.printAll();
}
附录所有代码如下:
import TArrayList.TArrayList;
public class Queue {
/**
* 0、定义数据域:底层数组,长度,大小,头指针,尾指针;(用于操作,就是队列的一些特征的东西)
* 1、入队,包括扩容
* public void in(Object e)
* 2、出队
* public Object out()
* 3、遍历所有元素
* public void printAll()
* 4、测试
*
*/
/**
* 数据域:
* 底层数组values;
* 长度size;
* 大小length;
* 队尾指针tail;
* 队头指针head;
*/
Object[] values;
int size;
int length;
int tail;
int head;
//构造方法
public Queue() {
values = new Object[10];
size = 0;
length = 10;
tail = 0;
head = 0;
}
public Queue(int length) {
values = new Object[length];
size = 0;
this.length = length;
tail = 0;
head = 0;
}
//方法
/**
* 思路:
* 1、判断是否满了
* 2、扩容
* 3、values[tail++]=e;
* 4、size++
*/
//tail入队,head出队,tail的位置不存放元素,像PC一样,tail是下一次入队的位置,head是下一次出队的位置
public void in(Object e) {
//栈满
if (tail == length - 1) {
System.out.println("栈满");
//扩容
int oldLength = length;
int newLength = oldLength + (oldLength >> 1);//注意先进行移位运算,再进行加法运算,加法的优先级高于移位运算(错过)
Object[] newValues = new Object[newLength];
for (int i = 0; i < oldLength; i++) {
newValues[i] = values[i];
}
length = newLength;
values = newValues;
System.out.println("扩容完成");
}
values[tail++] = e;
size++;
}
/**
* 思路:
* 1、判断是否为空
* 2、values[head++]=null;
* 3、size--
* 4、返回删除的元素
*/
public Object out() {
if (size == 0) {
System.out.println("栈空");
return null;
}
Object e = values[head];
values[head++] = null;
size--;
return e;
}
//遍历所有元素
public void printAll() {
for (int i = head; i < tail; i++) {
System.out.println(values[i]);
}
}
//测试
public static void main(String[] args) {
Queue queue = new Queue();
queue.in(1);
queue.in(2);
queue.in(3);
queue.in(4);
queue.in(5);
queue.in(6);
queue.in(7);
queue.in(8);
queue.in(9);
queue.in(10);
queue.in(11);
queue.in(12);
queue.in(13);
queue.in(14);
queue.in(15);
queue.in(16);
queue.in(17);
queue.in(18);
queue.in(19);
queue.in(20);
queue.in(21);
queue.in(22);
queue.in(23);
queue.in(24);
queue.in(25);
queue.in(26);
queue.out();
queue.out();
queue.out();
System.out.println(queue.length);
System.out.println(queue.head);
System.out.println(queue.tail);
queue.printAll();
}
}