2.线性表——队列

1.什么是队列

  [1] 队列只允许在表的前端(front)进行删除操作,且在表的后端(rear)进行添加操作;

  [2] 队列是“先进先出”,进行插入操作的后端称为队尾rear,进行删除操作的前端称为队首front;

  [3] 队列的种类:顺序队列和循环队列。

  [4] 队列同栈一样,即能利用数组实现(线性存储),也能利用链表实现(链式存储)。

  [5] 队列的表示:

1.1 队列的表示

 

 2.栈的基本操作

  [1] 队列的存储结构

  [2] 进队

  [3] 出队

  [4] 判断空,判断满

  [5] 队列的遍历

  [6] 队列清空

  备注: 静态队列       【利用数组来实现循环队列】

       链式(动态)队列  【利用链表实现】

 

3.静态队列的实现(循环队列)

  [1] 静态队列的存储结构

 

package com.Queue;

public class MyArray {
    final int QUEUESIZE = 5;
    int[] array ;
    public int front; // 队首
    public int rear;  // 队尾

    public MyArray() {
        front = rear = -1;
        array = new int[QUEUESIZE]; // 设置数组容量为QUEUESIZE
    }
}

 

  [2] 静态队列的基本操作实现

 

  1 package com.Queue;
  2 
  3 // 队列:先进先出
  4 // 数组实现,循环队列,尾进头出
  5 // 循环队列设置front(首)指向第一个元素,rear(尾)指向最后一个元素的下一个元素
  6 // 则容量为5的数组,只能存放4个元素;
  7 
  8 import com.Stack.MyStack;
  9 
 10 public class DemoArrayQueue {
 11     static MyArray arr = new MyArray();
 12 
 13     public static void init(){
 14         arr.front = 0;
 15         arr.rear = 0;
 16     }
 17 
 18     public static boolean isEmpty(){
 19         if(arr.rear == arr.front)
 20             return true;
 21         else
 22             return false;
 23     }
 24     public static boolean isFull(){
 25         //if(arr.front-arr.rear == arr.QUEUESIZE-1)
 26         if((arr.rear+1) % arr.QUEUESIZE == arr.front)
 27             return true;
 28         else
 29             return false;
 30     }
 31 
 32     public static void printOut(){
 33         if(isEmpty()){
 34             System.out.println("当前队列为空,输出失败!");
 35             return;
 36         }
 37         int temp = arr.front;
 38         while(temp!=arr.rear){
 39             System.out.print(arr.array[temp]+" ");
 40             temp = (temp+1)%arr.QUEUESIZE;
 41         }
 42         System.out.println();
 43     }
 44 
 45     // 入队:尾进
 46     public static void inQueue(int num){
 47         if(isFull()){
 48             System.out.println("该队列满!无法插入!");
 49             return;
 50         }
 51         arr.array[arr.rear] = num;
 52         arr.rear = (arr.rear+1) % arr.QUEUESIZE;
 53     }
 54 
 55     // 出队:头出
 56     public static void outQueue(){
 57         if(isEmpty()){
 58             System.out.println("队列为空!无法出队!");
 59             return;
 60         }
 61 
 62         // 此种逐一移动数组删除队列元素的方法,其时间复杂度过高!
 63         // 所以,静态队列在初始创建时就可直接创建为循环队列。
 64         /*
 65         System.out.println("当前队列出队:"+arr.array[arr.front]);
 66         for(int i = arr.front+1;i<=arr.rear;i++){
 67             arr.array[arr.front] = arr.array[i];
 68         }*/
 69         // 循环队列:出队操作
 70         int num = arr.array[arr.front];
 71         System.out.println("当前队列出队:"+num);
 72         arr.front = (arr.front+1) % arr.QUEUESIZE;
 73     }
 74 
 75     // 清空队列
 76     public static void clear(){
 77         arr.array = null;
 78         arr.rear = arr.front = -1;
 79         System.out.println("清空队列!");
 80     }
 81 
 82     public static void main(String[] args) {
 83         init();
 84         printOut();
 85 
 86         System.out.println("入队:");
 87         inQueue(10);
 88         inQueue(20);
 89         inQueue(30);
 90         inQueue(40);
 91         printOut();
 92         inQueue(50);
 93 
 94         System.out.println("出队:");
 95         outQueue();
 96         printOut();
 97 
 98         inQueue(60);
 99         printOut();
100 
101         clear();
102         printOut();
103     }
104 }

  [3] 运行结果

 

 

 

