本文原创,转载请注明来自:http://blog.csdn.net/j903829182/article/details/38011661
#include<stdio.h>
#include<malloc.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
q->front=NULL;//定义初始队尾指针下标值
q->rear=NULL;//定义初始对头指针下标值
}
//非空否
int QueueNotEmpty(LQueue q){
//判断链式队列q非空否,非空则返回1,否则返回0
if(q.front==NULL){
return 0;
}else{
return 1;
}
}
//入队列
void QueueAppend(LQueue *q,DataType x){
//把数据元素值x插入链式队列q的队尾
LQNode *p;
p=(LQNode *)malloc(sizeof(LQNode));
p->data=x;
p->next=NULL;
if(q->rear!=NULL){//队列原来非空时队尾加新结点
q->rear->next=p;
}
q->rear=p;//修改队尾指针
if(q->front==NULL){//队列原来为空时修改对头指针
q->front=p;
}
}
//出队列
int QueueDelete(LQueue *q,DataType *d){
//删除链式队列q的对头数据元素值d,出队列成功返回1,否则返回0
LQNode *p;
if(q->front==NULL){
printf("队列已空无数据元素出队列!!\n");
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){
//取链式队列q的队头数据元素到d,成功返回1,否则返回0
if(q.front==NULL){
printf("队列已空无数据元素出队列!!\n");
return 0;
}else{
*d=q.front->data;
return 1;
}
}
//撤销动态申请空间
void Destroy(LQueue q){
LQNode *p,*p1;
p=q.front;
while(p!=NULL){
p1=p;
p=p->next;
free(p1);
}
}
void main(){}原文地址:http://blog.csdn.net/j903829182/article/details/38011661