• c语言实现基本的数据结构(五) 单链队列


    #include <stdio.h>
    #include <tchar.h>
    #include <stdlib.h>
    
    
    #define MaxQueueSize 100
    // TODO:  在此处引用程序需要的其他头文件
    struct Node{
        int data;
        Node* next;
    };
    struct Queue{
        Node* front;
        Node* rear;
    };
    //初始化队列
    bool Init_Queue(Queue* q){
        q->front = q->rear = (Node*)malloc(MaxQueueSize*sizeof(Node));
        if (!q->front) return false;
        q->front->next = NULL;
        return true;
    }
    //清空队列
    bool Clear_Queue(Queue* q){
        q->front = q->rear;
        return true;
    }
    //销毁队列
    bool Destroy_Queue(Queue* q){
        Node* temp = q->front;
        while (q->front != q->rear){
            temp = q->front->next;
            free(q->front);
            q->front = temp;
        }
        q->front = q->rear = NULL;
        return true;
    }
    //入队
    bool EnQueue(Queue* q, int value){
        Node* n = (Node*)malloc(sizeof(Node));
        n->next = NULL;
        n->data = value;
    
        q->rear->next = n;
        q->rear = n;
        return true;
    }
    //出队,返回队头元素的值
    int DeQueue(Queue* q){
        int temp;
        if (q->front == q->rear) exit(-1);
        Node* p = q->front;
        temp = p->next->data;
        q->front = p->next;
        if (p == q->rear) q->front = q->rear;
        free(p);
        p = NULL;
        return temp;
    }
    //打印队列
    void Print_Queue(Queue q){
        if (q.front == q.rear) printf("空队列
    ");
        while (q.front != q.rear){
            printf("%d
    ", DeQueue(&q));
        }
    }
  • 相关阅读:
    centos 7下安装mysql 5.7.21
    以多主模式优雅进行MGR复制搭建
    JMeter测试工具
    keepalived故障切换邮件通知
    vim常用快捷键
    mysql高可用之MHA--邮件报警
    Shell脚本实现批量下载资源并保留原始路径
    面试常考题 max pool实现
    Cpp 书籍推荐
    面试常考题 浅谈 赛马问题
  • 原文地址:https://www.cnblogs.com/xin1998/p/7745912.html
Copyright © 2020-2023  润新知