隊列(c語言實現)

來源:互聯網
上載者:User

標籤:als   oid   初始化   iostream   lib   count   false   bool   res   

#include <iostream>#include <stdlib.h>using namespace std;/*********************************    鏈隊列的結構實現********************************/typedef int QElemType;typedef struct QNode{    QElemType data;    struct QNode *next;}QNode, *QNodePtr;typedef struct{    QNodePtr front;    QNodePtr rear;}LinkQueue;/*********************************    鏈隊列的操作實現********************************//*初始化一個鏈隊列*/bool InitQueue(LinkQueue *lq){    cout << "Init LinkQueue ..." << endl;        QNodePtr p = (QNodePtr)malloc(sizeof(QNode));    p->next = NULL;    lq->front = lq->rear = p;    return true;}/*插入元素e為Q的新的隊尾元素*/ bool EnQueue(LinkQueue *lq, QElemType e){    QNodePtr s = (QNodePtr)malloc(sizeof(QNode));    if(!s)    {        exit(0);    }    s->data = e;    s->next = NULL;    lq->rear->next = s;    lq->rear = s;    return true;}/*刪除隊列的隊頭元素,用e返回其值*/bool DeQueue(LinkQueue *lq, QElemType *e){    QNodePtr p = NULL;    if(lq->front == lq->rear)    {        return false;    }    p = lq->front->next;    *e = p->data;    lq->front->next = p->next;    if(p == lq->rear)/*若隊頭同時又是隊尾,則刪除後將rear指向頭結點*/    {        lq->rear = lq->front;    }    cout << "DeQueue Item: " << *e << endl;    free(p);    return true;}/*銷毀隊列,包括頭結點*/bool DestroyQueue(LinkQueue *lq){    cout << "Destroy Queue... " << endl;    while(lq->front)    {        lq->rear = lq->front->next;        free(lq->front);        lq->front = lq->rear;    }    return true;}/*將隊列清空,但是保留頭結點*/bool ClearQueue(LinkQueue *lq){    if(lq->front == lq->rear)    {        return true;    }    cout << "Clear Queue ..." << endl;    QNodePtr p = lq->front->next;    QNodePtr q = NULL;    lq->front->next = NULL;    lq->rear = lq->front;    while(p)    {        q = p->next;        free(p);        p = q;    }    return true;}/*判斷隊列是否為空白*/bool IsEmptyQueue(LinkQueue lq){    return lq.front == lq.rear;}/*返回隊列中結點的個數*/int QueueLength(LinkQueue lq){    int count = 0;    if(lq.front == NULL)//隊列沒有初始化    {        return 0;    }    QNodePtr p = lq.front->next;    while(p)    {        p = p->next;        count++;    }    return count;}/*返回隊列的隊頭元素*/bool GetTop(LinkQueue *lq, QElemType *e){    QNodePtr p;    if(lq->front == lq->rear)    {        return false;    }    p = lq->front->next;    *e = p->data;    cout << "Get Top Item: " << *e << endl;    return true;}/*遍曆隊列中的各個元素*/bool QueueTraverse(LinkQueue lq){    if(lq.front == lq.rear)    {        return false;    }    cout << "Queue Traverse ..." << endl;    QNodePtr p = lq.front->next;    while(p)    {        cout << p->data <<‘ ‘;        p = p->next;    }    cout << endl;    return true;}void main(void){    LinkQueue lq;    InitQueue(&lq);    for(int i = 0; i < 5; i++)    {        EnQueue(&lq, i);    }    QueueTraverse(lq);    int result;    GetTop(&lq, &result);    DeQueue(&lq, &result);    if(!IsEmptyQueue(lq))    {        cout << "Queue Length: " << QueueLength(lq) << endl;    }    QueueTraverse(lq);    DestroyQueue(&lq);    return 0;}    

 

隊列(c語言實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.