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

简介:

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

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

复制代码
  1 class Queue
  2 {
  3     private int maxSize;
  4     private long[] queArray;
  5     private int front;
  6     private int rear;
  7     private int nItems;
  8     
  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;
 62     
 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;
 72         
 73         if(nItems == 0)
 74             queArray[nItems++] = item;
 75         else
 76         {
 77             for(j = nItems - 1; j >= 0; 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 {
111 
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);
120         
121         myQueue.insert(10);
122         myQueue.insert(20);
123         myQueue.insert(30);
124         myQueue.insert(40);
125         
126         myQueue.remove();
127         myQueue.remove();
128         myQueue.remove();
129         
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);
145         
146         myPQ.remove();
147         myPQ.remove();
148         
149         myPQ.insert(600);
150         myPQ.insert(700);
151         
152         
153 
154     }
155 
156 }
复制代码


输出:

目录
相关文章
|
C语言
【数据结构】栈和队列(c语言实现)(附源码)
本文介绍了栈和队列两种数据结构。栈是一种只能在一端进行插入和删除操作的线性表,遵循“先进后出”原则;队列则在一端插入、另一端删除,遵循“先进先出”原则。文章详细讲解了栈和队列的结构定义、方法声明及实现,并提供了完整的代码示例。栈和队列在实际应用中非常广泛,如二叉树的层序遍历和快速排序的非递归实现等。
1265 10
|
前端开发 Java
java实现队列数据结构代码详解
本文详细解析了Java中队列数据结构的实现,包括队列的基本概念、应用场景及代码实现。队列是一种遵循“先进先出”原则的线性结构,支持在队尾插入和队头删除操作。文章介绍了顺序队列与链式队列,并重点分析了循环队列的实现方式以解决溢出问题。通过具体代码示例(如`enqueue`入队和`dequeue`出队),展示了队列的操作逻辑,帮助读者深入理解其工作机制。
643 1
|
存储 安全 Java
Java 集合面试题从数据结构到 HashMap 源码剖析详解及长尾考点梳理
本文深入解析Java集合框架,涵盖基础概念、常见集合类型及HashMap的底层数据结构与源码实现。从Collection、Map到Iterator接口,逐一剖析其特性与应用场景。重点解读HashMap在JDK1.7与1.8中的数据结构演变,包括数组+链表+红黑树优化,以及put方法和扩容机制的实现细节。结合订单管理与用户权限管理等实际案例,展示集合框架的应用价值,助你全面掌握相关知识,轻松应对面试与开发需求。
531 3
|
存储 C语言 C++
【C++数据结构——栈与队列】顺序栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现顺序栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 1.初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储
1036 77
|
12月前
232.用栈实现队列,225. 用队列实现栈
在232题中,通过两个栈(`stIn`和`stOut`)模拟队列的先入先出(FIFO)行为。`push`操作将元素压入`stIn`,`pop`和`peek`操作则通过将`stIn`的元素转移到`stOut`来实现队列的顺序访问。 225题则是利用单个队列(`que`)模拟栈的后入先出(LIFO)特性。通过多次调整队列头部元素的位置,确保弹出顺序符合栈的要求。`top`操作直接返回队列尾部元素,`empty`判断队列是否为空。 两题均仅使用基础数据结构操作,展示了栈与队列之间的转换逻辑。
|
存储 Java 编译器
Java 中 .length 的使用方法:深入理解 Java 数据结构中的长度获取机制
本文深入解析了 Java 中 `.length` 的使用方法及其在不同数据结构中的应用。对于数组,通过 `.length` 属性获取元素数量;字符串则使用 `.length()` 方法计算字符数;集合类如 `ArrayList` 采用 `.size()` 方法统计元素个数。此外,基本数据类型和包装类不支持长度属性。掌握这些区别,有助于开发者避免常见错误,提升代码质量。
1115 1
|
存储 C++ 索引
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
【数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】初始化队列、销毁队列、判断队列是否为空、进队列、出队列等。本关任务:编写一个程序实现环形队列的基本运算。(6)出队列序列:yzopq2*(5)依次进队列元素:opq2*(6)出队列序列:bcdef。(2)依次进队列元素:abc。(5)依次进队列元素:def。(2)依次进队列元素:xyz。开始你的任务吧,祝你成功!(4)出队一个元素a。(4)出队一个元素x。
559 13
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
|
算法 调度 C++
STL——栈和队列和优先队列
通过以上对栈、队列和优先队列的详细解释和示例,希望能帮助读者更好地理解和应用这些重要的数据结构。
373 11
☀☀☀☀☀☀☀有关栈和队列应用的oj题讲解☼☼☼☼☼☼☼
### 简介 本文介绍了三种数据结构的实现方法:用两个队列实现栈、用两个栈实现队列以及设计循环队列。具体思路如下: 1. **用两个队列实现栈**: - 插入元素时,选择非空队列进行插入。 - 移除栈顶元素时,将非空队列中的元素依次转移到另一个队列,直到只剩下一个元素,然后弹出该元素。 - 判空条件为两个队列均为空。 2. **用两个栈实现队列**: - 插入元素时,选择非空栈进行插入。 - 移除队首元素时,将非空栈中的元素依次转移到另一个栈,再将这些元素重新放回原栈以保持顺序。 - 判空条件为两个栈均为空。