转:http://www.cnblogs.com/langtianya/archive/2013/03/13/2958476.html
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。
在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将是最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。
队列空的条件:front=rear
队列满的条件: rear = MAXSIZE
队列的数组实现
队列可以用数组Q[1…m]来存储,数组的上界m即是队列所容许的最大容量。在队列的运算中需设两个指针:head,队头指针,指向实际队头元素的前一个位置;tail,队尾指针,指向实际队尾元素所在的位置。一般情况下,两个指针的初值设为0,这时队列为空,没有元素。数组定义Q[1…10]。Q(i) i=3,4,5,6,7,8头指针head=2,尾指针tail=8。队列中拥有的元素个数为:L=tail-head现要让排头的元素出队,则需将头指针加1。即head=head+1这时头指针向上移动一个位置,指向Q(3),表示Q(3)已出队。如果想让一个新元素入队,则需尾指针向上移动一个位置。即tail=tail+1这时Q(9)入队。当队尾已经处理在最上面时,即tail=10,如果还要执行入队操作,则要发生"上溢",但实际上队列中还有三个空位置,所以这种溢出称为"假溢出"。
克服假溢出的方法有两种。一种是将队列中的所有元素均向低地址区移动,显然这种方法是很浪费时间的;另一种方法是将数组存储区看成是一个首尾相接的环形区域。当存放到n地址后,下一个地址就"翻转"为1。在结构上采用这种技巧来存储的队列称为循环队列。
队列和栈一样只允许在断点处插入和删除元素。
循环队的入队算法如下:
1、tail=tail+1;
2、若tail=n+1,则tail=1;
3、若head=tail尾指针与头指针重合了,表示元素已装满队列,则作上溢出错处理;
4、否则,Q(tail)=X,结束(X为新入出元素)。
队列和栈一样,有着非常广泛的应用。
注意:(1)有时候队列中还会设置表头结点,就是在对头的前面还有一个结点,这个结点的数据域为空,但是指针域指向对头元素。
(2)另外,上面的计算还可以利用下面给出的公式cq.rear=(cq.front+1)/max;
当有表头结点时,公式变为cq.rear=(cq.front+1)/(max+1)。
队列的链表实现
在队列的形成过程中,可以利用线性链表的原理,来生成一个队列。
基于链表的队列,要动态创建和删除节点,效率较低,但是可以动态增长。
队列采用的FIFO(first in first out),新元素(等待进入队列的元素)总是被插入到链表的尾部,而读取的时候总是从链表的头部开始读取。每次读取一个元素,释放一个元素。所谓的动态创建,动态释放。因而也不存在溢出等问题。由于链表由结构体间接而成,遍历也方便。
队列的基本运算
(1)初始化队列 Qini (Q)
(2)入队 QADD(Q,X) (3)出队 QDel(Q,X)
(4)判断队列是否为空 qempty(Q)
(5)判断队列是否为满qfull(Q)
|
操作 |
类型 |
作用 |
返回值 |
例子 |
|
length(s) |
函数 |
求字符串s的长度 |
整型 |
s:='123456789'; l:=length(s);{l的值为9} |
|
copy(s,w,k) |
函数 |
复制s中从w开始的k位 |
字符串 |
s:='123456789'; s1:=copy(s,3,5);{s1的值是'34567'} |
|
val(s,k,code) |
过程 |
将字符串s转为数值,存在k中;code是错误代码 |
|
var s:string;k,code:integer; begin s:='1234'; val(s,k,code); write(k);{k=1234} |
|
str(i,s) |
过程 |
将数值i转为字符串s |
|
i:=1234; str(i,s); write(s);{s='1234'} |
|
Delete(s,w,k) |
过程 |
在s中删除从第w位开始的k个字符 |
|
s := 'Honest Abe Lincoln'; Delete(s,8,4); Writeln(s); { 'Honest Lincoln' } |
|
Insert(s1, S, w) |
过程 |
将s1插到s中第w位 |
|
S := 'Honest Lincoln'; Insert('Abe ', S, 8); { 'Honest Abe Lincoln' } |
|
Pos(c, S) |
函数 |
求字符c在s中的位置 |
整型 |
S := ' 123.5'; i :=Pos(' ', S);{i的值为1} |
|
+ |
运算符 |
将两个字符串连接起来 |
|
s1:='1234'; s2:='5678'; s:=s1+s2;{'12345678'} |
在STL中,对队列的使用很是较完美
Data_structures
|
▪ 集合 |
▪ 容器 | ||
|
▪ 数组 |
▪ 关联数组 |
▪ Multimap |
▪ 集 |
|
▪ 多重集 |
▪ 散列表 |
▪ 树状数组 | |
|
▪ 列表 |
▪ 链表 |
▪ 队列 |
▪ 堆栈 |
|
▪ 循环队列 |
▪ 跳跃列表 | ||
|
▪ 树 |
▪ 二叉查找树 |
▪ 堆 |
▪ 线段树 |
|
▪ 红黑树 |
▪ AVL树 | ||
|
▪ 图 |
▪ 有向无环图 |
▪ 二元决策图 |
▪ 无向图 |
|
|
package snippet;
public class TestQueue {
int[] queue;
int front;
int rear;
public TestQueue(){
}
public TestQueue(int size){
queue = new int[size];
front = 0;
rear=0;
}
public boolean isEmpty(){
return front == rear;
}
public void poll(int x){
if(rear==queue.length){
System.out.println("己经队满了");
}else{
queue[rear++]=x;
}
}
public void out(){
if(rear==front){
System.out.println("队列为空");
}else{
System.out.print(queue[front]+" ");
front++;
}
}
public int length(){
if(rear>front){
return rear-front;
}else{
return 0;
}
}
public static void main(String args[]){
int index=5;
TestQueue queue=new TestQueue(index);
for(int i=0;i<index;i++){
queue.poll(i);
}
System.out.println("size="+queue.length());
int size=queue.length();
System.out.println("*******出栈操作*******");
for(int i=0; i<size;i++){
queue.out();
}
}
}
package snippet;
class Queue {
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
public Queue(int s) {
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
// 插入方法,队尾是数组的最后一项
public void insert(long j) {
if (rear == maxSize - 1) {
rear = -1;
}
queArray[++rear] = j;
nItems++;
}
// 先进先出
public long remove() {
long temp = queArray[front++];
if (front == maxSize) {
front = 0;
}
nItems--;
return temp;
}
public long peekFront() {
return queArray[front];
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (nItems == maxSize);
}
public int size() {
return nItems;
}
}
class PriorityQ {
private int maxSize;
private long[] queArray;
private int nItems;
public PriorityQ(int s) {
maxSize = s;
queArray = new long[maxSize];
nItems = 0;
}
// 插入方法,从大到小排列
public void insert(long item) {
int j;
if (nItems == 0) {
queArray[nItems++] = item;
}
else {
for (j = nItems - 1; j >= 0; j--) {
if (item > queArray[j]) { // if new item larger,
queArray[j + 1] = queArray[j];
}
else {
break;
}
}
queArray[j + 1] = item;
nItems++;
}
}
// 按照优先级从后往前移除,不再跟先进还是后进有关
public long remove() {
return queArray[--nItems];
}
public long peekMin() {
return queArray[nItems - 1];
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (nItems == maxSize);
}
}
public class QueueApp {
public static void main(String[] args) {
Queue theQueue = new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
theQueue.remove();
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while (!theQueue.isEmpty()) {
long n = theQueue.remove();
System.out.print(n); // 40, 50, 60, 70, 80
System.out.print(" ");
}
System.out.println("");
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);
while (!thePQ.isEmpty()) {
long item = thePQ.remove();
System.out.print(item + " "); // 10, 20, 30, 40, 50
}
System.out.println("");
}
} 转:http://www.cnblogs.com/thlzhf/p/4024454.html
本文深入探讨了队列数据结构的概念、实现方式(数组和链表)、基本操作(入队、出队等),并介绍了队列在实际应用中的广泛用途。包括循环队列的实现、队列链表实现的特性、以及队列与栈的区别。同时,通过实例展示了队列在不同场景下的应用,如数据处理、任务调度等。
150

被折叠的 条评论
为什么被折叠?



