类型名称:队列(Queue)
数据对象集:一个有0个或多个元素的有穷线性表。
操作集:长度为MaxSize的队列Q Queue,队列元素item ElementType
1、Queue CreatQueue( int MaxSize ):生成长度为MaxSize的空队列;
2、int IsFullQ( Queue Q, int MaxSize ):判断队列Q是否已满;
3、void AddQ( Queue Q, ElementType item ): 将数据元素item插入队列Q中;
4、int IsEmptyQ( Queue Q ): 判断队列Q是否为空;
5、ElementType DeleteQ( Queue Q ):将队头数据元素从队列中删除并返回。
由于队列增加和删除元素的地方不同,所以需要两个标志位。
数组实现队列(非环形队列)
数组实现的非环形队列,在元素删除后,空间不能再利用,是一种非常低效的结构。
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct _qnode
{
ElemType* arr;
int front,rear;
}QNode;
typedef QNode* Queue;
Queue createQueue(int size)
{
Queue currentPtr=(Queue)malloc(sizeof(Queue));
currentPtr->arr=(ElemType*)malloc(sizeof(ElemType)*size);
currentPtr->front=-1;
currentPtr->rear=-1;
return currentPtr;
}
void AddQ(Queue q,ElemType item)
{
q->arr[++q->rear]=item;
}
ElemType DeleteQ(Queue q)
{
return q->arr[++q->front];
}
int isEmpty(Queue q)
{
return (q->front==q->rear);
}
int main(int argc, char const *argv[])
{
Queue q=createQueue(1000);
int n;
while(scanf("%d",&n)!=EOF)
AddQ(q,n);
while(!isEmpty(q))
printf("%d\n",DeleteQ(q));
return 0;
}
数组实现环形队列
假设队列有10个存储位,有效的索引下标有0-9共十个数,两两作差的绝对值也是0-9十个取值,也就是说,从理论上就不可能使用两个索引下标表示出11种情况(队列空和队列满)。
解决方案也是显而易见的,要么添加标志位,要么舍弃掉一个存储位。
根据策略的不同,如果是先存储再修改标志位,可以利用第一个存储位,像这样
0 -1 -1 -1 -1 -1 -1 -1 -1 -1
0 1 -1 -1 -1 -1 -1 -1 -1 -1
0 1 2 -1 -1 -1 -1 -1 -1 -1
0 1 2 3 -1 -1 -1 -1 -1 -1
0 1 2 3 4 -1 -1 -1 -1 -1
0 1 2 3 4 5 -1 -1 -1 -1
0 1 2 3 4 5 6 -1 -1 -1
0 1 2 3 4 5 6 7 -1 -1
0 1 2 3 4 5 6 7 8 -1
The Queue is full,the item won't join in.
0 1 2 3 4 5 6 7 8 -1
The Queue is full,the item won't join in.
0 1 2 3 4 5 6 7 8 -1
如果是先修改标志位,再存储,则可以利用最后一个存储位。
-1 0 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 1 -1 -1 -1 -1 -1 -1 -1
-1 0 1 2 -1 -1 -1 -1 -1 -1
-1 0 1 2 3 -1 -1 -1 -1 -1
-1 0 1 2 3 4 -1 -1 -1 -1
-1 0 1 2 3 4 5 -1 -1 -1
-1 0 1 2 3 4 5 6 -1 -1
-1 0 1 2 3 4 5 6 7 -1
-1 0 1 2 3 4 5 6 7 8
The Queue is full,the item won't join in.
-1 0 1 2 3 4 5 6 7 8
The Queue is full,the item won't join in.
-1 0 1 2 3 4 5 6 7 8
按ZJU数据结构课程所给的代码来看,选择的是先修改标志位,再存储。
一言以蔽之 return q->arr[++(q->front)];和return q->arr[(q->front)++];的区别。
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct _qnode
{
int MaxSize;
int rear;
int front;
int *arr;
}QNode;
typedef QNode* Queue;
Queue createQueue(int size)
{
Queue q=(Queue)malloc(sizeof(QNode));
q->MaxSize=size;
q->rear=0;
q->front=0;
q->arr=(ElemType*)malloc(sizeof(ElemType)*size);
return q;
}
int isEmpty(Queue q)
{
return (q->rear==q->front);
}
int isFull(Queue q)
{
return (q->rear+1)%(q->MaxSize)==(q->front);
}
void AddQ(Queue q,ElemType item)
{
if(isFull(q))
printf("The Queue is full,the item won't join in.\n");
else
{
q->rear=(q->rear+1)%q->MaxSize;
q->arr[q->rear]=item;
}
}
ElemType DeleteQ(Queue q)
{
if(isEmpty(q))
printf("The Queue is empty, there's nothing to return.\n");
else
return q->arr[++(q->front)];
}
void printArray(int *array,int size)
{
int i;
for(i=0;i<size;++i)
printf("%3d%c",array[i],i==size-1?'\n':' ' );
}
int main(int argc, char const *argv[])
{
const int size=50;
Queue q=createQueue(size);
int i;
for(i=0;i<size;++i)
q->arr[i]=-1;
for(i=0;i<10*size;++i)
{
AddQ(q,i);
DeleteQ(q);
printArray(q->arr,size);
}
return 0;
}
但是只要不是刚好满的情况,是不会有什么问题的。
比如一边存一边删,保证空间充足。
上面的一个小Demo可以实现如下效果。