数据结构-队列(Queue)

#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define true 1
#define false 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define OPSETSIZE 7
#define MAXQSIZE 100
typedef int Status;
typedef int ElemType;
typedef int QElemType;
typedef struct QNode
{
   QElemType data;
   struct QNode *next;
}QNode, *QueuePtr;

typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

Status InitQueue(LinkQueue *Q);
Status DestoryQueue(LinkQueue *Q);
Status Push(LinkQueue *Q, QElemType e);
Status Pop(LinkQueue *Q, QElemType *e);

int main()
{
    LinkQueue Q;
    QElemType e;
    InitQueue(&Q);
    Push(&Q, 1);
    Push(&Q, 2);
    Push(&Q, 3);
    Push(&Q, 4);
    while(Pop(&Q, &e))
    {
        printf("%d
", e);
    }
    DestoryQueue(&Q);
    return 0;
}


Status InitQueue(LinkQueue *Q)
{
    Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode));
    if(!Q->front)
        exit(OVERFLOW);
    Q->front->next = NULL;
    return OK;
}

Status DestoryQueue(LinkQueue *Q)
{
    while(Q->front)
    {
        Q->rear = Q->front->next;
        free(Q->front);
        Q->front = Q->rear;
    }
    return OK;
}

Status Push(LinkQueue *Q, QElemType e)
{
    QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
    if(!p)
        exit(OVERFLOW);
    p->data = e;
    Q->rear->next = p;
    p->next = NULL;
    Q->rear = p;
    return OK;
}

Status Pop(LinkQueue *Q, QElemType *e)
{
    if(Q->front == Q->rear)
        return ERROR;
    QueuePtr p = Q->front->next;
    *e = p->data;
    Q->front->next = p->next;
    if(Q->rear == p)
        Q->rear = Q->front;
    free(p);
    return OK;
}




原文地址:https://www.cnblogs.com/chinashenkai/p/9451404.html