
测试代码均由AI生成,其余手写
2.2.1
#include <iostream>
using namespace std;
#define MAXSIZE 10
typedef struct Queue{
int data[MAXSIZE];
int size;
int head;
int tail;
}ArrayQueue;
void initQueue(ArrayQueue &a)
{
a.head = 0;
a.tail = 0;
a.size = 0;
}
//入队
bool enQueue(ArrayQueue &a, int e)
{
if(a.size < MAXSIZE)
{
a.data[a.tail] = e;
a.tail = (a.tail + 1) % MAXSIZE;
a.size++;
return true;
}
return false;
}
//出队
bool deQueue(ArrayQueue &a, int &e)
{
if(a.size > 0)
{
e = a.data[a.head];
a.head = (a.head + 1) % MAXSIZE;
a.size--;
return true;
}
return false;
}
//判空
bool isEmpty(ArrayQueue &a)
{
return a.size == 0;
}
//判满
bool isFull(ArrayQueue &a)
{
return a.size == MAXSIZE;
}
// 打印队列状态
void printQueue(ArrayQueue &a)
{
cout << "队列状态: ";
cout << "head=" << a.head << ", tail=" << a.tail << ", size=" << a.size << endl;
if (isEmpty(a)) {
cout << "队列为空" << endl;
return;
}
cout << "队列元素: ";
int current = a.head;
for (int i = 0; i < a.size; i++) {
cout << a.data[current] << " ";
current = (current + 1) % MAXSIZE;
}
cout << endl;
}
int main()
{
ArrayQueue a;
initQueue(a);
cout << "=== 循环队列测试 ===" << endl;
// 测试1: 基本入队出队
cout << "\n1. 测试基本操作:" << endl;
for(int i = 1; i <= 5; i++) {
if(enQueue(a, i * 10)) {
cout << "入队成功: " << i * 10 << endl;
} else {
cout << "入队失败: " << i * 10 << endl;
}
}
printQueue(a);
// 测试2: 出队操作
cout << "\n2. 测试出队操作:" << endl;
int value;
while(deQueue(a, value)) {
cout << "出队元素: " << value << endl;
}
printQueue(a);
// 测试3: 边界测试 - 填满队列
cout << "\n3. 测试队列满情况:" << endl;
for(int i = 1; i <= MAXSIZE + 2; i++) { // 故意多入队2个
if(enQueue(a, i)) {
cout << "入队成功: " << i << endl;
} else {
cout << "入队失败(队满): " << i << endl;
}
}
cout << "队列是否满: " << (isFull(a) ? "是" : "否") << endl;
printQueue(a);
// 测试4: 循环特性测试
cout << "\n4. 测试循环特性:" << endl;
// 先出队几个元素
for(int i = 0; i < 3; i++) {
if(deQueue(a, value)) {
cout << "出队: " << value << endl;
}
}
printQueue(a);
// 再入队新元素,测试是否循环到数组开头
for(int i = 100; i <= 103; i++) {
if(enQueue(a, i)) {
cout << "入队: " << i << endl;
}
}
printQueue(a);
// 测试5: 空队列出队
cout << "\n5. 测试空队列出队:" << endl;
// 清空队列
while(deQueue(a, value)) {
cout << "清空出队: " << value << endl;
}
// 尝试从空队列出队
if(!deQueue(a, value)) {
cout << "空队列出队失败(正确)" << endl;
}
cout << "队列是否空: " << (isEmpty(a) ? "是" : "否") << endl;
}
2.2.2
#include <iostream>
using namespace std;
#define MAXSIZE 10
typedef struct LinkNode{
int data;
struct LinkNode *next;
}LinkNode;
typedef struct {
LinkNode *front, *rear;
}LinkQueue;
void initQueue(LinkQueue &a)
{
a.front = a.rear = (LinkNode*)malloc(sizeof(LinkNode));
a.front->next = NULL;
}
//入队
void enQueue(LinkQueue &a, int e)
{
LinkNode *temp = (LinkNode*)malloc(sizeof(LinkNode));
temp->data = e;
temp->next = NULL;
a.rear->next = temp;
a.rear = temp;
}
//出队
bool deQueue(LinkQueue &a, int &e)
{
if(a.front == a.rear)
{
return false;
}
LinkNode *temp = a.front->next;
e = temp->data;
a.front->next = temp->next;
if(a.rear == temp) //代表仅有最后一个结点
{
a.rear = a.front;
}
free(temp);
return true;
}
//判空
bool isEmpty(LinkQueue &a)
{
return a.front == a.rear;
}
//判满
// bool isFull(ArrayQueue &a)
// {
// return a.size == MAXSIZE;
// }
int main()
{
LinkQueue a;
initQueue(a);
}

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



