队列的链式存储(纯代码)

队列的链式存储(纯代码)

//
// Created by Changepeng on 2019/6/18.
//

#include
#include
#define stackSize 100
typedef int states;
typedef int datatype;
#define ERROR 0
#define FALSE 0
#define OK 1
#define TRUE 1
typedef struct Node{
     
    datatype data;
    struct Node *next;
}Node,*NodeList;
typedef struct QNode{
     
    NodeList head;
    NodeList footer;
}QNode,*QNodeList;
//初始化
states initList(QNodeList q){
     
    q->head = q->footer = (NodeList)malloc(sizeof(Node));
    if(q->footer==NULL){
     
        return ERROR;
    }
    q->footer->next = NULL;
    return OK;
}
//判空
states QNodeEmpty(QNodeList q){
     
    if(q->head == q->footer){
     
        return TRUE;
    }else{
     
        return FALSE;
    }
}
//入队
states EnterQNode(QNodeList q,datatype x){
     
    NodeList p = (NodeList)malloc(sizeof(Node));
    if(p!=NULL){
     
        p->data = x;
        p->next = NULL;
        q->head->next = p;
        q->head = p;
        return OK;
    }else{
     
        return ERROR;
    }
}
//出队
states DelQnode(QNodeList q){
     
    NodeList p;
    if(QNodeEmpty(q)==TRUE){
     
        return ERROR;
    }
    p = q->footer;
    q->footer = q->footer->next;
    free(p);
    return OK;
}
//循环遍历
void Out(QNode q){
     
    while(q.footer!=q.head){
     
        q.footer = q.footer->next;
        printf("%d\t",q.footer->data);
    }
}
int main(){
     
    QNode q;
    initList(&q);
    EnterQNode(&q,12);
    EnterQNode(&q,14);
    EnterQNode(&q,16);
    EnterQNode(&q,18);
    EnterQNode(&q,20);
    EnterQNode(&q,22);
    Out(q);
    DelQnode(&q);
    printf("\n");
    Out(q);
    DelQnode(&q);
    printf("\n");
    Out(q);
    DelQnode(&q);
    printf("\n");
    Out(q);
}

你可能感兴趣的:(队列)