• C语言——循环队列和链队列的基本运算


    // 循环队列
    #include <stdio.h> #include "SeqQue.h" // 循环队列的基本运算 /* const int maxsize = 20; typedef struct cycque { int data[maxsize]; int front, rear; }CycQue; */ // 1. 初始化 void InitQueue(CycQue CQ) { CQ.front = 0; CQ.rear = 0; } // 2. 判断队空 int EmptyQueue(CycQue CQ) { if(CQ.rear == CQ.front) return 1; else return 0; } // 3. 入队列 int EnQueue(CycQue CQ, int x) { if((CQ.rear + 1)%maxsize == CQ.front) { printf("队列满 "); return 0; } else { CQ.rear = (CQ.rear + 1)%maxsize; CQ.data[CQ.rear] = x; return 1; } } // 4. 出队列 int OutQueue(CyQue CQ) { if(EmptyQueue(CQ)) { printf("队列空 "); return 0; } else { CQ.front = (CQ.front + 1)%maxsize; return 1; } } // 5.取队列首元素 int GetHead(CycQue CQ) { if(EmptyQueue(CQ)) { printf("队列为空 "); return 0; } else { return CQ.data[(CQ.front + 1)%maxsize]; /* 说明:为了方便操作,规定front指向队列首元素的前一个单元, rear指向实际的队列尾元素单元。 */ } } // 循环队列的基本运算 main() { }

    链队列

    #include <stdio.h>
    #include "Lkqueue.h"
    
    /*
    // 链队列类型定义
    typedef struct LinkQueueNode
    {
        int data;
        struct LinkQueueNode *next;
    }LkQueNode
    typedef struct LkQueue
    {
        LkQueNode *front, *rear;
    }LkQue;
    */
    
    // 1. 队列的初始化
    void InitQueue(LkQue *LQ)
    {
        LkQueNode *temp;
        temp = (LkQueNode *)malloc(sizeof(LkQueNode));
        LQ->front = temp;
        LQ->rear = temp;
        (LQ->front)->next = NULL;
    }
    
    // 2. 判队列空
    int EmptyQueue(LkQue LQ)
    {
        if(LQ.rear == LQ.front)
            return 1;
        else
            return 0;
    }
    
    // 3. 入队列
    void EnQueue(LkQue *LQ, int x)
    {
        LkQueNode *temp;
        temp = (LkQueNode *)malloc(sizeof(LkQueNode));
        temp->data = x;
        temp->next = NULL;
        (LQ->rear)->next = temp; // 新节点入队列
        LQ->rear = temp; // 置新的队列尾节点
    
    }
    
    // 4. 出队列
    int OutQueue(LkQueue *LQ)
    {
        LkQueNode *temp;
        if(EmptyQueue(LQ))
        {
            printf("队列为空
    ");
            return 0;
        }
        else
        {
            temp = LQ->front->next; // 队列首元素
            (LQ->front)->next = temp->next;
    
            if(temp->next == NULL)
                LQ->rear = LQ->front; // 无首节点时,front和rear都指向头节点
            free(temp);
            return 1;
        }
    }
  • 相关阅读:
    合并区间
    编程团体赛
    寻找数组的中间位置
    翻转链表2
    链表翻转
    CF1237H. Balanced Reversals
    arc108E
    agc028D
    CF1446D. Frequency Problem
    CF1439D. INOI Final Contests
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/7401893.html
Copyright © 2020-2023  润新知