关闭

数据结构-队列(Queue)

89人阅读 评论(0) 收藏 举报
分类:
#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\n", 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;
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8953次
    • 积分:428
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:3篇
    • 译文:0篇
    • 评论:2条
    文章分类