#include <stdio.h>
#include <stdlib.h>
typedef struct Qnode//队列结点定义
{
int data;
Qnode* next;
}Qnode;
typedef struct Lq//链队类型定义
{
Qnode* front;
Qnode* rear;
}Lq;
bool is_empty(Lq *qu)//判断是否队空
{
printf("%d\n", sizeof(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;
printf("in\n");
if(is_empty(qu))
{
printf("2\n");
qu->front = qu->rear = s;
}
else
{
printf("3\n");
qu->rear->next = s;
printf("4\n");
qu->rear = s;
}
printf("in2\n");
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 *tmp = &qu;
printf("front:%.8X, rear:%.8X\n", tmp->front, tmp->rear);
init(&qu);
printf("front:%.8X, rear:%.8X\n", tmp->front, tmp->rear);
printf("*******************************\n");
qu.front = NULL;
qu.rear = NULL;
printf("front:%.8X, rear:%.8X\n", tmp->front, tmp->rear);
exit(1);
printf("1\n");
in_qu(&qu, 1);
printf("12\n");
out_qu(&qu);
return 0;
}
front:CCCCCCCC, rear:CCCCCCCC
初始化成功!
front:CCCCCCCC, rear:CCCCCCCC
*******************************
front:00000000, rear:00000000
请按任意键继续. . .
// 初始化
void init(Lq *qu)
{
qu = (Lq *)malloc(sizeof(Lq));
(*qu).front = (*qu).rear = NULL;
printf("初始化成功!\n");
}
|