posts - 137,comments - 0,views - 40818
复制代码
#include <iostream>
#include <Windows.h>
using namespace std;
typedef struct _QNode{
    int data;
    struct _QNode* next;
}QNode;
typedef struct {
    QNode* front;
    QNode* rear;
    int count;
}LinkQueue;
//初始化队列
void InitQueue(LinkQueue& Q) {
    Q.rear = Q.front = new QNode;
    Q.front->next = NULL;
    Q.count = 0;
}
//入队
bool EntQueue(LinkQueue& Q,int e) {
    QNode* q = new QNode;
    if (!q)return false;
    q->data = e;
    q->next = NULL;
    Q.rear->next = q;
    Q.rear = q;
    Q.count++;
    return true;
}
//出队
bool DeleteQueue(LinkQueue& Q,int &e) {
    if (Q.front == Q.rear) {
        cout << "队列为空,无法出队" << endl;
        return false;
    }
    QNode* q = Q.front->next;
    e = q->data;
    Q.front->next = q->next;
    if (Q.rear == q) {
        Q.rear = Q.front;
    }
    delete q;
    Q.count--;
    q = NULL;
    return true;
}
//获取头结点
int getHead(LinkQueue& Q) {
    if (Q.front == Q.rear) {
        cout << "队列为空,无法获取头结点" << endl;
        exit(-1);
    }
    return Q.front->next->data;
}
//获取尾结点
int getBack(LinkQueue& Q) {
    if (Q.front == Q.rear) {
        cout << "队列为空,无法获取尾结点" << endl;
        exit(-1);
    }
    return Q.rear->data;
}
//获取队列长度
int getLength(LinkQueue& Q) {
    return Q.count;
}
//打印队列
void PrintQueue(LinkQueue& Q) {
    QNode* q = Q.front->next;
    if (!q) return;
    cout << "队列的元素为:" ;
    while (q) {
        cout <<  q->data << " ";
        q = q->next;
    }
    cout << endl;
}
void showMenu() {
    cout << "******************************\n";
    cout << "    欢迎来到链队列系统\n";
    cout << "------1.入队--------------\n";
    cout << "------2.出队--------------\n";
    cout << "------3.获取头结点--------\n";
    cout << "------4.获取尾结点--------\n";
    cout << "------5.获取队列长度------\n";
    cout << "------6.打印队列----------\n";
    cout << "------7.退出--------------\n" << endl;
}
void Destroy(LinkQueue& Q){
    if (Q.front) {
        delete Q.front;
        Q.front = NULL;
    }
    if (Q.rear) {
        delete Q.rear;
        Q.rear = NULL;
    }
    Q.count = 0;
}
int main() {
    LinkQueue Q;
    InitQueue(Q);
    bool flag = -1;
    int e1 = 0;//要入队的元素
    int e2 = 0;
    int n = -1;
    while (flag) {
        showMenu();
        cout << "请选择:";
        cin >> n;
        switch (n)
        {
        case 1:
            cout << "请输入要入队的元素:";
            cin >> e1;
            if (EntQueue(Q, e1)) {
                cout << "元素 " << e1 << " 入队成功!" << endl;
                break;
                PrintQueue(Q);
            }
            else {
                cout << "元素 " << e1 << " 入队失败!" << endl;
                break;
            }
        case 2:
            if (DeleteQueue(Q, e2)) {
                cout << "元素 " << e2 << " 出队成功!" << endl;
                break;
            }
            else {
                cout << "元素 " << e2 << " 出队失败!" << endl;
                break;
            }
        case 3:
            cout << "头结点为:" << getHead(Q) << endl;
            break;
        case 4:
            cout << "尾结点为:" << getBack(Q) << endl;
            break;
        case 5:
            cout << "队列的长度为:" << getLength(Q) << endl;
            break;
        case 6:
            PrintQueue(Q);
            break;
        case 7:
        default:
            flag = 0;
            Destroy(Q);
            cout << "退出系统!\n";
        }
    }
    
    system("pause");
    return 0;
}
复制代码

posted on   wshidaboss  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示