• 队列数组实现


    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <list>
    #include <limits.h>
    #include <algorithm>
    
    /*队列数组实现*/
    struct Queue{
        int Capacity;//容量
        int Size;//实际大小
        int Front;//前指针
        int Rear;//后指针
        int* data;//数组
    };
    //1 is Empty
    int IsEmpty(Queue* q)
    {
        return q->Size == 0;
    }
    //1 is Full
    int IsFull(Queue* q)
    {
        return q->Capacity == q->Size;
    }
    //入队
    void Enqueue(Queue* q, int val)
    {
        if (IsFull(q))
        {
            printf("Queue is full!
    ");
            return;
        }
        q->Rear = (q->Rear + 1) % q->Capacity;
        q->data[q->Rear] = val;
        q->Size++;
    }
    //出队
    int Dequeue(Queue* q)
    {
        if (IsEmpty(q))
        {
            printf("Queue is Empty!
    ");
            return NULL;
        }
        int tmp = q->data[q->Front];
        q->Front = (q->Front + 1) % q->Capacity;
        q->Size--;
        return tmp;
    }
    //打印队列
    void PrintQueue(Queue* q)
    {
        if (IsEmpty(q))
        {
            printf("Queue is Empty!
    ");
            return;
        }
        int i = 0, j = q->Front;
        while (i < q->Size)
        {
            printf("%d
    ", q->data[j++%q->Capacity]);
            i++;
        }
        printf("
    ");
    }
    //创建队列
    Queue* CreateQueue(int MaxCapacity)
    {
        Queue* q = (Queue*)malloc(sizeof(Queue));
        q->Capacity = MaxCapacity;
        q->data = (int*)malloc(sizeof(int)*MaxCapacity);
        q->Front = 0;
        q->Rear = -1;
        q->Size = 0;
        return q;
    }
    
    
    int main(int argc,char** argv)
    {
        Queue *q=CreateQueue(5);
        Enqueue(q, 0);
        Enqueue(q, 1);
        Enqueue(q, 2);
        Enqueue(q, 3);
        Enqueue(q, 4);
        PrintQueue(q);
    
    
        Dequeue(q);
        Dequeue(q);
        PrintQueue(q);
    
        //Enqueue(q, 6);
        Enqueue(q, 7);
        Enqueue(q, 8);
        Enqueue(q, 9);
        PrintQueue(q);
        return 0;
    }
  • 相关阅读:
    RedisUtil
    CSS基础知识点笔记
    fdgfgfgfgf
    PerfMon Metrics Collector插件的Disks I/O使用总结
    Jmeter使用笔记之html报告扩展(一)
    Jmeter使用笔记之意料之外的
    Jmeter使用笔记之函数
    Jmeter使用笔记之组件的作用域
    css 初始化文件 全面
    vue-grid-layout 使用以及各项参数作用
  • 原文地址:https://www.cnblogs.com/wyc199288/p/5292007.html
Copyright © 2020-2023  润新知