队列的链式存储结构

#include<stdlib.h>

#include <stdio.h>

typedef int QElemType;



typedef struct QNode

{

    QElemType data;

    struct QNode *next;

}QNode,*QueuePtr;



typedef struct

{

    QueuePtr front,rear;//队头,队尾指针

}LinkQueue;



void InitQueue(LinkQueue *Q)

{

    Q->front = (QueuePtr)malloc(sizeof(QNode));

    Q->rear = Q->front;

}



//入队操作

int Enqueue(LinkQueue *Q,QElemType e)

{

    QueuePtr s = (QueuePtr)malloc(sizeof(QNode));

    if(!s)

        exit(1);

    s->data = e;

    s->next = NULL;

    Q->rear->next = s;

    Q->rear = s;

    return 1;

}



//出队操作

int Dequeue(LinkQueue *Q,QElemType *e)

{

    QueuePtr p;

    //判断队列是否为空

    if(Q->front == Q->rear)

    {

        return 0;

    }



    p = Q->front->next;

    *e = p->data;

    Q->front->next = p->next;

    if(Q->rear == p)

        Q->rear = Q->front;

    free(p);

    return 1;

}



void main()

{

    LinkQueue lq;

    InitQueue(&lq);

    Enqueue(&lq,10);

    Enqueue(&lq,12);

    Enqueue(&lq,15);



    while((&lq)->front != (&lq)->rear)

    {

        QElemType elem;

        Dequeue(&lq,&elem);

        printf("%4d",elem);

    }

    system("pause");

}

 

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