1.队列的基本介绍
队列是一个有序的列表,可以用数组或是链表来实现
遵循先入先出的原则
2.使用数组来模拟队列
队列本身是有序列表
1 首先需要一个属性用来记录队列的最大容量(如果存入数据元素的重量超过了队列的最大的容量则不允许放入)
2 需要定义两个属性来表示队列的头和尾,当取元素的时候从头部取,存放的时候在尾部进行
代码实现 : 一次性队列
public class ArrayQueue {
private int currentSize;//实际容量
private int maxSize;//最大容量
private int front;//队列首
private int back;//队列尾
private int[] arr;//存放数据的容器
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
front = -1;
back = -1;
arr = new int[maxSize];
}
/**
* 判断队列是否满
* @return
*/
boolean isFull(){
return currentSize==maxSize;
}
/**
* 判断队列是否为空
* @return
*/
boolean isEmpty(){
return currentSize==0;
}
/**
* 进入队列
* @param val
*/
public void enQueue(int val){
if(isFull()){
new Exception("抱歉队列已满,无法再存入数据");
}
back +=1;
arr[back]=val;
currentSize++;
}
/**
* 出队列
*/
public int deQueue(){
if(isEmpty()){
new Exception("抱歉队列为空");
}
front++;
int res =arr[front];
arr[front]=0;
currentSize--;
return res;
}
// 显示队列的所有数据
public void showQueue() {
// 遍历
if (isEmpty()) {
System.out.println("队列空的,没有数据~~");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
}
public class Demo {
public static void main(String[] args) {
//创建一个队列
ArrayQueue queue = new ArrayQueue(3);
char key = ' '; //接收用户输入
Scanner scanner = new Scanner(System.in);//
boolean loop = true;
//输出一个菜单
while(loop) {
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("g(get): 从队列取出数据");
key = scanner.next().charAt(0);//接收一个字符
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
queue.enQueue(value);
break;
case 'g': //取出数据
try {
int res = queue.deQueue();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e': //退出
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~");
}
}
存在一些问题;
队列会出现假溢出的情况 没有达到复用的效果
处理 使用环形数组来解决 取模
public class CircleArrayQueue {
private int currentSize=0;//实际容量
private int maxSize;//最大容量
private int front=0;//队列首
private int back=0;//队列尾
private int[] arr;//存放数据的容器
public CircleArrayQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
}
/**
* 判断队列是否满
* @return
*/
public boolean isFull(){
return currentSize==maxSize;
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty(){
return currentSize==0;
}
/**
* 进入队列
* @param val
*/
public void enQueue(int val){
if(isFull()){
new Exception("抱歉队列已满,无法再存入数据");
}
arr[back]=val;
currentSize++;
back=(back+1)%maxSize;
}
/**
* 出队列
*/
public int deQueue(){
if(isEmpty()){
new Exception("抱歉队列为空");
}
int res =arr[front];
arr[front]=0;
front=(front+1)%maxSize;
currentSize--;
return res;
}
// 显示队列的所有数据
public void showQueue() {
// 遍历
if (isEmpty()) {
System.out.println("队列空的,没有数据~~");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
}
public class Demo {
public static void main(String[] args) {
//创建一个队列
CircleArrayQueue queue = new CircleArrayQueue(3);
char key = ' '; //接收用户输入
Scanner scanner = new Scanner(System.in);//
boolean loop = true;
//输出一个菜单
while(loop) {
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("g(get): 从队列取出数据");
key = scanner.next().charAt(0);//接收一个字符
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
queue.enQueue(value);
break;
case 'g': //取出数据
try {
int res = queue.deQueue();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e': //退出
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~");
}
}
3 链表实现队列
基本介绍:
链表仍然是有序的,是以节点的方式进行存储,是链式存储。每个节点包含data域和next域 ,分为单向链表,双向链表,循环链表
带头节点的单向链表
单向链表实现队列
public class SingleLinkedLIstToQueue<T> {
public Node head; //头结点,用来记录队列头部
public Node current;//当前节点
public int size ;//队列长度
public SingleLinkedLIstToQueue() {
Node node = new Node(null, null);
this.head = node;
this.current = node;
}
private class Node{
T data;
Node next;
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return data.toString() ;
}
}
/**
* 增加元素,进列
* @param ele
*/
public void add(T ele){
size++;
Node node = new Node(ele, null);
current.next=node;
current=node;
}
/**
* 取出元素,出列
* @return
*/
public T remove(){
if(isEmpty()){
new Exception("空空如也");
}
size--;
T data = head.next.data;
head.next=head.next.next;
return data;
}
public boolean isEmpty(){
return size==0;
}
/**
* 遍历
*/
public void showData(){
//判断链表是否为空
if(head.next == null) {
System.out.println("链表为空");
return;
}
//因为头节点,不能动,因此我们需要一个辅助变量来遍历
Node temp = head.next;
while(true) {
//判断是否到链表最后
if(temp == null) {
break;
}
//输出节点的信息
System.out.println(temp);
//将temp后移, 一定小心
temp = temp.next;
}
}
}
双向链表
特点 包含 data域 和 next域(记录下个节点的地址,pre域(记录上个节点的地址)
对比与单向链表的优点
可以进行双向查询 可以不依赖辅助节点来删除(单向链表删除节点时,需要找到上个节点,并将上个节点的next中的地址 指向删除节点的下一个节点的地址 也就是删除节点中的next域)
示意图 :
双向链表可以根据自身节点就可以查询出上级节点和下级节点的地址,完成删除操作(将上级节点的next 指向 删除节点的 next 将下级节点的 pre 指向 删除节点的 pre)
示意图:
代码略
单向环形链表
使用环形链表解决约瑟夫问题
问题描述:N个人围成一圈,从第一个人开始报数,报到m的人出圈,剩下的人继续从1开始报数,报到m的人出圈;如此往复,直到所有人出圈。(模拟此过程,输出出圈的人的序号)
代码实现
public class CircleLink {
private Node head;
class Node{
int i;
Node next;
public Node(int i) {
this.i = i;
}
}
public void add(int i){
if (i < 1) {
System.out.println("i的值不正确");
return;
}
Node cur=null;
for(int num=1;num<=i;num++){
Node node = new Node(num);
if(num==1){
head=node;
head.next=node;
cur=head;
}else{
cur.next=node;
node.next=head;
cur=node;
}
}
}
public void show(){
Node tmp=head;
while(true){
System.out.printf("小孩的编号 %d \n", tmp.i);
if(tmp.next==head){
break;
}
tmp=tmp.next;
}
}
/**
*
* @param startNo
* 表示从第几个小孩开始
* @param countNum
* 表示数几下
* @param nums
* 表示最初还有几个小孩在圈中
*/
public void count(int startNo,int countNum,int nums ){
//1.先创建一个辅助指针helper用来记录末尾 保证每次遍历都能将圈内的小孩全部包含
//2.假如要移动m下,将helper 和 head 指针移动 m-1下 header 指针用来记录出圈的小孩编号
// helper指针用来保证记录圈尾
//先进行校验
if (head == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误, 请重新输入");
return;
}
Node helper= head;
//保证helper指向最后那个节点
while(true){
if(helper.next==head){
break;
}
helper=helper.next;
}
//先移动到报数的小孩哪里
for(int i=0;i<startNo-1;i++){
helper=helper.next;
head=head.next;
}
while (true){
if(helper==head){//说明圈中只有一个节点
break;
}
for(int j = 0; j < countNum - 1; j++){
helper=helper.next;
head=head.next;
}
System.out.println(head.next.i);
head.next=head.next.next;
}
}
}
public class demo {
public static void main(String[] args) {
CircleLink circleLink = new CircleLink();
circleLink.add(5);
circleLink.show();
circleLink.count(2,2,5);
}
}
//打印结果
小孩的编号 1
小孩的编号 2
小孩的编号 3
小孩的编号 4
小孩的编号 5
4
1
3
2
5