首页 > 其他 > 详细

队列的链式存储结构

时间:2015-05-18 20:26:42      阅读:94      评论:0      收藏:0      [点我收藏+]
#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");
}

 

队列的链式存储结构

原文:http://www.cnblogs.com/louissong/p/4512928.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!