- 链式队列:
- 即:队列的
链式存储结构
;
本质
上就是线性表的单链表
,只是它局限于尾进头出
,也就是单链表
的尾插法
和头删法
;
- 入队列:
int push_link_queue(queue_t *link_queue,int data)
{
if(NULL == link_queue)
{
printf("入参合理性检查\n");
return -1;
}
node_t *pnew = (node_t *)malloc(sizeof(node_t));
if(NULL == pnew){
printf("内存空间分配失败\n");
return -1;
}
pnew->data = data;
pnew->next = NULL;
if(NULL == link_queue->front && NULL == link_queue->rear)
{
link_queue->front = pnew;
link_queue->rear = pnew;
} else {
link_queue->rear->next = pnew;
link_queue->rear = pnew;
}
return 0;
}
int pop_link_queue(queue_t *link_queue,int *data)
{
if(NULL == link_queue || NULL == data)
{
printf("入参合理性检查\n");
return -1;
}
if(queue_is_empty(link_queue))
{
printf("此时队列为空,出队失败\n");
return -1;
}
*data = link_queue->front->data;
node_t *pdel = link_queue->front;
link_queue->front = pdel->next;
free(pdel);
pdel = NULL;
if(NULL == link_queue->front)
{
link_queue->rear = NULL;
}
return 0;
}
#ifndef __LINK_QUEUE_H__
#define __LINK_QUEUE_H__
#include
#include
#include
typedef struct NODE
{
int data;
struct NODE *next;
}node_t;
typedef struct link_queue{
node_t *front;
node_t *rear;
}queue_t;
int create_link_queue(queue_t **p);
int push_link_queue(queue_t *link_queue,int data);
int queue_is_empty(queue_t *link_queue);
int pop_link_queue(queue_t *link_queue,int *data);
int clean_link_queue(queue_t *link_queue);
int destroy_link_queue(queue_t **link_queue);
int print_link_queue(queue_t *link_queue);
#endif
#include "link_queue.h"
int create_link_queue(queue_t **p)
{
if(NULL == p)
{
printf("入参合理性检查\n");
return -1;
}
*p = (queue_t *)malloc(sizeof(queue_t));
if(NULL == *p)
{
printf("内存空间分配失败\n");
return -1;
}
(*p)->front = NULL;
(*p)->rear = NULL;
return 0;
}
int push_link_queue(queue_t *link_queue,int data)
{
if(NULL == link_queue)
{
printf("入参合理性检查\n");
return -1;
}
node_t *pnew = (node_t *)malloc(sizeof(node_t));
if(NULL == pnew){
printf("内存空间分配失败\n");
return -1;
}
pnew->data = data;
pnew->next = NULL;
if(NULL == link_queue->front && NULL == link_queue->rear)
{
link_queue->front = pnew;
link_queue->rear = pnew;
} else {
link_queue->rear->next = pnew;
link_queue->rear = pnew;
}
return 0;
}
int queue_is_empty(queue_t *link_queue)
{
if(NULL == link_queue)
{
printf("入参合理性检查\n");
return -1;
}
return link_queue->rear == NULL ? 1 : 0;
}
int pop_link_queue(queue_t *link_queue,int *data)
{
if(NULL == link_queue || NULL == data)
{
printf("入参合理性检查\n");
return -1;
}
if(queue_is_empty(link_queue))
{
printf("此时队列为空,出队失败\n");
return -1;
}
*data = link_queue->front->data;
node_t *pdel = link_queue->front;
link_queue->front = pdel->next;
free(pdel);
pdel = NULL;
if(NULL == link_queue->front)
{
link_queue->rear = NULL;
}
return 0;
}
int clean_link_queue(queue_t *link_queue)
{
if(NULL == link_queue)
{
printf("入参合理性检查\n");
return -1;
}
node_t *pdel = NULL;
while(NULL != link_queue->front)
{
pdel = link_queue->front;
link_queue->front = pdel->next;
free(pdel);
pdel = NULL;
}
link_queue->rear = NULL;
return 0;
}
int destroy_link_queue(queue_t **link_queue)
{
if(NULL == link_queue || NULL == *link_queue)
{
printf("入参合理性检查\n");
return -1;
}
clean_link_queue(*link_queue);
free(*link_queue);
*link_queue = NULL;
return 0;
}
int print_link_queue(queue_t *link_queue)
{
if(NULL == link_queue)
{
printf("入参合理性检查\n");
return -1;
}
node_t *ptemp = link_queue->front;
while(NULL != ptemp)
{
printf("%d ",ptemp->data);
ptemp = ptemp->next;
}
puts("");
return 0;
}
#include "link_queue.h"
int main(int argc, char const *argv[])
{
queue_t *link_queue = NULL;
create_link_queue(&link_queue);
printf("link_queue = %p\n",link_queue);
push_link_queue(link_queue,22);
push_link_queue(link_queue,22);
push_link_queue(link_queue,22);
push_link_queue(link_queue,99);
push_link_queue(link_queue,22);
push_link_queue(link_queue,22);
push_link_queue(link_queue,22);
push_link_queue(link_queue,66);
print_link_queue(link_queue);
int data;
pop_link_queue(link_queue,&data);
printf("%d ",data);
pop_link_queue(link_queue,&data);
printf("%d ",data);
pop_link_queue(link_queue,&data);
printf("%d ",data);
pop_link_queue(link_queue,&data);
printf("%d ",data);
pop_link_queue(link_queue,&data);
printf("%d ",data);
pop_link_queue(link_queue,&data);
printf("%d ",data);
pop_link_queue(link_queue,&data);
printf("%d ",data);
pop_link_queue(link_queue,&data);
printf("%d ",data);
puts("");
pop_link_queue(link_queue,&data);
clean_link_queue(link_queue);
print_link_queue(link_queue);
destroy_link_queue(&link_queue);
printf("link_queue = %p\n",link_queue);
return 0;
}
link_queue = 0x55f7c1cbd260
22 22 22 99 22 22 22 66
22 22 22 99 22 22 22 66
此时队列为空,出队失败
link_queue = (nil)