• 循环队列(数组实现)


     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 #define false 0
     5 #define true 1
     6 
     7 typedef int ElementType;
     8 typedef int Position;
     9 typedef int bool;
    10 typedef struct QNode *PtrToQNode;
    11 struct QNode
    12 {
    13    ElementType *Data;
    14    Position Front,Rear;
    15    int MaxSize;
    16 };
    17 typedef PtrToQNode Queue;
    18 
    19 Queue CreateQueue(int MaxSize);   //创建一个最大长度为MaxSize的空队列
    20 bool IsFull(Queue Q);   //判断队列是否已满
    21 bool IsEmpty(Queue Q);  //判断队列是否为空
    22 bool AddQ(Queue Q, ElementType X);    //入队
    23 ElementType DeleteQ(Queue Q);         //出队
    24 
    25 
    26 
    27 Queue CreateQueue(int MaxSize)
    28 {
    29     Queue Q = (Queue)malloc(sizeof(struct QNode));
    30     Q->Data = (ElementType *)malloc(MaxSize*sizeof(ElementType));
    31     Q->Front = Q->Rear = 0;
    32     Q->MaxSize = MaxSize;
    33     return Q;
    34 }
    35 
    36 bool IsFull(Queue Q)
    37 {
    38     return ((Q->Rear+1) % (Q->MaxSize)) == Q->Front;
    39 }
    40 
    41 bool IsEmpty(Queue Q)
    42 {
    43     return Q->Front == Q->Rear;
    44 }
    45 
    46 bool AddQ(Queue Q, ElementType X)
    47 {
    48     if(IsFull(Q))
    49     {
    50         printf("队列已满,无法入队!
    ");
    51         return false;
    52     }
    53     Q->Rear = (Q->Rear+1)%(Q->MaxSize);
    54     Q->Data[Q->Rear] = X;
    55     return true;
    56 }
    57 
    58 ElementType DeleteQ(Queue Q)
    59 {
    60     if(IsEmpty(Q))
    61     {
    62         printf("队列为空,无法出队!
    ");
    63         return false;
    64     }
    65     Q->Front = (Q->Front+1)%(Q->MaxSize);
    66     return Q->Data[Q->Front];
    67 }
  • 相关阅读:
    算法面试题总结
    面试题目整理
    九月百度,迅雷,华为,阿里巴巴,最新校招笔试面试十题
    ubuntu 环境变量配置
    VM 共享设置
    五大常用算法之五:分支限界法
    五大常用算法之四:回溯法
    Python之路【第十六篇】Django基础
    Python之路【第十五篇】WEB框架
    Python之路【第十四篇】前端补充回顾
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/9447127.html
Copyright © 2020-2023  润新知