队列--链式存储

/**
 * 队列--链式存储
 **/
#include <stdlib.h>
#include <iostream.h>

#define OK 1
#define ERROR 0

typedef struct node {         //链式队列的结点结构
    int item;                  //队列的数据元素类型
    struct node *next;        //指向后继结点的指针
}NODE;

typedef struct queue{      //链式队列
    NODE *front;           //队头指针
    NODE *rear;            //队尾指针
}QUEUE; 

//初始化队列Q  
void InitQueue(QUEUE *Q)
{
    Q->front=(NODE*)malloc(sizeof(NODE));
    if (Q->front==NULL) exit(0);
    Q->rear=Q->front;
}

//入队 
void EnQueue(QUEUE *Q,int item)
{
    NODE *s=(NODE*)malloc(sizeof(NODE));
    if (!s) exit(0);
    s->item=item;
    s->next=NULL;
    Q->rear->next=s;
    Q->rear=s;
}

//判断队列Q是否为空  
int QueueEmpty(QUEUE Q)
{
    if (Q.front==Q.rear) return 1;
    else return 0;
}

//获取队头元素内容  
void  GetFront(QUEUE Q,int *item) 
{
    if (QueueEmpty(Q)) exit(0);
    else *item=Q.front->next->item;
}

//出队 
void DeQueue(QUEUE *Q,int *item)
{
    if (QueueEmpty(*Q)) exit(0);
    else 
    {
        *item=Q->front->next->item;   
        NODE *s=Q->front->next;
        Q->front->next=s->next;
        free(s);
    }
}

 

posted @ 2013-02-23 17:58  何长春  阅读(175)  评论(0编辑  收藏  举报