顺序队列,循环队列,链队列
生活随笔
收集整理的這篇文章主要介紹了
顺序队列,循环队列,链队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
隊列
- 先看隊列接口和結點類
- 1. 順序隊列
- 2. 循環隊列
- 3. 鏈隊列
先看隊列接口和結點類
package com.lovely.queue;/** * 2020年4月26日下午2:42:44* * @author echo lovely**/ public interface IQueue {public void clear(); // 將隊列置空public boolean isEmpty(); // 判斷隊列是否為空public int length(); // 返回隊列的數據元素個數public Object peek(); // 返回隊首元素 public void offer(Object x) throws Exception; // x插入隊列,隊尾入隊public Object poll(); // 返回隊首元素,并刪除 隊首出隊public void display(); // 輸出隊列中所有數據元素}package com.lovely.linearList;/** * 2020年4月1日下午8:25:10* * @author echo lovely** @category 節點類 用于存數據 和 后繼節點*/public class Node {public Object data; // 存放節點數據值public Node next; // 存放后繼節點public Node() {this(null, null);}// 只有節點值的構造函數public Node(Object data) {this(data, null);}// 帶有節點值和后繼節點的構造函數public Node(Object data, Node next) {this.data = data;this.next = next;} }1. 順序隊列
package com.lovely.queue;/*** @author echo lovely** 2020年4月26日下午3:03:05* * 隊列的順序儲存結構* 隊首出隊(刪除),隊尾入隊(插入)。*/ public class SeqQueue implements IQueue {private Object[] queueElem; // 隊列的儲存空間private int maxSize; // 隊列的最大儲存單元個數private int front; // 指向隊首元素private int rear; // 指向隊尾元素的下一個元素 // 構造最大儲存單元為maxSize的空隊列public SeqQueue(int maxSize) {this.maxSize = maxSize;front = rear = 0;queueElem = new Object[maxSize];}@Overridepublic void clear() {// 隊列置空front = rear = 0; }@Overridepublic boolean isEmpty() {// 隊列是否為空 return front == rear;}@Overridepublic int length() {// 隊列長度return rear - front;}@Overridepublic Object peek() {// 返回隊首元素if (isEmpty())return null;return queueElem[front];}@Overridepublic void offer(Object x) throws Exception {// 隊尾入隊if (rear == maxSize)throw new Exception("隊列已滿");queueElem[rear] = x;rear ++; // 指向隊尾的下一個元素}@Overridepublic Object poll() {// 出隊 只不過是不顯示罷了if (isEmpty())return null;front ++; // 指向原來隊首元素的下一個元素return queueElem[front];}@Overridepublic void display() {if (!isEmpty()) {for (int i = front; i < rear; i++) {System.out.print(queueElem[i] + "\t");}} elseSystem.out.println("隊列為空!");}}- 測試
2. 循環隊列
package com.lovely.queue;/*** * @author echo lovely** 2020年5月19日下午8:53:03* * 循環順序隊列* 順序隊列的多次入隊和出隊 會造成有儲存空間 卻不能進行入隊操作的假溢出現象。*/ public class CircleSeqQueue {private Object[] queueElem; // 隊列的儲存空間private int front; // 指向隊首元素private int rear; // 指向隊尾元素的下一個儲存元素private int maxSize; // 隊列的最大儲存單元個數// 構造最大儲存單位個數為maxSize的空隊列public CircleSeqQueue(int maxSize) {front = rear = 0;queueElem = new Object[maxSize];this.maxSize = maxSize;}// 將隊列置空public void clear() {front = rear = 0;}// 判斷隊列是否為空public boolean isEmpty() {return front == rear;}// 隊列的長度public int length() {return (rear - front + maxSize) % maxSize; }// 讀取隊首元素public Object peek() {if (isEmpty())return null;return queueElem[front];}// 入隊public void offer(Object x) throws Exception {if ((rear + 1) % maxSize == front) throw new Exception("隊列已滿");queueElem[rear] = x;rear = (rear + 1) % maxSize;}// 出隊public Object poll() {if (rear == front)return null;Object p = queueElem[front];front = (front + 1) % maxSize;return p;}// 遍歷隊列public void display() {if (!isEmpty()) {for (int i = 0; i < rear; i = (i + 1) % maxSize) {System.out.print(queueElem[i] + " ");}} elseSystem.out.println("此隊列為空!");} }- 測試
3. 鏈隊列
package com.lovely.queue;import com.lovely.linearList.Node;/*** * @author echo lovely* 2020年6月7日下午7:20:02* * 鏈隊列 * 使用單鏈表實現* 實現入隊隊尾 出隊隊首, 沒有中間的插入或者刪除* * 無需頭節點 , front指向頭節點 rear指向隊尾結點便可*/ public class LinkQueue implements IQueue {private Node front; // 隊首指針private Node rear; // 隊尾指針// 構造空隊列public LinkQueue() {front = rear = null;}public void clear() {front = rear = null;}public boolean isEmpty() {// 隊首是否為空return front == null;}@Overridepublic int length() {Node p = front;int length = 0;while (p != null) {p = p.next;length ++;}return length;}// 返回隊首元素值public Object peek() {if (isEmpty()) return null;return front.data;}@Overridepublic void offer(Object x) throws Exception {// 入隊Node s = new Node(x);if (!isEmpty()) { // 隊列非空rear.next = s;rear = s;} else front = rear = s;}// 出隊public Object poll() {if (front == null)return null;Node p = front;front = front.next;if (p == rear) {rear = null; // 刪除結點為隊尾結點時需要修改rear}return p.data;}public void display() {if (!isEmpty()) {for(Node p = front;p != null;p = p.next) {System.out.print(p.data + "\t");}System.out.println();} else {System.out.println("此隊列為空");}}}- 測試
總結
以上是生活随笔為你收集整理的顺序队列,循环队列,链队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql没有开启binlog能恢复数据
- 下一篇: Windows11动态磁贴替代软件大盘点