目录
一、结构体
1.头尾指针节点
typedef struct
{
node front; //头指针
node rear; //尾指针
}link_que, *linkque;
2.队列元素节点
typedef struct node_que
{
int data;
struct node_que *next;
} Node, *node;
二、创建
1.头尾指针节点
linkque create_link_node()
{
linkque Q = (linkque)malloc(sizeof(link_que));
if (Q == NULL)
{
printf("指针节点失败\n");
return NULL;
}
Q->front = NULL;
Q->rear = NULL;
return Q;
}
2.创建队列元素节点
node create_node(int data)
{
node Q = (node)malloc(sizeof(Node));
if (Q == NULL)
{
printf("入参为空\n");
return NULL;
}
Q->next = NULL;
Q->data = data;
return Q;
}
三、判空
int empty_linkque(linkque Q)
{
if (Q == NULL)
{
printf("入参为空\n");
return -1;
}
return Q->rear == NULL ? 1 : 0;
}
四、入队(尾插)
void push_linkque(linkque Q, int data)
{
if (Q == NULL)
{
printf("入参为空\n");
return;
}
node newnode = create_node(data);
if (empty_linkque(Q))
{
Q->front = newnode;
Q->rear = newnode;
return;
}
Q->rear->next = newnode;
Q->rear = newnode;
}
五、出队(头删)
int pop_linkque(linkque Q)
{
if (Q == NULL)
{
printf("入参为空\n");
return -1;
}
if (empty_linkque(Q))
{
printf("链队为空,无法出队\n");
return -1;
}
node del = Q->front;
if (Q->front == Q->rear)
{
int num = del->data;
free(del);
del = NULL;
Q->front = NULL;
Q->rear = NULL;
return num;
}
Q->front = del->next;
int num = del->data;
free(del);
del = NULL;
return num;
}
六、打印
void print_linkque(linkque Q)
{
if (Q == NULL)
{
printf("入参为空\n");
return;
}
if (empty_linkque(Q))
{
printf("链队为空\n");
return;
}
node S = Q->front;
while (S != NULL)
{
printf("Q->data = %d\n", S->data);
S = S->next;
}
}
七、清空
void clean_linkque(linkque Q)
{
if (Q == NULL)
{
printf("入参为空\n");
return;
}
if (empty_linkque(Q))
{
printf("链队为空\n");
return;
}
while (!empty_linkque(Q))
{
pop_linkque(Q);
}
printf("链队已清空\n");
}
八、销毁
void destory_linkque(linkque *Q)
{
if (*Q == NULL)
{
printf("入参为空\n");
return;
}
clean_linkque(*Q);
free(*Q);
*Q = NULL;
printf("链队已销毁\n");
}