zoukankan      html  css  js  c++  java
  • 顺序队列(C语言)

      1 #define Queue_MAX_SIZE 20
      2 #define OK 1
      3 #define ERROR 0
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 typedef int QueueType; //队列元素类型
      7 typedef struct
      8 {
      9     QueueType *pBase;    //队列指针
     10     QueueType front;     //队头索引
     11     QueueType rear;      //队尾索引
     12     int maxSize;         //当前分配最大容量
     13 }Queue;
     14 //队列的初始化
     15 int InitQueue(Queue *p)
     16 {
     17     p->pBase = (QueueType *)malloc(Queue_MAX_SIZE * sizeof(QueueType));
     18     if (p->pBase == NULL) return ERROR; //内存分配失败
     19     p->front = 0;
     20     p->rear = 0;     //初始化 队头队尾索引均为0
     21     p->maxSize = Queue_MAX_SIZE;
     22     return 0;
     23 }
     24 //销毁队列
     25 void destroyQueue(Queue *p)
     26 {
     27     free(p);
     28     p = NULL;
     29 
     30 }
     31 //清空队列
     32 void clearQueue(Queue *p)
     33 {
     34     p->front = 0;
     35     p->rear = 0;
     36 }
     37 //判断队列是否为空
     38 int isEmpityQueue(Queue *p)
     39 {
     40     if (p->front == p->rear)
     41         return OK;
     42     return ERROR;
     43 
     44 }
     45 /*
     46  *在循环队列中,“队满”和“队空”的条件有可能是相同的,都是front==rear,
     47  *这种情况下,无法区别是“队满”还是“队空”。
     48  *针对这个问题,有3种可能的处理方法:
     49  *(1)另设一个标志以区别是“队满”还是“队空”。(即入队/出队前检查是否“队满”/“队空”)
     50  *(2)设一个计数器,此时甚至还可以省去一个指针。
     51  *(3)少用一个元素空间,即约定队头指针在队尾指针的下一位置时就作为“队满”的标志,
     52  *即“队满”条件为:(PQueue->rear+1)%MAX_SIZE == PQueue->front。
     53  *  【这里采用了第3种处理方法】
     54  */
     55 //判断队列是否满
     56 int isFullQueue(Queue *p)
     57 {
     58     if ((p->rear + 1) % p->maxSize == p->front)
     59         return OK;
     60     return ERROR;
     61 
     62 }
     63 //获得队列长度
     64 int getQueueLen(Queue *p)
     65 {
     66     return  (p->rear - p->front + p->maxSize) % p->maxSize;
     67 }
     68 //新元素入队
     69 int enQueue(Queue *p, QueueType e)
     70 {
     71     if (isFullQueue(p) == OK)
     72     {
     73         printf("队列已满
    ");
     74         return ERROR;
     75     }
     76     p->pBase[p->rear] = e;
     77     p->rear = (p->rear + 1) % p->maxSize;
     78     return OK;
     79 }
     80 //队头元素出列
     81 int deQueue(Queue *p, QueueType *pe)
     82 {
     83     //如果队列为空 则返回ERROR
     84     if (isEmpityQueue(p) == OK)
     85     {
     86         printf("队列为空,出队失败
    ");
     87         return ERROR;
     88 
     89     }
     90     *pe = p->pBase[p->front];
     91     p->front = (p->front + 1) % p->maxSize;
     92 
     93     return OK;
     94 }
     95 //遍历队列
     96 void queueTraverse(Queue *p)
     97 {
     98     int i = p->front;
     99     printf("遍历队列
    ");
    100     while (i != p->rear)
    101     {
    102         printf("%d ", p->pBase[i]);
    103         i = (i + 1) % p->maxSize;
    104     }
    105     printf("
    ");
    106 
    107 }
    108 
    109 int main()
    110 {
    111     QueueType *pe;
    112     pe = (QueueType*)malloc(sizeof(QueueType));
    113     Queue *PQueue = (Queue *)malloc(sizeof(Queue));
    114     if (!PQueue->pBase)
    115     {
    116         printf("给队列对象分配内存失败
    ");
    117         return -1;
    118     }
    119 
    120     //调用初始化队列的函数
    121     InitQueue(PQueue);
    122     //调用出队函数
    123     enQueue(PQueue, 1);
    124     enQueue(PQueue, 2);
    125     enQueue(PQueue, 3);
    126     enQueue(PQueue, 4);
    127     enQueue(PQueue, 5);
    128     enQueue(PQueue, 6);
    129     enQueue(PQueue, 7);
    130     enQueue(PQueue, 8);
    131     //调用遍历队列的函数
    132     queueTraverse(PQueue);
    133     //调用出队函数
    134     if (deQueue(PQueue, pe))
    135     {
    136         printf("出队一次,元素为:%d
    ", *pe);
    137     }
    138     queueTraverse(PQueue);
    139     if (deQueue(PQueue, pe))
    140     {
    141         printf("出队一次,元素为:%d
    ", *pe);
    142     }
    143     queueTraverse(PQueue);
    144 
    145     destroyQueue(PQueue);
    146 
    147 
    148     return 0;
    149 
    150 
    151 
    152 }
  • 相关阅读:
    [报错]编译报错:clang: error: linker command failed with exit code 1及duplicate symbol xxxx in错误解决方法之一
    修改UISearBar的文字颜色,placehoder颜色及输入框颜色
    designated initializer和secondary initializer是什么?
    设置UIButton的文字居右显示 去掉点击默认置灰效果
    设置UITextField的placeholder的颜色
    Xcode8和iOS10的适配问题
    [转载]做一个 App 前需要考虑的几件事
    git 常用命令行整理
    Xcode中使用GitHub详解
    截取字符串
  • 原文地址:https://www.cnblogs.com/mwq1024/p/10545781.html
Copyright © 2011-2022 走看看