4.链式队列的实现

  [1] 链式队列的存储结构

 

package com.Queue;

public class MyNode {
    private int data;
    private MyNode next;

    public MyNode(int data,MyNode next) {
        this.data = data;
        this.next = next;
    }
    public MyNode(MyNode next) {
        this.next = next;
    }
    public MyNode() {
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public MyNode getNext() {
        return next;
    }

    public void setNext(MyNode next) {
        this.next = next;
    }
}

 

  [2] 链式队列的基本操作实现

 

 1 package com.Queue;
 2 
 3 // 队列:先进先出
 4 // 链表实现,普通队列,尾进头出
 5 
 6 public class DemoQueue {
 7     static final int SIZE = 10;
 8     static MyNode head;
 9     static MyNode tail;
10     static int stackSize = 0;
11 
12     // 无参构造函数
13     public DemoQueue(){
14         head = tail = null;
15     }
16 
17     public static boolean isEmpty(){
18         if(stackSize == 0 )
19             return true;
20         else
21             return false;
22     }
23     public static boolean isFull(){
24         if(stackSize == SIZE)
25             return true;
26         else
27             return false;
28     }
29 
30     // 当前队列输出
31     public static void printOut(){
32         if(isEmpty()){
33             System.out.println("当前队列为空!无法输出!");
34         }
35         MyNode temp = head;
36         for (int i = 0; i < stackSize; i++) {
37             System.out.print(temp.getData() + " ");
38             temp = temp.getNext();
39         }
40         System.out.println();
41     }
42 
43     // 出队:队首出队
44     public static void outQueue(){
45         if(isEmpty()){
46             System.out.println("队列为空,无法出队!");
47             return;
48         }
49         System.out.println("出队:"+head.getData());
50         head = head.getNext();
51         stackSize--;
52     }
53 
54     // 入队:队尾入队
55     public static void enQueue(int num){
56         if(isFull()){
57             System.out.println("队列已满,无法入队!");
58             return;
59         }
60         if(isEmpty()){
61             tail = new MyNode(num,null);
62             head = tail;
63             stackSize++;
64             System.out.println("1.head,tail:"+head.getData()+" "+tail.getData());
65 
66         }else {
67             MyNode node = new MyNode(num, null);
68             tail.setNext(node);
69             tail = node;
70             stackSize++;
71             System.out.println("2.head,tail:" + head.getData() + " " + tail.getData());
72         }
73     }
74 
75     public static void clear(){
76         head = tail = null;
77         stackSize = 0;
78         if(isEmpty()){
79             System.out.println("当前队列清空完毕!");
80         }
81     }
82 
83     public static void main(String[] args) {
84         //调用无参构造函数中tail=head=null;(初始化)
85         DemoQueue node = new DemoQueue();
86         System.out.println("入队:");
87         node.enQueue(10);
88         node.enQueue(20);
89         node.enQueue(30);
90         node.printOut();
91         System.out.println("出队:");
92         node.outQueue();
93         node.printOut();
94 
95         node.clear();
96         node.printOut();
97     }
98 }

 

  [3] 运行结果

 

 

 

备注:学习过程中难免会出现很多错误,请大家指教!有问题可以直接评论我,谢谢!

 

posted @ 2020-04-13 22:03  周小周mie  阅读(167)  评论(0)    收藏  举报