数据结构顺序队列和链队列的数据封装

顺序队列

定义数据结构:

typedef void (queue_op_t)(const void *);


typedef struct queue {
    void *queue;
    int size;
    int max;
    int front;
    int rear;
} QUEUE;


//初始化
QUEUE *queue_creat(int size, int max);
//入队
int queue_push(QUEUE *handle, void *data);
//出队
void *queue_pop(QUEUE *handle);
//判断满队
int queue_is_full(QUEUE *handle);
//判断空队
int queue_is_empty(QUEUE *handle);
//遍历
void queue_travel(QUEUE *handle, queue_op_t *op);
//销毁
void queue_destroy(QUEUE **handle);
//清空
void queue_clean(QUEUE *handle);
//num
int queue_num(QUEUE *handle);
//存储
void queue_store(QUEUE *handle, const char *path);
//加载
QUEUE *queue_load(const char *path);

init creat

QUEUE *queue_creat(int size, int max)
{
    QUEUE *handle = (QUEUE *)malloc(sizeof(QUEUE));
    ERRP(handle == NULL, "handle malloc", goto ERR1);

    handle->queue = malloc(max * size);
    ERRP(handle->queue == NULL, "queue malloc", goto ERR2);

    handle->size = size;
    handle->max = max;
    handle->front = 0;
    handle->rear = 0;

    return handle;

ERR2:
    free(handle);
ERR1:
    return NULL;
}

**push **

int queue_push(QUEUE *handle, void *data)
{
    if (queue_is_full(handle))
    {
        printf("queue is full, can't push\n");
        return 0;
    }
    else
    {
        memmove(handle->queue + handle->rear * handle->size, data, handle->size);
        handle->rear = (handle->rear + 1) % handle->max;
    }
    return 1;
}

pop

void *queue_pop(QUEUE *handle)
{
    if (queue_is_empty(handle))
    {
        printf("queue is empty, can't pop\n");
        return NULL;
    }
    else
    {
        void *data = malloc(handle->size);
        ERRP(data == NULL, "data malloc", goto ERR1);

        memmove(data, handle->queue + handle->front * handle->size, handle->size);
        handle->front = (handle->front + 1) % handle->max;

        return data;
    }

ERR1:
    return NULL;
}

is_full

int queue_is_full(QUEUE *handle)
{
    return (handle->rear + 1) % handle->max == handle->front;
}

is_empty

int queue_is_empty(QUEUE *handle)
{
    return handle->front == handle->rear;
}

traval

void queue_travel(QUEUE *handle, queue_op_t *op)
{
    int i = handle->front;
    while (i != handle->rear)
    {
        op(handle->queue + i * handle->size);
        i = (i + 1) % handle->max;
    }
}

destroy

void queue_destroy(QUEUE **handle)
{
    free((*handle)->queue);
    free(*handle);
    *handle = NULL;
}

**clean **

void queue_clean(QUEUE *handle)
{
    handle->front = 0;
    handle->rear = 0;
}

num count

int queue_num(QUEUE *handle)
{
    if (handle->rear >= handle->front)
    {
        return handle->rear - handle->front;
    }
    else
    {
        return handle->max - (handle->front - handle->rear);
    }
}

file io

// 存储
void queue_store(QUEUE *handle, const char *path)
{
    FILE *fp = fopen(path, "w");
    ERRP(fp == NULL, "fopen", goto ERR1);

    if (fwrite(handle, sizeof(*handle), 1, fp) != 1)
    {
        perror("fwrite");
        goto ERR2;
    }

    int i = handle->front;
    while (i != handle->rear)
    {
        if (fwrite(handle->queue + i * handle->size, handle->size, 1, fp) != 1)
        {
            perror("fwrite");
            goto ERR2;
        }
        i = (i + 1) % handle->max;
    }

    fclose(fp);
    return;

ERR2:
    fclose(fp);
ERR1:
    return;
}

// 加载
QUEUE *queue_load(const char *path)
{
    FILE *fp = fopen(path, "r");
    ERRP(fp == NULL, "fopen", goto ERR1);

    QUEUE *handle = (QUEUE *)malloc(sizeof(QUEUE));
    ERRP(handle == NULL, "handle malloc", goto ERR2);

    if (fread(handle, sizeof(*handle), 1, fp) != 1)
    {
        perror("fread");
        goto ERR2;
    }

    handle->queue = malloc(handle->max * handle->size);
    ERRP(handle->queue == NULL, "queue malloc", goto ERR3);

    int i = handle->front;
    while (i != handle->rear)
    {
        if (fread(handle->queue + i * handle->size, handle->size, 1, fp) != 1)
        {
            perror("fread");
            goto ERR4;
        }
        i = (i + 1) % handle->max;
    }

    fclose(fp);
    return handle;

ERR4:
    free(handle->queue);
ERR3:
    free(handle);
ERR2:
    fclose(fp);
ERR1:
    return NULL;
}

