王道数据结构应用题强化打卡2.2.1-2.2.4

测试代码均由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);

    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值