• 数据结构与算法(五)循环队列实现


    • 接口
    #ifndef __QUEUE_H__ 
    #define __QUEUE_H__ 
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <stdbool.h> 
    typedef int ElementType; 
    #define MAXSIZE 5 
    typedef struct QNode { 
       ElementType Data[MAXSIZE]; 
       int rear; 
       int front; 
       int count; 
    }QNode; 
    typedef QNode* Queue; 
    Queue CreatQueue();// 
    bool IsFull(Queue Q);// 
    bool isEmpty(Queue Q);// 
    void AddQ(Queue Q, ElementType item);// 
    ElementType DeleteQ(Queue Q);// 
    void Display(Queue Q); 

    • 函数实现
    #include "Queue.h" 
    Queue CreatQueue() 
    { 
       Queue temp = (Queue)malloc(sizeof(QNode)); 
       temp->front = 0; 
       temp->rear = 0; 
       temp->count = 0; 
       return temp; 
    } 
    bool IsFull(Queue Q) 
    { 
       return (Q->count == MAXSIZE); 
    } 
    bool IsEmpty(Queue Q) 
    { 
       return (Q->count == 0); 
    } 
    void AddQ(Queue Q, ElementType item) 
    { 
       if(IsFull(Q)){ 
           printf("queue is full.\n"); 
           return; 
       } 
       Q->Data[Q->rear] = item; 
       Q->rear = (Q->rear + 1) % MAXSIZE; 
       Q->count++; 
    } 
    ElementType DeleteQ(Queue Q) 
    { 
       if (IsEmpty(Q)) { 
           printf("queue is empty.\n"); 
           return 0; 
       } 
       ElementType firstData = Q->Data[Q->front]; 
       Q->front = (Q->front + 1) % MAXSIZE; 
       Q->count--; 
       return firstData; 
    } 
    void Display(Queue Q) 
    { 
       while (!IsEmpty(Q)) 
       { 
           printf("%d ", DeleteQ(Q)); 
       } 
       printf("\n"); 
    }
    • 测试函数
    int main(int argc, char**argv) 
    { 
       Queue myQueue = CreatQueue(); 
       for (int i = 0; i < 10; i++) { 
           AddQ(myQueue, i); 
       } 
       Display(myQueue); 
       for (int i = 100; i < 105; i++) { 
           AddQ(myQueue, i); 
       } 
       Display(myQueue); 
       return 0; 
    }
    • 测试结果

    • GitHub地址:

    https://github.com/baiyu2017/baiyu2021.git

  • 相关阅读:
    java 利用jsoup 爬取知乎首页问题
    ROIAlign, ROIPooling及ROIWarp对比
    yii2.0 gii
    mysql索引操作
    lbs basic mongodb
    php操作mongodb
    设计模式六大原则
    Java集合
    Java 快速失败( fail-fast ) 安全失败( fail-safe )
    计数数组中值的出现次数
  • 原文地址:https://www.cnblogs.com/wuyouxiaocai/p/15640639.html
Copyright © 2020-2023  润新知