链队列

定义数据结构:

typedef void (queue_op_t)(const void *);


typedef struct lqueue_node {
    void *data; // 数据
    struct lqueue_node *next; // 指向下一个节点的指针
}LQUEUE_NODE;

// 定义链式队列
typedef struct lqueue {
    LQUEUE_NODE *front; // 队头指针
    LQUEUE_NODE *rear;  // 队尾指针
    int size; // 队中数据类型大小
    int count; // 队列中元素的数量
}LQUEUE;

//初始化
LQUEUE *lqueue_creat(int size);
//入队
int lqueue_push(LQUEUE *handle, void *data);
//出队
void *lqueue_pop(LQUEUE *handle);
//判断空队
int lqueue_is_empty(LQUEUE *handle);
//遍历
void lqueue_travel(LQUEUE *handle, queue_op_t *op);
//销毁
void lqueue_destroy(LQUEUE **handle);
//清空
void lqueue_clean(LQUEUE *handle);
//num
int lqueue_num(LQUEUE *handle);
//存储
void lqueue_store(LQUEUE *handle, const char *path);
//加载
LQUEUE *lqueue_load(const char *path);

// init creat

LQUEUE *lqueue_creat(int size)
{
    LQUEUE *handle = (LQUEUE *)malloc(sizeof(LQUEUE));
    if (handle == NULL)
    {
        perror("handle malloc");
        return NULL;
    }

    handle->front = NULL;
    handle->rear = NULL;
    handle->size = size;
    handle->count = 0;

    return handle;
}

**push **

int lqueue_push(LQUEUE *handle, void *data)
{
    LQUEUE_NODE *new_node = (LQUEUE_NODE *)malloc(sizeof(LQUEUE_NODE));
    if (new_node == NULL)
    {
        perror("new_node malloc");
        return 0;
    }

    new_node->data = (void *)malloc(handle->size);
    if (new_node->data == NULL)
    {
        perror("data malloc");
        free(new_node);
        return 0;
    }

    memcpy(new_node->data, data, handle->size);
    new_node->next = NULL;

    if (handle->rear == NULL)
    {
        handle->front = new_node;
    }
    else
    {
        handle->rear->next = new_node;
    }

    handle->rear = new_node;
    handle->count++;
    return 1;
}

pop

void *lqueue_pop(LQUEUE *handle)
{
    if (handle->front == NULL)
    {
        printf("queue is empty, can't pop\n");
        return NULL;
    }

    LQUEUE_NODE *node = handle->front;
    void *data = node->data;

    handle->front = node->next;
    if (handle->front == NULL)
    {
        handle->rear = NULL;
    }

    free(node);
    handle->count--;
    return data;
}

empty

int lqueue_is_empty(LQUEUE *handle)
{
    return handle->count == 0;
}

traval

void lqueue_travel(LQUEUE *handle, queue_op_t *op)
{
    LQUEUE_NODE *current = handle->front;
    while (current != NULL)
    {
        op(current->data);
        current = current->next;
    }
}

destroy

void lqueue_destroy(LQUEUE **handle)
{
    lqueue_clean(*handle);
    free(*handle);
    *handle = NULL;
}

clean

void lqueue_clean(LQUEUE *handle)
{
    while (!lqueue_is_empty(handle))
    {
        void *data = lqueue_pop(handle);
        free(data);
    }
}

num count

int lqueue_num(LQUEUE *handle)
{
    return handle->count;
}

file io

// 存储
void lqueue_store(LQUEUE *handle, const char *path)
{
    FILE *fp = fopen(path, "w");
    if (fp == NULL)
    {
        perror("fopen");
        return;
    }

    fwrite(&handle->size, sizeof(int), 1, fp);
    fwrite(&handle->count, sizeof(int), 1, fp);

    LQUEUE_NODE *current = handle->front;
    while (current != NULL)
    {
        fwrite(current->data, handle->size, 1, fp);
        current = current->next;
    }

    fclose(fp);
}

// 加载
LQUEUE *lqueue_load(const char *path)
{
    FILE *fp = fopen(path, "r");
    if (fp == NULL)
    {
        perror("fopen");
        return NULL;
    }

    LQUEUE *handle = (LQUEUE *)malloc(sizeof(LQUEUE));
    if (handle == NULL)
    {
        perror("handle malloc");
        fclose(fp);
        return NULL;
    }

    fread(&handle->size, sizeof(int), 1, fp);
    fread(&handle->count, sizeof(int), 1, fp);

    handle->front = NULL;
    handle->rear = NULL;

    for (int i = 0; i < handle->count; i++)
    {
        void *data = malloc(handle->size);
        if (data == NULL)
        {
            perror("data malloc");
            lqueue_destroy(&handle);
            fclose(fp);
            return NULL;
        }

        fread(data, handle->size, 1, fp);
        lqueue_push(handle, data);
        free(data);
    }

    fclose(fp);
    return handle;
}

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值