第一种:
typedef struct Qnode//队列结点定义
{
int data;
Qnode * next;
}Qnode;
typedef struct Lq//链队类型定义
{
Qnode * front;
Qnode * rear;
}Lq;
bool is_empty(Lq *qu)//判断是否队空
{
if(qu->front==NULL||qu->rear==NULL)
{
return true;
}
else
return false;
}
void init(Lq * &qu)//初始化
{
qu=(Lq *)malloc(sizeof(Lq));
(*qu).front=(*qu).rear=NULL;
printf("初始化成功!\n");
}
void in_qu(Lq * qu,int a)//入队
{
Qnode s;
s=(Qnode)malloc(sizeof(Qnode));
s->next=NULL;
s->data=a;
if(is_empty(qu))
{
qu->front=qu->rear=s;
}
else
{
qu->rear->next=s;
qu->rear=s;
}
return;
}
void out_qu(Lq * qu)//出队
{
Qnode * p;
int a;
if(is_empty(qu))
{
printf("队空不能出队!\n");
return;
}
p=qu->front;
a=qu->front->data;
qu->front=p->next;
if(qu->front==NULL)//即队列中只有一个元素
qu->rear=NULL;
free(p);
printf("出队的元素是:%d\n",a);
return;
}
void out_qu(Lq * qu);
void in_qu(Lq * qu,int a);
bool is_empty(Lq *qu);
void init(Lq * & qu);
int main()
{
Lq qu;
Lq * p;
p=&qu;
init(p);
in_qu(p,1);
out_qu(p);
return 0;
}
第二种:
typedef struct Qnode//队列结点定义
{
int data;
Qnode * next;
}Qnode;
typedef struct Lq//链队类型定义
{
Qnode * front;
Qnode * rear;
}Lq;
bool is_empty(Lq *qu)//判断是否队空
{
if(qu->front==NULL||qu->rear==NULL)
{
return true;
}
else
return false;
}
void init(Lq * &qu)//初始化
{
qu=(Lq *)malloc(sizeof(Lq));
(*qu).front=(*qu).rear=NULL;
printf("初始化成功!\n");
}
void in_qu(Lq * qu,int a)//入队
{
Qnode s;
s=(Qnode)malloc(sizeof(Qnode));
s->next=NULL;
s->data=a;
if(is_empty(qu))
{
qu->front=qu->rear=s;
}
else
{
qu->rear->next=s;
qu->rear=s;
}
return;
}
void out_qu(Lq * qu)//出队
{
Qnode * p;
int a;
if(is_empty(qu))
{
printf("队空不能出队!\n");
return;
}
p=qu->front;
a=qu->front->data;
qu->front=p->next;
if(qu->front==NULL)//即队列中只有一个元素
qu->rear=NULL;
free(p);
printf("出队的元素是:%d\n",a);
return;
}
void out_qu(Lq * qu);
void in_qu(Lq * qu,int a);
bool is_empty(Lq *qu);
void init(Lq * & qu);
int main()
{
Lq qu;
init(&qu);
in_qu(&qu,1);
out_qu(&qu);
return 0;
}