鏈?zhǔn)疥?duì)列的兩種實(shí)現(xiàn)方式(C語言)

實(shí)現(xiàn)方式一

1辣往、頭文件LinkQueue.h

#include <stdio.h>
typedef struct Node
{
    struct Node* next;
}Node;

typedef struct LinkQueue
{
    Node* front;
    Node* rear;
    int length;
}LinkQueue;

void InitQueue(LinkQueue*);
int QueueEmpty(LinkQueue*);
int QueueLength(LinkQueue*);
void GetFront(LinkQueue*, Node**);
void EnQueue(LinkQueue*, Node*);
void DeQueue(LinkQueue*, Node**);
void ClearQueue(LinkQueue*);

2鲫骗、相關(guān)操作函數(shù)文件LinkQueue.c

#include <stdio.h>
#include "LinkQueue.h"

void InitQueue(LinkQueue* lq)
{
    lq->front = NULL;
    lq->rear = NULL;
    lq->length = 0;
}

int QueueEmpty(LinkQueue* lq)
{
    if(lq->length == 0)
        return 1;

    return 0;
}

int QueueLength(LinkQueue* lq)
{
    return lq->length;
}

void GetFront(LinkQueue* lq, Node** e)
{
    if(lq->length == 0)
    {
        printf("空隊(duì)列织阳!\n");
        return;
    }

    *e = lq->front;
}

void EnQueue(LinkQueue* lq, Node* e)
{
    if(lq->length == 0)
    {
        lq->front = lq->rear = e;
    }
    else
    {
        lq->rear->next = e;
        lq->rear = lq->rear->next;
    }

    lq->length++;
}

void DeQueue(LinkQueue* lq, Node** e)
{
    if(lq->length == 0)
    {
        printf("空隊(duì)列!\n");
        return;
    }

    *e = lq->front;
    lq->front = lq->front->next;
    lq->length--;
    if(lq->length == 0)
    {
        lq->rear = NULL;
    }
}

void ClearQueue(LinkQueue* lq)
{
    while(lq->length)
    {
        Node* tmp;
        DeQueue(lq, (Node**)&tmp);
    }
}

3填帽、主函數(shù)測(cè)試文件main.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"

typedef struct stu
{
    Node p;
    int id;
    int age;
}student;

int main()
{
    LinkQueue lq;
    InitQueue(&lq);
    student s[10];
    int i = 0;
    for(i = 0; i < 10; i++)
    {
        s[i].id = i;
        s[i].age = i + 20;
        EnQueue(&lq, &s[i].p);
    }

    printf("當(dāng)前隊(duì)列長(zhǎng)度為:%d\n", QueueLength(&lq));

    while(!QueueEmpty(&lq))
    {
        Node* tmp;
        GetFront(&lq, (Node**)&tmp);
        student* tmp1 = (student*)tmp;
        printf("當(dāng)前隊(duì)頭元素值為:id = %d, age = %d\n", tmp1->id, tmp1->age);
        DeQueue(&lq, (Node**)&tmp);
        tmp1 = (student*)tmp;
        printf("當(dāng)前出隊(duì)元素值為:id = %d, age = %d\n", tmp1->id, tmp1->age);
        printf("\n");

    }
    return 0;
}

實(shí)現(xiàn)方式二

1智厌、頭文件LinkQueue.h

#include <stdio.h>

typedef struct Node
{
    struct Node* next;
}Node;

typedef struct QueueNode
{
    Node P;
    void* data;
}QueueNode;

typedef struct LinkQueue
{
    int length;
    QueueNode* front;
    QueueNode* rear;
}LinkQueue;

void InitQueue(LinkQueue*);
int QueueEmpty(LinkQueue*);
int QueueLength(LinkQueue*);
void GetFront(LinkQueue*, void**);
void EnQueue(LinkQueue*, void*);
void DeQueue(LinkQueue*, void**);
void ClearQueue(LinkQueue*);

2、相關(guān)操作函數(shù)main.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"

void InitQueue(LinkQueue* lq)
{
    lq->length = 0;
    lq->front = NULL;
    lq->rear = NULL;
}

int QueueEmpty(LinkQueue* lq)
{
    if(lq->length == 0)
        return 1;
    return 0;
}

int QueueLength(LinkQueue* lq)
{
    return lq->length;
}

void GetFront(LinkQueue* lq, void** e)
{
    if(lq->length == 0)
    {
        printf("空隊(duì)列盲赊!\n");
        return;
    }

    *e = lq->front->data;
}

