服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java数据结构基础:顺序队列和循环队列

java数据结构基础:顺序队列和循环队列

2021-11-01 11:04去吧猫头夜鹰 Java教程

下面小编就为大家分享一篇java队列实现方法(顺序队列,循环队列),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

队列:

队列是一种受限制的线性表

只允许在表的一端进行插入,另一端进行删除

插入的一端称作队尾,删除的一端称作队头

具有先进先出的特性

顺序队列:

队列底层数据采用数组存储

设置队头指针front指向队头元素前一个位置,初始值为-1

设置队尾指针rear指向队尾元素,初始值为-1

判满:rear == maxSize - 1

判空:rear == front

代码实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//顺序队列
public class ArrayQueue {
    private int maxSize;    //数组的最大容量
    private int front;        //队头指针
    private int rear;        //队尾指针
    private int[] array;    //存放数据
    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        array = new int[maxSize];
        front = -1;        //指向队头的前一个位置
        rear = -1;        //指向队尾
    }
    //判断队列是否满
    public boolean isFull() {
        return rear == maxSize - 1;
    }
    //判断队列是否空
    public boolean isEmpty() {
        return rear == front;
    }
    //入队
    public void addQueue(int n) {
        //判断队列是否满
        if (isFull()) {
            System.out.println("队列满");
            return;
        }
        rear++;    //rear后移
        array[rear] = n;
    }
    //出队
    public int getQueue() {
        //判断队列是否空
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        front++;    //front后移
        return array[front];
    }
    //取队头数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return array[front + 1];
    }
    //输出队列所有数据
    public void showQueue() {
        //遍历输出
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = 0; i < array.length; i++) {
            System.out.printf("array[%d] = %d\n", i, array[i]);
        }
    }
}

顺序队列存在假溢出现象,故使用循环队列替代顺序队列

循环队列:

队列底层数据仍然采用数组存储

为了便于判空和判满,在数组中预留一个空间,认为只留下一个空间的时候队列为满

设置队头指针front指向队头元素,初始值为0

设置队尾指针rear指向队尾元素的后一个位置,初始值为0

判满:(rear + 1) % maxSize == front

判空:rear == front

取得当前队列有效数据个数:(rear + maxSize - front) % maxSize

代码实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//循环队列
public class CircleQueue {
    private int maxSize;    //数组的最大容量
    private int front;        //队头指针
    private int rear;        //队尾指针
    private int[] array;    //存放数据
    public CircleQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        array = new int[maxSize];
        front = 0;        //指向队头的前一个位置
        rear = 0;        //指向队尾
    }
    //判断队列是否满
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }
    //判断队列是否空
    public boolean isEmpty() {
        return rear == front;
    }
    //入队
    public void addQueue(int n) {
        //判断队列是否满
        if (isFull()) {
            System.out.println("队列满");
            return;
        }
        array[rear] = n;
        rear = (rear + 1) % maxSize;
    }
    //出队
    public int getQueue() {
        //判断队列是否空
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        //保存front对应的值
        int value = array[front];
        front = (front + 1) % maxSize;
        return value;
    }
    //取队头数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return array[front];
    }
    //获取当前队列有效数据个数
    public int size() {
        return (rear + maxSize - front) % maxSize;
    }
    //输出队列所有数据
    public void showQueue() {
        //遍历输出
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        //从front开始遍历
        for (int i = front; i < front + size(); i++) {
            System.out.printf("array[%d] = %d\n", i % maxSize, array[i % maxSize]);
        }
    }
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/qq_25274377/article/details/119214501

延伸 · 阅读

精彩推荐