隊列的鏈式存儲

定義:

隊列的鏈式存儲又叫做鏈隊列,實際上是一個同時帶有頭指針和隊尾指針的單鏈表,頭指針指向隊頭結點,尾指針指向隊尾結點

正好可以複習理解一下隊頭和隊尾指針O(∩_∩)O

鏈式存儲類型可以描述爲

typedef struct LQnode //C++測試刪除這個LQnode貌似有錯
{
    int data;
    struct LQnode *next;
}LQnode;
typedef struct
{
    LQnode *Front,*Rear; //定義頭尾指針
}LinkQueue;

初始化

void init_queue(LinkQueue &Q)
{
    Q.Front=Q.Rear=(LQnode*)malloc(sizeof(LQnode));
}

判空

bool isEmpty(LinkQueue Q)
{
    if(Q.Front==Q.Rear)return true;
    return false;
}

入隊

void Enqueue(LinkQueue &Q,int x)
{
    LQnode *s=(LQnode*)malloc(sizeof(LQnode));
    s->data=x;
    s->next=NULL;
    Q.Rear->next=s;
    Q.Rear=s;
}

出隊

bool Popqueue(LinkQueue &Q,int &x)
{
    if(Q.Front==Q.Rear) return false;//隊滿報錯
    LQnode *p=Q.Front->next;
    x=p->data;
    Q.Front->next=p->next;
    if(Q.Rear=p)
        Q.Rear=Q.Front;
    free(p);
    return true;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章