void EnQueue(LinkQueue* lq, void* e)
{
    QueueNode* pNew = (QueueNode*)malloc(sizeof(QueueNode));
    pNew->data = e;

    if(lq->length == 0)
    {
        lq->front = lq->rear = pNew;
    }
    else
    {
        lq->rear->P.next = &pNew->P;
        lq->rear = pNew;
    }
    lq->length++;
}

void DeQueue(LinkQueue* lq, void** e)
{
    if(lq->length == 0)
    {
        printf("空隊(duì)列铣鹏!\n");
        return;
    }

    QueueNode* pDel;
    pDel = lq->front;
    *e = pDel->data;
    lq->front = (QueueNode*)pDel->P.next;
    free(pDel);

    lq->length--;
    if(lq->length == 0)
    {
        lq->rear = NULL;
    }
}
void ClearQueue(LinkQueue* lq)
{
    while(lq->length)
    {
        void* tmp;
        DeQueue(lq, (void**)&tmp);
    }
}

3、主函數(shù)main.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"

typedef struct stu
{
    int id;
    int age;
}student;

int main()
{
    LinkQueue lq;
    InitQueue(&lq);
    student s[10];

    int i = 0;
    for(i = 0; i < 10; i++)
    {
        s[i].id = i;
        s[i].age = i + 20;
        EnQueue(&lq, &s[i]);
    }

    printf("當(dāng)前隊(duì)列的長(zhǎng)度為:%d\n", QueueLength(&lq));

    while(!QueueEmpty(&lq))
    {
        student* tmp;
        GetFront(&lq, (void**)&tmp);
        printf("當(dāng)前隊(duì)頭元素值為:id = %d, age = %d\n", tmp->id, tmp->age);

        DeQueue(&lq, (void**)&tmp);
        printf("當(dāng)前出隊(duì)元素值為:id = %d, age = %d\n", tmp->id, tmp->age);
        printf("\n");
    }
    printf("當(dāng)前隊(duì)列的長(zhǎng)度為:%d\n", QueueLength(&lq));

    EnQueue(&lq, &s[0]);
    ClearQueue(&lq);
    printf("當(dāng)前隊(duì)列的長(zhǎng)度為:%d\n", QueueLength(&lq));

    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末哀蘑,一起剝皮案震驚了整個(gè)濱河市诚卸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌绘迁,老刑警劉巖合溺,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異缀台,居然都是意外死亡棠赛,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來睛约,“玉大人鼎俘,你說我怎么就攤上這事”缋裕” “怎么了贸伐?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)怔揩。 經(jīng)常有香客問我捉邢,道長(zhǎng),這世上最難降的妖魔是什么商膊? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任伏伐,我火速辦了婚禮,結(jié)果婚禮上晕拆,老公的妹妹穿的比我還像新娘藐翎。我一直安慰自己,他們只是感情好潦匈,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布阱高。 她就那樣靜靜地躺著,像睡著了一般茬缩。 火紅的嫁衣襯著肌膚如雪赤惊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天凰锡,我揣著相機(jī)與錄音未舟,去河邊找鬼。 笑死掂为,一個(gè)胖子當(dāng)著我的面吹牛裕膀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播勇哗,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼昼扛,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了欲诺?” 一聲冷哼從身側(cè)響起抄谐,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎扰法,沒想到半個(gè)月后蛹含,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡塞颁,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年浦箱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吸耿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡酷窥,死狀恐怖咽安,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情竖幔,我是刑警寧澤板乙,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布是偷,位于F島的核電站拳氢,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蛋铆。R本人自食惡果不足惜馋评,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望刺啦。 院中可真熱鬧留特,春花似錦、人聲如沸玛瘸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽糊渊。三九已至右核,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間渺绒,已是汗流浹背贺喝。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宗兼,地道東北人躏鱼。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像殷绍,于是被迫代替她去往敵國(guó)和親染苛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 一主到、溫故而知新 1. 內(nèi)存不夠怎么辦 內(nèi)存簡(jiǎn)單分配策略的問題地址空間不隔離內(nèi)存使用效率低程序運(yùn)行的地址不確定 關(guān)于...
    SeanCST閱讀 7,813評(píng)論 0 27
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,103評(píng)論 1 32
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 2,698評(píng)論 0 3
  • 在C語言中,五種基本數(shù)據(jù)類型存儲(chǔ)空間長(zhǎng)度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 3,345評(píng)論 0 2
  • Lua 5.1 參考手冊(cè) by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 13,798評(píng)論 0 38