数据结构 清华大学 队列 实现

本文通过一个C++程序演示了队列的基本操作,包括初始化、获取长度、插入元素、检查是否为空、删除元素以及获取队首元素等。每一步操作都通过用户输入选择,并给出相应的反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//main.cpp
#include <iostream>
using namespace std;
#include "Queue.h"
int main() {
    int n;
    ElemType e;
    SqQueue Q;
    while (1)
    {// 实现多次测试
        cout << "Please input a number to choose:";
        cin >> n;
        switch (n)
        {
            case 1:
                if(Init(Q))
                    cout<<"Init queue successfully!\n";
                else
                    cout<<"Init fail!\n";
                break;
            case 2: cout<<QueueLength(Q);
                break;
            case 3: cout<<"Please insert a element into the queue:";
                    cin >> e;
                    if(EnQueue(Q, e))
                        cout<<"Insert element successfully!\n";
                    else
                        cout<<"Insert fail!\n";
                break;
            case 4:
                if(QueueEmpty(Q))
                    cout<<"Empty!\n";
                else
                    cout<<"Not empty!\n";
                break;
            case 5: cout<<"Please input a element to delete:";
                    cin>>e;
                    if(DeQueue(Q, e))
                        cout<<"Delete element successfully!\n";
                    else
                        cout<<"Delete fail!\n";
                break;
            case 6: if(GetHead(Q,e))
                        cout<<"head element is:" << e << endl;
                    else
                        cout<<"Gethead fail!\n";
                break;
            case 7:
            case 0: exit(1);
            default: cout<<"ERROR, please input again!\n";
        }
    }//while
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值