C 算法與數據結構 隊列

隊列 線性數據結構的一種。數據結構

特色 先進先出spa

入隊的那一頭叫隊尾,出隊的那一頭叫隊首。指針

隊列裏的指針域老是指向下一個節點。
code

下面是隊列的鏈式存儲結構C代碼實現隊列

#include <stdio.h>#include <stdlib.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2typedef int QElemType;typedef int Status;/*
 * 存儲結構
 */typedef struct QNode
{
    QElemType data;    struct QNode *next;
}QNode, *QueuePtr;typedef struct{
    QueuePtr front;    //隊頭指針
    QueuePtr rear;    //隊尾指針}LinkQueue;/*
 * 初始化隊列
 */Status InitQueue(LinkQueue *Q){
    Q->front = Q->rear = (QueuePtr) malloc(sizeof(QNode));    if (!Q->front)
    {        exit(OVERFLOW);
    }
    Q->front->next = NULL;    return OK;
}/*
 * 銷燬隊列
 */Status DestroyQueue(LinkQueue *Q){    while (Q->front)
    {
        Q->rear = Q->front->next;        free(Q->front);
        Q->front = Q->rear;
    }    return OK;
}/*
 * 清空隊列
 */Status ClearQueue(LinkQueue *Q){
    DestroyQueue(Q);
    InitQueue(Q);
}/*
 * 判斷隊列是否爲空
 */Status IsEmpty(LinkQueue Q){    if (Q.front->next == NULL)
    {        return TRUE;
    }    else
    {        return FALSE;
    }
}/*
 * 獲取隊列的長度
 */int GetLength(LinkQueue Q){    int i = 0;
    QueuePtr p = Q.front;    while (Q.rear != p)
    {
        i++;
        p = p->next;
    }    return i;
}/*
 * 獲取隊頭元素
 */Status GetHead(LinkQueue Q, QElemType *e){
    QueuePtr p;    if (Q.front == Q.rear)
    {        return ERROR;
    }
    p = Q.front->next;
    *e = p->data;    return OK;
}/*
 * 入隊
 */Status EnQueue(LinkQueue *Q, QElemType e){
    QueuePtr p = (QueuePtr) malloc(sizeof(QNode));    if (!p)
    {        exit(OVERFLOW);
    }
    p->data = e;
    p->next = NULL;
    Q->rear->next = p;
    Q->rear = p;    return OK;
}/*
 * 出隊
 */Status DeQueue(LinkQueue *Q, QElemType *e){
    QueuePtr p;    if (Q->front == Q->rear)
    {        return ERROR;
    }
    p = Q->front->next;
    *e = p->data;
    Q->front->next = p->next;    if (Q->rear == p)
    {
        Q->rear = Q->front;
    }    free(p);    return OK;
}/*
 * 訪問元素
 */void visit(QElemType e){    printf("%d ", e);
}/*
 * 遍歷隊列
 */Status TraverseQueue(LinkQueue Q, void (*visit)(QElemType)){
    QueuePtr p = Q.front->next;    while (p)
    {
        visit(p->data);
        p = p->next;
    }    return OK;
}int main(){
    LinkQueue Q;    if (InitQueue(&Q))
    {
        QElemType e;        int i;        printf("init_success\n");        if (IsEmpty(Q))
        {            printf("queue is empty\n");
        }        for (i = 0; i < 10; i++)
        {
            EnQueue(&Q, i);
        }

        GetHead(Q, &e);        printf("The first element is %d\n", e);        printf("length is %d\n", GetLength(Q));

        DeQueue(&Q, &e);        printf("delete element is %d\n", e);

        TraverseQueue(Q, *visit);        if (DestroyQueue(&Q))
        {            printf("\ndestroy_success\n");
        }
    }
}

隊列的順序存儲結構能夠實現循環隊列。(此處記錄下之後會有代碼實現)
element

下面是關於棧和隊列的使用練習
it

一、把一個十進制的數轉換爲一個二進制的數,例如9轉換爲二進制是1001,可使用棧來實現。io

二、用兩個棧來實現一個隊列,完成隊列的Push和Pop操做。 隊列中的元素爲int類型。class

相關文章
相關標籤/搜索