資料結構C語言版-隊列

來源:互聯網
上載者:User

標籤:overflow   exit   names   return   eth   eof   malloc   struct   get   

#include <stdlib.h>#include <stdio.h>#include <iostream>using namespace std;typedef int QElemType;typedef struct QNode {    QElemType data;    struct QNode *next;} QNode, *QueuePtr;typedef struct {    QueuePtr front;    QueuePtr rear;} LinkQueue;bool InitQueue(LinkQueue &Q);bool DestoryQueue(LinkQueue &Q);bool ClearQueue(LinkQueue &Q);bool QueueEmpty(LinkQueue Q);int QueueLength(LinkQueue Q);int GetHead(LinkQueue Q);bool EnQueue(LinkQueue &Q, QElemType e);bool DeQueue(LinkQueue &Q, QElemType &e);void QueueTraverse(LinkQueue Q);//Q.front裡面是沒有資料的//Q.rear裡面是有資料的int main() {    LinkQueue s;    InitQueue(s);    EnQueue(s, 5);    EnQueue(s, 6);    EnQueue(s, 7);    ClearQueue(s);    EnQueue(s, 8);    EnQueue(s, 100);    cout << QueueLength(s) << endl;    int a;    DeQueue(s, a);    cout << a << endl;    if (QueueEmpty(s)) {        cout << ‘a‘ << endl;    }    cout << GetHead(s) << endl;}void QueueTraverse(LinkQueue Q) {    if (Q.front == Q.rear) {        return;    }    QueuePtr tmp = Q.front->next;    while (tmp != Q.rear) {        printf("%d ", tmp->data);        tmp = tmp->next;    }    printf("%d\n", tmp->data);}int GetHead(LinkQueue Q) {    return Q.front->next->data;}bool QueueEmpty(LinkQueue Q) {    if (Q.front == Q.rear) {        return true;    }    return false;}int QueueLength(LinkQueue Q) {    int length = 0;    LinkQueue tmp = Q;    while (tmp.front) {        length++;        tmp.front = tmp.front->next;    }    return length - 1;}bool InitQueue(LinkQueue &Q) {    Q.front = Q.rear = (QNode *) malloc(sizeof(QNode));    if (!Q.front) {        exit(EOVERFLOW);    }    Q.front->next = NULL;    return true;}bool ClearQueue(LinkQueue &Q) {    QueuePtr tmp = Q.front->next;    while (tmp) {        QueuePtr tp = tmp;        tmp = tmp->next;        free(tp);    }    Q.rear = Q.front;    Q.front->next = NULL;}bool DestoryQueue(LinkQueue &Q) {    while (Q.front) {        Q.rear = Q.front->next;        free(Q.front);        Q.front = Q.rear;    }    return true;}bool EnQueue(LinkQueue &Q, QElemType e) {    QueuePtr p = (QueuePtr) malloc(sizeof(QNode));    if (!p) {        exit(EOVERFLOW);    }    p->data = e;    p->next = NULL;    Q.rear->next = p;    Q.rear = p;    return true;}bool DeQueue(LinkQueue &Q, QElemType &e) {    if (Q.front == Q.rear) {        return false;    }    QueuePtr p = Q.front->next;    e = p->data;    Q.front->next = p->next;    if (Q.rear == p) {        Q.rear = Q.front;    }    free(p);    return true;}

 

資料結構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.