• 数据结构:用单链表实现的队列


    #ifndef LINK_QUEUE_HEAD
    #define LINK_QUEUE_HEAD
    #include <stdio.h>
    #include <stdlib.h>
    #define Status int
    #define OVERFLOW -1
    #define OK 0
    #define ERROR 1
    typedef int ElemType;
    
    typedef struct _Queue_Node {
        ElemType data;
        struct _Queue_Node *next;
    } Node;
    
    typedef struct {
        Node *front;//头指针
        Node *rear;//尾指针
    } LinkQueue;
    
    //初始化
    Status init(LinkQueue *q)
    {
        q->front = NULL;
        q->rear  = NULL;
        return OK;
    }
    
    //销毁队列,注意把队列中的元素全部销毁
    Status destroy(LinkQueue *q)
    {
        q->rear = q->front;
        while (q->rear != NULL){
            q->rear = q->rear->next;
            free(q->front);
            q->front = q->rear;
        }
        q->front = NULL;
        q->rear  = NULL;
        return OK;
    }
    
    //压入队列
    Status push(LinkQueue *q, ElemType e)
    {
        Node *p = (Node *)malloc(sizeof(Node));
        if (p == NULL) return OVERFLOW;
    
        p->data = e;
        p->next = NULL;
    
        if (q->front == NULL)//如果队列为空,则重置头指针
            q->front = p;
        else
            q->rear->next = p;//队列存在,则把当前尾部元素的next指向新入队元素
        q->rear = p;//重置尾指针
        return OK;
    }
    
    //出队
    Status pop(LinkQueue *q, ElemType *e)
    {
        Node *p;
        if (q->front == NULL) return OVERFLOW;
        *e = q->front->data;
    
        p = q->front;
        q->front = q->front->next;//front指针后移
        free(p);//已出队的元素需要销毁
        p = NULL;
        return OK;
    }

    #endif
  • 相关阅读:
    快速获取一个正数的掩码
    使用pdfbox删除pdf指定文字内容
    判断奇偶性
    RabbitMQ高级特性
    常见排序算法
    postman和postman interceptor的安装
    Linux命令
    Chrome 错误代码:ERR_UNSAFE_PORT
    IDEA运行tomcat控制台乱码
    Spring Boot 项目架构
  • 原文地址:https://www.cnblogs.com/ifan/p/4227899.html
Copyright © 2020-2023  润新知