数据结构学习系列之链式队列

  • 链式队列:
  • 即:队列的链式存储结构
  • 本质上就是线性表的单链表,只是它局限于尾进头出,也就是单链表尾插法头删法
  • 入队列:
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;


}
  • 示例代码:
  • link_queue.h:
#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
  • link_queue.c:
#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;

}
  • main.c:
#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)
  • 本示例代码,仅供参考;

你可能感兴趣的:(数据结构学习系列,数据结构,学习,算法,后端,开发语言,链式队列)