队列
顺序队列
结构体中成员:
1.data
给出队列存储空间的起始地址
2.front
为队头指针,它指向队头元素
3.rear
为队尾指针,它指向下一个入队元素的存储位置
4.max
指明队列存储空间中最多可存储的数据元素个数。(通常为了区分队列空和满,会在队列尾留一个空数据单元,此时队列最多可放max-1个数据元素)
特别说明:空间的开始地址为data
,连续空间里的位置编号从data所指的开始位置起,到该空间的结束位置,编号依次是0,1,2,…,max-1
。在图1的示例中max=6
。下一个出队元素(这里是队列的头结点)所存储的位置编号用front
给出,下一个入队元素应存储的位置编号用rear
给出。
顺序队列操作
1.判空:当front
与rear
相等时,队列为空
2.判满:当front=0,rear=max-1
或者front=rear+1
时,队列为满
3.出队:出队操作的前提是队列不为空。每出队一个元素(将front
处的元素出队),就将front
加 1 ;加 1 后,如果front
超出最后一个位置max-1
,就将front
重新设置为 0
4.入队:入队操作的前提是队列不为满。每入队一个元素(新元素存储在rear处),就将rear
加 1 ;加 1 后,如果rear
超出最后一个位置max-1
,就将rear
重新设置为 0
- 在给定图1的状态下,连续 2 次出队操作,这时的状态则变为如图 2 所示的状态
- 在给定图 2 的状态下,连续 2 次入队操作(依次入队 23 、78 ),这时的状态则如图 3 所示
SeqQueue.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SeqQueue.h"
SeqQueue* SQ_Create(int maxlen)
// 创建顺序队列, 队列最多存储maxlen个队列元素。
{
SeqQueue* sq=(SeqQueue*)malloc(sizeof(SeqQueue));
sq->data=(T*)malloc(sizeof(T)*(maxlen+1));
sq->front=sq->rear=0;
sq->max=maxlen+1;
return sq;
}
void SQ_Free(SeqQueue* sq)
// 释放队列空间,以删除队列。
{
free(sq->data);
free(sq);
}
void SQ_MakeEmpty(SeqQueue* sq)
// 将队列置空。
{
sq->front=0;
sq->rear=0;
}
bool SQ_IsEmpty(SeqQueue* sq)
// 判断队列是否为空,为空返回true,否则返回false。
{
// 请在Begin-End之间补充代码,完成队列是否为空的判断。
/********** Begin *********/
if(sq->front == sq->rear) return true;
return false;
/********** End **********/
}
bool SQ_IsFull(SeqQueue* sq)
// 判断队列是否为满。为满返回true,否则返回false。
{
// 请在Begin-End之间补充代码,完成队列是否为满的判断。
/********** Begin *********/
if((sq->front == 0&&sq->rear == sq->max - 1)||sq->front == sq->rear + 1) return true;
return false;
/********** End **********/
}
int SQ_Length(SeqQueue* sq)
// 队列长度。
{
// 请在Begin-End之间补充代码,获取队列长度。
/********** Begin *********/
if(SQ_IsFull(sq)) return sq->max-1;
return sq->rear - sq->front;
/********** End **********/
}
bool SQ_In(SeqQueue* sq, T x)
// 将x入队。若入队失败(队列满),则返回false,否则返回true。
{
// 请在Begin-End之间补充代码,将 x 入队。
/********** Begin *********/
if(SQ_IsFull(sq)) return false;
sq->data[sq->rear] = x;
sq->rear++;
if(sq->rear > sq->max - 1) sq->rear = 0;
return true;
/********** End **********/
}
bool SQ_Out(SeqQueue* sq, T& item)
// 从队列sq出队一个元素,返回时item为出队的元素的值。若出队成功(队列不为空),则返回true,否则(队列空),返回false,此时item不会返回有效值。
{
// 请在Begin-End之间补充代码,完成元素出队操作。
/********** Begin *********/
if(SQ_IsEmpty(sq))
{
item = -1;
return false;
}
item = sq->data[sq->front];
sq->front++;
if(sq->front > sq->max - 1) sq->front = 0;
return true;
item = sq->data[sq->front];
sq->front++;
if(sq->front > sq->max - 1) sq->front = 0;
return true;
/********** End **********/
}
bool SQ_Head(SeqQueue* sq, T& head)
// 获取队列头结点元素,返回时head保存头结点元素。
// 若获取失败(队列空),则返回值为false,否则返回值为true。
{
if ( SQ_IsEmpty(sq) ){
return false;
}
else {
head = sq->data[sq->front];
return true;
}
}
void SQ_Print(SeqQueue* sq)
// 依次打印出队列中的每个元素。
{
int i=sq->front;
if (SQ_IsEmpty(sq)) {
printf("queue is emtpy");
return;
}
for (i=sq->front; i!=sq->rear; i=(i+1)%sq->max) {
printf("%d ", sq->data[i]);
}
printf("\n");
}
SeqQueue.h
#if !defined(SEQUENCE_QUEUE_H_LIELJE7398CNHD_INCLUDE_)
#define SEQUENCE_QUEUE_H_LIELJE7398CNHD_INCLUDE_
typedef int T;
struct SeqQueue {
T* data; // 指向数据元素数组的指针。
int front; // 下一个出队元素的数组下标。
int rear; // 下一个入队元素应该存放的单元的数组下标。
int max; // 队列中最多可放max-1个数据元素,留一个空数据单元以区分空和满。
};
SeqQueue* SQ_Create(int maxlen);
void SQ_Free(SeqQueue* sq);
void SQ_MakeEmpty(SeqQueue* sq);
bool SQ_IsEmpty(SeqQueue* sq);
bool SQ_IsFull(SeqQueue* sq);
int SQ_Length(SeqQueue* sq);
bool SQ_In(SeqQueue* sq, T x);
bool SQ_Out(SeqQueue* sq, T& item);
bool SQ_Head(SeqQueue* sq, T& head);
void SQ_Print(SeqQueue* sq);
#endif
Main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SeqQueue.h"
#include <string.h>
#pragma warning(disable:4996)
int main()
{
int maxlen;
scanf("%d", &maxlen);
SeqQueue* sq=SQ_Create(maxlen);
char dowhat[100];
while(true) {
scanf("%s", dowhat);
if (!strcmp(dowhat,"in")) {
T x;
scanf("%d", &x);
SQ_In(sq,x);
}else if (!strcmp(dowhat,"out")) {
T item;
SQ_Out(sq, item);
}
else {
break;
}
}
int length=SQ_Length(sq);
printf("Queue length: %d\n", length);
printf("Queue data: ");
SQ_Print(sq);
SQ_Free(sq);
}
样例输入:
5
in 1
in 3
in 5
in 9
in 12
out
end
样例输出:
Queue length: 4
Queue data: 3 5 9 12
链队列
链式储存方式
当队列是空队列时,rear
指向附加头结点,附加头结点的数据项等于 0 ,如图 2 所示
- 在给定图 1 的状态下,进队一个元素 25 以后的状态如图 3 所示:
- 若出队一个元素是指将当前队列头结点去掉。则在给定图 3 的状态下,进行一次出队后的状态如图 4 所示:
CLnkQueue.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CLnkQueue.h"
LNode* CLQ_Create()
// 创建一个队列。
{
LNode* rear=(LNode*)malloc(sizeof(LNode));
rear->data = 0;
rear->next = rear;
return rear;
}
void CLQ_Free(LNode* rear)
// rear指向尾结点。
{
CLQ_MakeEmpty(rear);
free(rear);
}
void CLQ_MakeEmpty(LNode* & rear)
// rear指向尾结点。
// 将队列变为空队列。
{
T item;
while(!CLQ_IsEmpty(rear))
CLQ_Out(rear,item);
}
bool CLQ_IsEmpty(LNode* rear)
// 判断队列是否为空。
{
// 请在Begin-End之间补充代码,完成队列是否为空的判断。
/********** Begin *********/
if(rear == rear->next) {
rear->data = 0;
return true;
}
return false;
/********** End **********/
}
int CLQ_Length(LNode* rear)
// 返回队列长度,rear指向尾结点。
{
// 请在Begin-End之间补充代码,获取队列长度。
/********** Begin *********/
int cnt = 0;
LNode* p = rear->next;
while(p != rear){
cnt++;
p = p->next;
}
return cnt;
/********** End **********/
}
void CLQ_In(LNode* & rear, T x)
// 入队列, 新结点加入链表尾部。rear指向尾结点。
{
// 请在Begin-End之间补充代码,完成新结点入队操作。
/********** Begin *********/
LNode *s =(LNode*)malloc(sizeof(LNode));
LNode *t;
t = rear->next;
s->data = x;
rear->next =s;
rear =s;
rear->next = t;
/********** End **********/
}
bool CLQ_Out(LNode* & rear, T& item)
// 出队列。空队列时,返回值为false。rear指向尾结点。
{
// 请在Begin-End之间补充代码,完成结点出队操作。
/********** Begin *********/
if(CLQ_IsEmpty(rear)) return false;
LNode *t;
item = t->data;
t = rear->next;
rear->next = t->next;
free(t);
return true;
/********** End **********/
}
bool CLQ_Head(LNode* rear, T& item)
// rear指向尾结点。
// 获取队列头。空队列时返回值为false。
{
if (CLQ_IsEmpty(rear))
return false;
item = rear->next->next->data;
return true;
}
void CLQ_Print(LNode* rear)
// 打印队列。
{
if (CLQ_IsEmpty(rear)) {
printf("The queue is: empty. \n");
return;
}
LNode* node=rear->next->next;
do {
printf("%d ", node->data);
node = node->next;
} while (node != rear->next);
//printf("\n");
}
CLnkQueue.h
#if !defined(LINKED_QUEUE_H_983982)
#define LINKED_QUEUE_H_983982
typedef int T;
struct LNode {
T data;
LNode* next;
};
LNode* CLQ_Create();
void CLQ_Free(LNode* rear);
void CLQ_MakeEmpty(LNode*& rear);
bool CLQ_IsEmpty(LNode* rear);
int CLQ_Length(LNode* rear);
void CLQ_In(LNode* & rear, T x);
bool CLQ_Out(LNode* & rear, T& item);
bool CLQ_Head(LNode* rear, T& item);
void CLQ_Print(LNode* rear);
#endif
Main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CLnkQueue.h"
#pragma warning(disable:4996)
int main()
{
LNode* rear=CLQ_Create();
char dowhat[100];
while(true) {
scanf("%s", dowhat);
if (!strcmp(dowhat,"in")) {
T x;
scanf("%d", &x);
CLQ_In(rear,x);
}else if (!strcmp(dowhat,"out")) {
T item;
CLQ_Out(rear, item);
}
else {
break;
}
}
int length=CLQ_Length(rear);
printf("Queue length: %d\n", length);
printf("Queue data: ");
CLQ_Print(rear);
CLQ_Free(rear);
}
样例输入:
in 1
in 3
in 5
in 9
in 12
out
end
样例输出:
Queue length: 4
Queue data: 3 5 9 12