数据结构学习——队列代码

本文主要探讨了数据结构中的队列,并提供了两种实现方式:顺序队列和链队的详细代码,包括`common.h`头文件,`queue.c`中对顺序队列的实现,以及`linkqueue.c`中链队的实现,帮助读者深入理解队列的操作和内部工作机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

common.h

#ifndef __COMMON_H__
#define __COMMON_H__

#define QUEUE_SIZE_MAX 5

typedef int QElemType;

typedef struct queue_s
{
    QElemType *base;     // queue base
    int front;           // head
    int rear;            // tail
}QUEUE_S, *PQUEUE_S;


int queue_init(QUEUE_S *q);

void queue_deinit(QUEUE_S *q);
int queue_en(QUEUE_S *q, QElemType e);
int queue_de(QUEUE_S *q, QElemType *e);
void queue_dump(QUEUE_S *q);

typedef struct qnode_s
{
    QElemType data;
    struct qnode_s *next;
}QNODE_S, *PQNODE_S;

typedef struct linkqueue_s
{
    PQNODE_S front;
    PQNODE_S rear;
}LINKQUEUE_S, *PLINKQUEUE_S;

int linkqueue_init(LINKQUEUE_S *q);

void linkqueue_deinit(LINKQUEUE_S *q);
int linkqueue_en(LINKQUEUE_S *q, QElemType e);
int linkqueue_de(LINKQUEUE_S *q, QElemType *e);
void linkqueue_dump(LINKQUEUE_S *q);


#endif


顺序队列queue.c

//顺序队列

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


int queue_init(QUEUE_S *q)
{
    if(!q)
    {
        printf("ERR: NULL pointer.\n");
        return -1;
    }
    
    q->base = malloc(QUEUE_SIZE_MAX * sizeof(QElemType));
    if(!(q->base))
    {
        printf("ERR: malloc fail\n");
        return -1;
    }

    q->rear = q->front = 0;
    return 0;
}

void queue_deinit(QUEUE_S *q)
{
    if(!q)
    {
        printf("ERR: NULL pointer.\n");
        return;
    }
    
    if(!(q->base))
    {
        printf("ERR: queue is null.\n");
        return;
    }

    free(q->base);

    q->base = NULL;
}

int queue_en(QUEUE_S *q, QElemType e)
{
    if(!q)
    {
        printf("ERR: NULL pointer.\n");
        return -1;
    }

    /* full? */
    if(((q->rear + 1) % QUEUE_SIZE_MAX) == q->front)
    {
        printf("ERR: queue is full.\n");
        return -1;
    }

    q->base[q->rear] = e;
    q->rear = (q->rear+1) % QUEUE_SIZE_MAX;
    return 0;
}

int queue_de(QUEUE_S *q, QElemType *e)
{
    if(!q)
    {
        printf("ERR: NULL pointer.\n");
        return -1;
    }

    /* empty? */
    if(q->rear == q->front)
    {
        printf("ERR: queue is empty.\n");
        return -1;
    }

    *e = q->base[q->front];
    q->front = (q->front+1) % QUEUE_SIZE_MAX;
    return 0;
}

void queue_dump(QUEUE_S *q)
{
    int i;
    QElemType e;
    int used_size;
    
    if(!q)
    {
        printf("ERR: NULL pointer.\n");
        return;
    }

    printf("======================================\n");
    printf("             queue dump  \n");
    printf("======================================\n");

    used_size = (QUEUE_SIZE_MAX + q->rear - q->front) % QUEUE_SIZE_MAX;
    printf(" Used / Total : %d / %d.\n", used_size, QUEUE_SIZE_MAX);

    for(i = 0; i < used_size; i++)
    {
        e = q->base[(q->front + i) % QUEUE_SIZE_MAX];
        printf(" %02d: %d.\n", (q->front + i), e);
    }
    
}


链队linkqueue.c

//链队
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#if 1

int linkqueue_init(LINKQUEUE_S *q)
{
    if(!q)
    {
        printf("ERR: NULL pointer.\n");
        return -1;
    }

    q->front = q->rear = malloc(sizeof(QNODE_S));
    q->front->next = NULL;
    return 0;
}

void linkqueue_deinit(LINKQUEUE_S *q)
{
    PQNODE_S ptr, tmp;
    if(!(q))
    {
        printf("ERR: %s--> link stack not exist.\n", __func__);
        return;
    }

    ptr = (q->front);
    while(ptr->next != NULL)
    {
        tmp = ptr;
        free(ptr);
        ptr = tmp->next;
    }    

    free(q->front);
}

int linkqueue_en(LINKQUEUE_S *q, QElemType e)
{
    PQNODE_S ptr = malloc(sizeof(QNODE_S));

    ptr->data = e;
    ptr->next = NULL;

    (q->rear)->next = ptr;
    q->rear = ptr;
    return 0;
}

int linkqueue_de(LINKQUEUE_S *q, QElemType *e)
{
    PQNODE_S ptr;
    if(!(q))
    {
        printf("ERR: %s--> link stack not exist.\n", __func__);
        return -1;
    }

    // empty?
    if(q->front == q->rear)
    {
        printf("ERR: %s--> link stack empty.\n", __func__);
        return -1;
    }
    
    ptr = (q->front)->next;
    *e = ptr->data;
    
    (q->front)->next = ptr->next;

    // empty now?
    if(q->rear == ptr)
    {
        q->rear = q->front;
        q->front->next = NULL;
    }
    
    free(ptr);
    return 0;
}

void linkqueue_dump(LINKQUEUE_S *q)
{
    int i = 0;
    PQNODE_S ptr = q->front;
    
    if(!(q))
    {
        printf("ERR: %s--> link stack not exist.\n", __func__);
        return;
    }

    printf("======================================\n");
    printf("             linkt stack dump  \n");
    printf("======================================\n");

    while(ptr->next != NULL)
    {
        printf("top - %d : %d.\n", i, (ptr->next)->data);
        i++;
        ptr = ptr->next;
    }    
    
    printf("==========dump=end====================\n");
}

#endif


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值