zl程序教程

您现在的位置是:首页 >  后端

当前栏目

JAVA数据结构--队列及优先队伍

JAVA队列数据结构队列 -- 优先 队伍
2023-09-14 08:59:36 时间

注意优先队列在插入时已排序。故在删除,不是以原始插入数据为顺序的。

还要了解两种队列的优点缺少,适合应用的场合。。。

复制代码
 1 class Queue

 3 private int maxSize;

 4 private long[] queArray;

 5 private int front;

 6 private int rear;

 7 private int nItems;

 9 public Queue(int s)

 10 {

 11 maxSize = s;

 12 queArray = new long[maxSize];

 13 front = 0;

 14 rear = -1;

 15 nItems = 0;

 16 }

 17 public void insert(long j)

 18 {

 19 if(rear == maxSize - 1)

 20 rear = -1;

 21 queArray[++rear] = j;

 22 nItems++;

 23 System.out.print("Insert value " + j + " into the queue,\t");

 24 System.out.print("nItems value is " + nItems);

 25 System.out.print("\trear value is " + rear);

 26 System.out.println("\tfront value is " + front);

 27 }

 28 public long remove()

 29 {

 30 long temp = queArray[front++];

 31 if(front == maxSize)

 32 front = 0;

 33 nItems--;

 34 System.out.print("Remove value " + temp + " from the queue,\t");

 35 System.out.print("nItems value is " + nItems);

 36 System.out.print("\trear value is " + rear);

 37 System.out.println("\tfront value is " + front);

 38 return temp;

 39 }

 40 public long peekFront()

 41 {

 42 return queArray[front];

 43 }

 44 public boolean isEmpty()

 45 {

 46 return (nItems == 0);

 47 }

 48 public boolean isFull()

 49 {

 50 return (nItems == maxSize);

 51 }

 52 public int size()

 53 {

 54 return nItems;

 55 }

 56 }

 57 class PriorityQ

 58 {

 59 private int maxSize;

 60 private long[] queArray;

 61 private int nItems;

 63 public PriorityQ(int s)

 64 {

 65 maxSize = s;

 66 queArray = new long[maxSize];

 67 nItems = 0;

 68 }

 69 public void insert(long item)

 70 {

 71 int j;

 73 if(nItems == 0)

 74 queArray[nItems++] = item;

 75 else

 76 {

 77 for(j = nItems - 1; j j--)

 78 {

 79 if(item queArray[j])

 80 queArray[j + 1] = queArray[j];

 81 else

 82 break;

 83 }

 84 queArray[j + 1] = item;

 85 nItems++;

 86 System.out.print("Insert item " + item + " into the priority queue,\t");

 87 System.out.println("nItems value is " + nItems);

 88 }

 89 }

 90 public long remove()

 91 {

 92 long temp = queArray[--nItems];

 93 System.out.print("Remove item " + temp + " from the priority queue,\t");

 94 System.out.println("nItems value is " + nItems);

 95 return temp;

 96 }

 97 public long peekMin()

 98 {

 99 return queArray[nItems - 1];

100 }

101 public boolean isEmpty()

102 {

103 return (nItems == 0);

104 }

105 public boolean isFull()

106 {

107 return (nItems == maxSize);

108 }

109 }

110 public class QueueApp {

112 /**

113 * @param args

114 */

115 public static void main(String[] args) {

116 // TODO Auto-generated method stub

117 System.out.println("Queue Operation:");

118 System.out.println("");

119 Queue myQueue = new Queue(20);

121 myQueue.insert(10);

122 myQueue.insert(20);

123 myQueue.insert(30);

124 myQueue.insert(40);

126 myQueue.remove();

127 myQueue.remove();

128 myQueue.remove();

130 myQueue.insert(50);

131 myQueue.insert(60);

132 myQueue.insert(70);

133 myQueue.insert(80);

134 myQueue.insert(90);

135 myQueue.insert(99);

136 System.out.println("");

137 System.out.println("Priority Queue Operation:");

138 System.out.println("");

139 PriorityQ myPQ = new PriorityQ(20);

140 myPQ.insert(200);

141 myPQ.insert(400);

142 myPQ.insert(100);

143 myPQ.insert(300);

144 myPQ.insert(500);

146 myPQ.remove();

147 myPQ.remove();

149 myPQ.insert(600);

150 myPQ.insert(700);

154 }

156 }
复制代码


输出:


Java数据结构之队列 队列是一个有序列表,可以用数组或是链表来实现。 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
Qz学算法-数据结构篇(稀疏数组、队列) 数据结构包括:线性结构和非线性结构。所以博主会通过这两个角度来对线性结构和非线性结构进行梳理归纳。本篇是稀疏数组和队列
【奇妙的数据结构世界】用图像和代码对队列的使用进行透彻学习 | C++ 简单来说,数据结构是一种辅助程序设计并且进行优化的方法论,它不仅讨论数据的存储与处理的方法,同时也考虑到了数据彼此之间的关系与运算,从而极大程度的提高程序执行的效率,减少对内存空间的占用等。不同种类的数据结构适用于不同的程序应用,选择合适正确的数据结构,可以让算法发挥出更大的性能,给设计的程序带来更高效率的算法。