#include <iostream>
#include <Windows.h>
using namespace std;
#define MAXSIZE 6
typedef int QElemType;
typedef struct {
    QElemType* base;//基地址
    int rear;//尾指针
    int front;//头指针
}LoopQueue;
//初始化循环队列
bool InitList(LoopQueue &queue) {
    queue.base = new int[MAXSIZE];
    if (!queue.base)return false;
    queue.front = queue.rear = 0;
    return true;
}
//入队
bool InsertQueue(LoopQueue& queue, QElemType e) {
    if ((queue.rear + 1) % MAXSIZE == queue.front) {
        cout << "队列已满,无法入队!" << endl;
        return false;
    }
    queue.base[queue.rear] = e;
    queue.rear = (queue.rear + 1) % MAXSIZE;
    return true;
}
//打印队列
bool PrintQueue(LoopQueue& queue) {
    if (queue.front == queue.rear) {
        cout << "队列为空,无法打印队列!" << endl;
        return false;
    }
    int i = queue.front;
    while (queue.front != queue.rear && (i + MAXSIZE) % MAXSIZE != queue.rear)
    {
        cout << queue.base[i] << " ";
        i++;
    }
    cout << endl;
    return true;
}
//获取头结点
int getHead(LoopQueue& queue) {
    return queue.base[queue.front];
}
//获取尾结点
int getBack(LoopQueue& queue) {
    return queue.base[queue.rear-1];
}
//获取队列的长度
int getLength(LoopQueue& queue) {
    return (queue.rear - queue.front + MAXSIZE) % MAXSIZE;
}
//出队
bool DeleteQueue(LoopQueue& queue, QElemType&e) {
    if (queue.front == queue.rear) {
        cout << "队列为空,无法出队!" << endl;
        return false;
    }
    e = queue.base[queue.front];
    queue.front = (queue.front + 1) % MAXSIZE;
    return true;
}
//销毁队列
void DestroyQueue(LoopQueue& queue) {
    if (queue.base) {
        delete queue.base;
    }
    queue.base = NULL;
    queue.front = queue.rear = 0;
}
//清空队列
void ClearQueue(LoopQueue& queue) {
    queue.front = queue.rear = 0;
}
void show_menu() {
    cout << "----------------循环队列的基本操作--------------\t" << endl;
    cout << "\t\t1.初始化循环队列" << endl;
    cout << "\t\t2.入队" << endl;
    cout << "\t\t3.获取队列的头结点" << endl;
    cout << "\t\t4.获取队列的尾结点" << endl;
    cout << "\t\t5.获取队列的长度" << endl;
    cout << "\t\t6.出队" << endl;
    cout << "\t\t7.打印队列" << endl;
    cout << "\t\t8.清空队列" << endl;
    cout << "\t\t0.退出系统" << endl;
}
int main() {
    LoopQueue queue;
    int  choose = -1;
    bool flag = -1;
    int e = 0;
    int elem = 0;
    show_menu();
    while (flag) {
        cout << "请选择:";
        cin >> choose;
        switch (choose)
        {
        case 1:
            if (InitList(queue)) {
                cout << "队列初始化成功!" << endl;
                break;
            }
            else {
                cout << "队列初始化失败!" << endl;
                break;
            }
        case 2:
            cout << "请输入要插入的元素:";
            cin >> e;
            if (InsertQueue(queue, e)) {
                cout << "元素 " << e << " 插入成功!" << endl;
                break;
            }
            else {
                cout << "元素 " << e << " 插入失败!" << endl;
                break;
            }
        case 3:
            cout << "队列的头结点为:" << getHead(queue) << endl;
            break;
        case 4:
            cout << "队列的尾结点为:" << getBack(queue) << endl;
            break;
        case 5:
            cout << "队列的长度为:" << getLength(queue) << endl;
            break;
        case 6:
            if (DeleteQueue(queue, elem)) {
                cout << "元素 " << elem << " 出队成功!" << endl;
                break;
            }
            else {
                cout << "元素出队失败!" << endl;
                break;
            }
        case 7:
            PrintQueue(queue);//打印队列
            break;
        case 8:
            ClearQueue(queue);
            cout << "清空队列元素!" << endl;
            break;
        default:
            flag = 0;
        }
    }
    DestroyQueue(queue);//销毁队列
    system("pause");
    return 0;
}

posted on 2023-02-03 23:19  wshidaboss  阅读(47)  评论(0编辑  收藏  举报