数据结构:顺序队列,链式对列,循环队列的详细说明

一、循环队列(queue)

1.1 概念

队列是一种先进先出的结构。 FIFO(first in first out)

1.2 队列的逻辑结构

线性结构

1.3 队列的存储结构

顺序队列是基于顺序标配个两个下标,一个是队列头 front,一个是队列尾 rear。

顺序队列相当于对顺序表操作的一种约束:一端插入,另一端删除。

一般这种顺序队列我们不使用,因为 入队列时 rear++ 出队列时 front++

相当于每块空间指使用了一次,即使出队列了,空间也不会复用了,相当于一次性的。

1.4d代码实现:

1.4.1头文件:

#ifndef _SEQ_QUEUE_H_
#define _SEQ_QUEUE_H_


#include <stdio.h>
#include <stdlib.h>

#define N 5

typedef struct _Queue{
    int s[N];
    int front;
    int rear;
}queue_t;

int print_queue(queue_t *my_queue);
int push_queue(queue_t *my_queue, int in_data);
int is_empty(queue_t *my_queue);
int is_full(queue_t* my_queue);
int create_queue(queue_t **p);
int clean_queue(queue_t *my_queue);
int pop_queue(queue_t *my_queue, int *out_data);
#endif

1.4.2函数文件:

#include "seq_queue.h"

int create_queue(queue_t **p)
{
    if (NULL == p){
        printf("传参c错误\n");
        return -1;
    }
    *p = (queue_t*)malloc(sizeof(queue_t));
    if (*p == NULL){
        printf("内存分配失败\n");
        exit(-1);
    }

    (*p)->front = 0;
    (*p)->front = 0;

    return 0;
}

int is_full(queue_t* my_queue)
{
    if (NULL ==my_queue){
        printf("传参c错误\n");
        return -1;
    }

    return (my_queue->rear+1)%N == my_queue->front?1:0;
}

int is_empty(queue_t *my_queue)
{
    if (NULL ==my_queue){
        printf("传参c错误\n");
        return -1;
    }

    return my_queue->front == my_queue->rear?1:0;
}

int push_queue(queue_t *my_queue, int in_data)
{
    if (NULL ==my_queue){
        printf("传参c错误\n");
        return -1;
    }

    if (is_full(my_queue)){
        printf("已经满了\n");
        return -1;
    }

    my_queue->s[my_queue->rear] = in_data;
    my_queue->rear = (my_queue->rear + 1)%N;

    return 0;
}

int print_queue(queue_t *my_queue)
{
    if (NULL ==my_queue){
        printf("传参c错误\n");
        return -1;
    }

    for (int i = my_queue->front; (i%N)!=my_queue->rear; i++){
        printf("%d ",my_queue->s[i%N]);
    }

    puts("");
}

int clean_queue(queue_t *my_queue)
{
    if (NULL ==my_queue){
        printf("传参c错误\n");
        return -1;
    }

    my_queue->front = 0;
    my_queue->rear = 0;
}

int pop_queue(queue_t *my_queue, int *out_data)
{
    if (NULL ==my_queue){
        printf("传参c错误\n");
        return -1;
    }

    *out_data = my_queue->s[my_queue->front];
    my_queue->front = (my_queue->front + 1)%N;

    return 0;
}

int destroy_queue(queue_t **my_queue)
{
    if (my_queue == NULL || *my_queue == NULL){
        printf("传参c错误\n");
        return -1;
    }

    free(*my_queue);
    *my_queue = NULL;

    return 0;
}

二队列:

2.1概念:

链式队列是基于链表实现的。

链式队列相当于对链表操作的一种约束:一端插入,另一端删除。

一般使用 尾插头删法。

2.2循序队列的操作:

链式队列的操作:

创建队列

清空队列

销毁队列

入队列

出队列

判断队列是否为空

遍历队列--学习阶段看现象用的

2.3代码实现:

2.3.1头文件:

#ifndef _LINK_QUEUE_H_
#define _LINK_QUEUE_H_

#include <stdio.h>
#include <stdlib.h>

typedef struct _Node{
    int data;
    struct _Node* next;
}node_t;

typedef struct _Queue{
    node_t* front;
    node_t* rear;
}queue_t;

int print_queue(queue_t *my_queue);
int push_queue(queue_t *my_queue, int in_data);
int create_queue(queue_t **p);
int pop_queue(queue_t *my_queue, int *out_data);
int is_empty(queue_t *my_queue);
int pop_queue(queue_t *my_queue, int *out_data);
int clean_queue(queue_t *my_queue);
#endif

2.3.2函数文件:

#include "link_queue.h"

int create_queue(queue_t **p)
{
    if (p == NULL){
        printf("入参为NULL,请检查\n");
        return -1;
    }

    *p = (queue_t*)malloc(sizeof(queue_t));
    if (*p == NULL){
        printf("内存分配失败\n");
        exit(-1);
    }

    (*p)->front = NULL;
    (*p)->rear = NULL;
    
    return 0;
}

int push_queue(queue_t *my_queue, int in_data)
{
    if (my_queue == NULL){
        printf("入参为NULL,请检查\n");
        return -1;
    }
    
    node_t* pnew = (node_t*)malloc(sizeof(node_t));
    pnew->next = NULL;
    pnew->data = in_data;
    
    if (my_queue->front == NULL && my_queue->rear == NULL){
        my_queue->front = pnew;
        my_queue->rear = pnew;
    } else {
        my_queue->rear->next = pnew;
        my_queue->rear = pnew;
    }

    return 0;
}

int print_queue(queue_t *my_queue)
{  
    if (my_queue == NULL){
        printf("入参为NULL,请检查\n");
        return -1;
    }

    node_t* temp = my_queue->front;

    while (temp!=NULL){
        printf("%d ",temp->data);
        temp = temp->next;
    }
    puts("");
    return 0;
}

int is_empty(queue_t *my_queue)
{
    if (my_queue == NULL){
        printf("入参为NULL,请检查\n");
        return -1;
    }

    return my_queue->front == NULL?1:0;
}

int pop_queue(queue_t *my_queue, int *out_data)
{
    if (my_queue == NULL){
        printf("入参为NULL,请检查\n");
        return -1;
    }

    if (is_empty(my_queue)){
        printf("队列为空,不需要出队列\n");
        return -1;
    }

    node_t* temp;

    temp = my_queue->front;
    my_queue->front = temp->next;
    *out_data = temp->data;

    free(temp);
    temp = NULL;

    return 0;
}

int clean_queue(queue_t *my_queue){
    if(NULL == my_queue){
        printf("入参为NULL, 请检查\n");
        return -1;
    }
 
    node_t *pdel = NULL;
    while(NULL != my_queue->front){
        pdel = my_queue->front;
        my_queue->front = pdel->next;
        free(pdel);
    }
    pdel = NULL;
    my_queue->rear = NULL;
    return 0;
}


int destroy_queue(queue_t **my_queue){
    if(NULL == my_queue || NULL == *my_queue){
        printf("入参为NULL, 请检查\n");
        return -1;
    }
  
    clean_queue(*my_queue);
    
    free(*my_queue);
    *my_queue = NULL;
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值