队列

#include "stdafx.h"
#include "malloc.h"
#include "stdlib.h"

typedef int DataType;

typedef struct qnode
{
DataType data;
struct qnode *next;
}LQNode;
typedef struct
{
LQNode *front;
LQNode *rear;
}LQueue;
//初始化
void QueueInitiate(LQueue *Q)
{
Q->rear=NULL;
Q->front=NULL;
}
//非空否
int QueueNotEmpty(LQueue Q)
{
if(Q.front==NULL)
return 0;
else
return 1;
}
//入队列
int QueueAppend(LQueue *Q,DataType x)
{
LQNode *p;
if((p=(LQNode *)malloc(sizeof(LQNode)))==NULL)
{
printf("内存空间不足");
return 0;
}
p->data=x;
p->next=NULL;
if(Q->rear!=NULL)
Q->rear->next=p;
Q->rear=p;
if(Q->front==NULL)
Q->front=p;
return 1;
}
//出队列
int QueueDelete(LQueue *Q,DataType *d)
{
LQNode *p;
if(Q->front==NULL)
{
printf("队列已经空,无数据出队列!");
return 0;
}
else
{
*d=Q->front->data;
p=Q->front;
Q->front=Q->front->next;
if(Q->front==NULL)
Q->rear=NULL;
free(p);
return 1;
}
}
//取对头元素
int QueueGet(LQueue Q,DataType *d)
{
if(Q.front==NULL)
{
printf("队列已空,无数据元素出队列");
return 0;
}
else
{
*d=Q.front->data;
return 1;
}
}
//撤销动态申请的空间
void Destory(LQueue Q)
{
LQNode *p,*p1;
p=Q.front;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
}
int main(int argc, char* argv[])
{
 printf("Hello World!\n");
 return 0;
}

 

posted on 2008-06-27 22:43  小顾问  阅读(249)  评论(0)    收藏  举报