數(shù)據(jù)結(jié)構(gòu)【動(dòng)態(tài)隊(duì)列】代碼實(shí)現(xiàn)

隊(duì)列是使用鏈表實(shí)現(xiàn),包含隊(duì)列的初始化、入隊(duì)、出隊(duì)、輸出隊(duì)列內(nèi)容、判斷隊(duì)列內(nèi)容是否為空

#include <iostream>

//鏈表節(jié)點(diǎn)
typedef struct Node
{
    int nData;
    Node *pNext;
}*PNODE;

//隊(duì)列
//隊(duì)列有兩個(gè)指針記錄數(shù)據(jù),front、rear
typedef struct Queue
{
    PNODE pFront;
    PNODE pRear;
}*PQUEUE;

//初始化隊(duì)列
void init_queue(PQUEUE pQueue);
void push_queue(PQUEUE pQueue, const int& nValue);
void pop_queue(PQUEUE pQueue);
void show_queue(const PQUEUE pQueue);
bool isEmpty_queue(const PQUEUE pQueue);


int main()
{
    Queue oQueue;
    init_queue(&oQueue);
    push_queue(&oQueue, 1);
    push_queue(&oQueue, 2);
    push_queue(&oQueue, 3);
    push_queue(&oQueue, 4);
    push_queue(&oQueue, 40);
    push_queue(&oQueue, 88);
    push_queue(&oQueue, 99);
    show_queue(&oQueue);

    pop_queue(&oQueue);
    pop_queue(&oQueue);
    pop_queue(&oQueue);
}

//初始化隊(duì)列
void init_queue(PQUEUE pQueue)
{
    //初始化的時(shí)候,申請(qǐng)一個(gè)無(wú)效節(jié)點(diǎn)(不存儲(chǔ)任何內(nèi)容)
    //讓隊(duì)首、隊(duì)尾都指向這個(gè)無(wú)效節(jié)點(diǎn)
    pQueue->pRear = (PNODE)malloc(sizeof(Node));
    if (nullptr == pQueue->pRear)
    {
        std::cout << "申請(qǐng)內(nèi)存失敗" << std::endl;
        exit(-1);
    }
    pQueue->pRear->pNext = nullptr;

    //隊(duì)首指向隊(duì)尾
    pQueue->pFront = pQueue->pRear;
}

//入隊(duì)
void push_queue(PQUEUE pQueue, const int& nValue)
{
    //插入需要從隊(duì)尾插入
    PNODE pNewNode = (PNODE)malloc(sizeof(Node));
    if (nullptr == pQueue->pRear)
    {
        std::cout << "申請(qǐng)內(nèi)存失敗" << std::endl;
        exit(-1);
    }
    pNewNode->pNext = nullptr;

    //由于隊(duì)尾永遠(yuǎn)指向一個(gè)無(wú)效的節(jié)點(diǎn),所以這里直接把數(shù)據(jù)存儲(chǔ)到隊(duì)尾節(jié)點(diǎn)即可
    pQueue->pRear->nData = nValue;
    //然后把申請(qǐng)好的無(wú)效節(jié)點(diǎn)與隊(duì)尾連接起來(lái),避免丟失節(jié)點(diǎn)
    pQueue->pRear->pNext = pNewNode;
    //讓隊(duì)尾指向新加的無(wú)效節(jié)點(diǎn),就等于rear + 1
    pQueue->pRear = pNewNode;
}

//出隊(duì)
void pop_queue(PQUEUE pQueue)
{
    if (isEmpty_queue(pQueue))
    {
        return;
    }

    //出隊(duì)從隊(duì)首彈出
    //先記錄隊(duì)首的節(jié)點(diǎn)
    PNODE pTempNode = pQueue->pFront;
    //隊(duì)首指向隊(duì)首的下一個(gè)節(jié)點(diǎn),相當(dāng)于front + 1
    pQueue->pFront = pQueue->pFront->pNext;

    std::cout << "彈出的元素為:" << pTempNode->nData << std::endl;

    //釋放內(nèi)存
    free(pTempNode);
}

//輸出隊(duì)列內(nèi)容
void show_queue(const PQUEUE pQueue)
{
    PNODE pNode = pQueue->pFront;
    while (pNode != pQueue->pRear)
    {
        std::cout << pNode->nData << "\t";
        pNode = pNode->pNext;
    }

    //輸出完以后換個(gè)行
    std::cout << "\n";
}

//隊(duì)列是否為空
bool isEmpty_queue(const PQUEUE pQueue)
{
    if (pQueue->pFront == pQueue->pRear)
    {
        return true;
    }

    return false;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容