队列的链式结构

#include <bits/stdc++.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct QNode{
    ElemType data;
    struct QNode *next;
}QNode,*QListNode;
typedef struct QPlace{
    QListNode front;
    QListNode rear;
}QPlace;
Status InitQueue(QPlace &Q){
    Q.front=Q.rear=(QListNode)malloc(sizeof(QNode));
    if(!Q.front) return ERROR;
    Q.front->next=NULL;
    return OK;
}
Status EnQueue(QPlace &Q,ElemType e){
    QListNode p;
    p=(QListNode)malloc(sizeof(QNode));
    if(!p) return ERROR;
    p->data=e;
    Q.rear->next=p;
    Q.rear=p;
    p->next=NULL;
    return OK;
}
Status DeQueue(QPlace &Q,ElemType &e){
    QListNode p;
    p=Q.front->next;
    Q.front->next=p->next;
    if(p==Q.rear) Q.rear=Q.front; 
    e=p->data;
    free(p);
    return OK;
}
Status DestroyQueen(QPlace &Q){
    QListNode p;
    while(Q.front){
        p=Q.front->next; 
        free(Q.front);
        Q.front=p;
    }
    return OK;
}
void print(QPlace Q){
    Q.front=Q.front->next;
    while(Q.front){
        printf("%d ",Q.front->data);
        Q.front=Q.front->next;
    }
}
int main(){
    QPlace Q;
    ElemType e;
    InitQueue(Q);
    EnQueue(Q,1);
    EnQueue(Q,2);
    EnQueue(Q,3);
    EnQueue(Q,4);
    DeQueue(Q,e);
    print(Q);
}

 

上一篇:数据结构知识点-栈和队列


下一篇:刷题记录——剑指 Offer 21. 调整数组顺序使奇数位于偶数前面