函数题:循环队列操作集
本题要求实现循环队列操作集。
### 函数接口定义:
c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int ElementType;
struct QueueRecord;
typedef struct QueueRecord *Queue;
//如果队列为空,返回1,否则返回0
int IsEmpty(Queue Q);
//如果队列满,返回1,否则返回0
int IsFull(Queue Q);
//创建队列,返回指向QueueRecord结构体的指针
Queue CreateQueue(int MaxElements);
//销毁队列
void DisposeQueue(Queue Q);
//清空队列
void MakeEmpty(Queue Q);
//入队(加入队尾)
void Enqueue(ElementType X, Queue Q);
//返回队头元素
ElementType Front(Queue Q);
//出队(删除队头元素)
void Dequeue(Queue Q);
//删除队头元素,并将该元素返回
ElementType FrontAndDequeue(Queue Q);
//返回队列长度(队列中有效元素的个数)
int Size(Queue Q);
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q);
//队列结构体。与Figure3.57相比,取消了Size分量
struct QueueRecord
{
int Capacity; //队列容量
int Front; //队头在数组中的下标
int Rear; //队尾在数组中的下标
ElementType *Array;//指向存储空间的指针
};
### 裁判测试程序样例:
c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int ElementType;
struct QueueRecord;
typedef struct QueueRecord *Queue;
//如果队列为空,返回1,否则返回0
int IsEmpty(Queue Q);
//如果队列满,返回1,否则返回0
int IsFull(Queue Q);
//创建队列,返回指向QueueRecord结构体的指针
Queue CreateQueue(int MaxElements);
//销毁队列
void DisposeQueue(Queue Q);
//清空队列
void MakeEmpty(Queue Q);
//入队(加入队尾)
void Enqueue(ElementType X, Queue Q);
//返回队头元素
ElementType Front(Queue Q);
//出队(删除队头元素)
void Dequeue(Queue Q);
//删除队头元素,并将该元素返回
ElementType FrontAndDequeue(Queue Q);
//返回队列长度(队列中有效元素的个数)
int Size(Queue Q);
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q);
//队列结构体。与Figure3.57相比,取消了Size分量
struct QueueRecord
{
int Capacity; //队列容量
int Front; //队头在数组中的下标
int Rear; //队尾在数组中的下标
ElementType *Array;//指向存储空间的指针
};
int main(void)
{
char cmd[20];
Queue Q = CreateQueue(9);
int x;
scanf("%s", cmd);
while (strcmp(cmd, "END") != 0)
{
if (strcmp(cmd, "ENQUEUE") == 0)
{
if (IsFull(Q) == 1)
{
printf("FULL\n");
}
else
{
scanf("%d", &x);
Enqueue(x, Q);
}
}
if (strcmp(cmd, "DEQUEUE") == 0)
{
if (IsEmpty(Q) == 1)
{
printf("EMPTY\n");
}
else
{
Dequeue(Q);
}
}
if (strcmp(cmd, "GETFRONT") == 0)
{
x = Front(Q);
printf("%d\n", x);
}
if (strcmp(cmd, "SIZE") == 0)
{
printf("SIZE = %d\n", Size(Q));
}
if (strcmp(cmd, "SHOW") == 0)
{
show(Q);
}
scanf("%s", cmd);
}
DisposeQueue(Q);
return 0;
}
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q)
{
if (Q->Front == Q->Rear)
return;
if (Q->Front < Q->Rear)
{
for (int i = 0; i < Q->Front; i++)
{
printf("X ");
}
for (int i = Q->Front; i < Q->Rear; i++)
{
printf("%d ", Q->Array[i]);
}
for (int i = Q->Rear; i < Q->Capacity; i++)
{
printf("X ");
}
}
else
{
for (int i = 0; i < Q->Rear; i++)
{
printf("%d ", Q->Array[i]);
}
for (int i = Q->Rear; i < Q->Front; i++)
{
printf("X ");
}
for (int i = Q->Front; i < Q->Capacity; i++)
{
printf("%d ", Q->Array[i]);
}
}
printf("\n");
}
/* 请在这里填写答案 */
### 输入样例:
in
DEQUEUE
ENQUEUE 1
ENQUEUE 2
ENQUEUE 3
SHOW
GETFRONT
SIZE
ENQUEUE 4
ENQUEUE 5
ENQUEUE 6
ENQUEUE 7
ENQUEUE 8
ENQUEUE 9
ENQUEUE 10
ENQUEUE 11
SHOW
GETFRONT
SIZE
DEQUEUE
DEQUEUE
DEQUEUE
SHOW
GETFRONT
SIZE
ENQUEUE 12
ENQUEUE 13
SHOW
GETFRONT
SIZE
END
### 输出样例:
out
EMPTY
1 2 3 X X X X X X X
1
SIZE = 3
FULL
FULL
1 2 3 4 5 6 7 8 9 X
1
SIZE = 9
X X X 4 5 6 7 8 9 X
4
SIZE = 6
13 X X 4 5 6 7 8 9 12
4
SIZE = 8
答案:若无答案欢迎评论
### 函数接口定义:
c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int ElementType;
struct QueueRecord;
typedef struct QueueRecord *Queue;
//如果队列为空,返回1,否则返回0
int IsEmpty(Queue Q);
//如果队列满,返回1,否则返回0
int IsFull(Queue Q);
//创建队列,返回指向QueueRecord结构体的指针
Queue CreateQueue(int MaxElements);
//销毁队列
void DisposeQueue(Queue Q);
//清空队列
void MakeEmpty(Queue Q);
//入队(加入队尾)
void Enqueue(ElementType X, Queue Q);
//返回队头元素
ElementType Front(Queue Q);
//出队(删除队头元素)
void Dequeue(Queue Q);
//删除队头元素,并将该元素返回
ElementType FrontAndDequeue(Queue Q);
//返回队列长度(队列中有效元素的个数)
int Size(Queue Q);
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q);
//队列结构体。与Figure3.57相比,取消了Size分量
struct QueueRecord
{
int Capacity; //队列容量
int Front; //队头在数组中的下标
int Rear; //队尾在数组中的下标
ElementType *Array;//指向存储空间的指针
};
### 裁判测试程序样例:
c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int ElementType;
struct QueueRecord;
typedef struct QueueRecord *Queue;
//如果队列为空,返回1,否则返回0
int IsEmpty(Queue Q);
//如果队列满,返回1,否则返回0
int IsFull(Queue Q);
//创建队列,返回指向QueueRecord结构体的指针
Queue CreateQueue(int MaxElements);
//销毁队列
void DisposeQueue(Queue Q);
//清空队列
void MakeEmpty(Queue Q);
//入队(加入队尾)
void Enqueue(ElementType X, Queue Q);
//返回队头元素
ElementType Front(Queue Q);
//出队(删除队头元素)
void Dequeue(Queue Q);
//删除队头元素,并将该元素返回
ElementType FrontAndDequeue(Queue Q);
//返回队列长度(队列中有效元素的个数)
int Size(Queue Q);
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q);
//队列结构体。与Figure3.57相比,取消了Size分量
struct QueueRecord
{
int Capacity; //队列容量
int Front; //队头在数组中的下标
int Rear; //队尾在数组中的下标
ElementType *Array;//指向存储空间的指针
};
int main(void)
{
char cmd[20];
Queue Q = CreateQueue(9);
int x;
scanf("%s", cmd);
while (strcmp(cmd, "END") != 0)
{
if (strcmp(cmd, "ENQUEUE") == 0)
{
if (IsFull(Q) == 1)
{
printf("FULL\n");
}
else
{
scanf("%d", &x);
Enqueue(x, Q);
}
}
if (strcmp(cmd, "DEQUEUE") == 0)
{
if (IsEmpty(Q) == 1)
{
printf("EMPTY\n");
}
else
{
Dequeue(Q);
}
}
if (strcmp(cmd, "GETFRONT") == 0)
{
x = Front(Q);
printf("%d\n", x);
}
if (strcmp(cmd, "SIZE") == 0)
{
printf("SIZE = %d\n", Size(Q));
}
if (strcmp(cmd, "SHOW") == 0)
{
show(Q);
}
scanf("%s", cmd);
}
DisposeQueue(Q);
return 0;
}
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q)
{
if (Q->Front == Q->Rear)
return;
if (Q->Front < Q->Rear)
{
for (int i = 0; i < Q->Front; i++)
{
printf("X ");
}
for (int i = Q->Front; i < Q->Rear; i++)
{
printf("%d ", Q->Array[i]);
}
for (int i = Q->Rear; i < Q->Capacity; i++)
{
printf("X ");
}
}
else
{
for (int i = 0; i < Q->Rear; i++)
{
printf("%d ", Q->Array[i]);
}
for (int i = Q->Rear; i < Q->Front; i++)
{
printf("X ");
}
for (int i = Q->Front; i < Q->Capacity; i++)
{
printf("%d ", Q->Array[i]);
}
}
printf("\n");
}
/* 请在这里填写答案 */
### 输入样例:
in
DEQUEUE
ENQUEUE 1
ENQUEUE 2
ENQUEUE 3
SHOW
GETFRONT
SIZE
ENQUEUE 4
ENQUEUE 5
ENQUEUE 6
ENQUEUE 7
ENQUEUE 8
ENQUEUE 9
ENQUEUE 10
ENQUEUE 11
SHOW
GETFRONT
SIZE
DEQUEUE
DEQUEUE
DEQUEUE
SHOW
GETFRONT
SIZE
ENQUEUE 12
ENQUEUE 13
SHOW
GETFRONT
SIZE
END
### 输出样例:
out
EMPTY
1 2 3 X X X X X X X
1
SIZE = 3
FULL
FULL
1 2 3 4 5 6 7 8 9 X
1
SIZE = 9
X X X 4 5 6 7 8 9 X
4
SIZE = 6
13 X X 4 5 6 7 8 9 12
4
SIZE = 8
答案:若无答案欢迎评论