zoukankan      html  css  js  c++  java
  • 数据结构-队列的顺序存储(循环队列)

     1 #include "stdio.h"
     2 #include "stdlib.h"
     3 
     4 #define OK 1
     5 #define ERROR 0
     6 #define OVERFLOW -1
     7 #define MAXQSIZE 100
     8 typedef int QElemType;
     9 typedef int Status;
    10 
    11 typedef struct
    12 {
    13     QElemType *base;
    14     int front;
    15     int rear;
    16 }SqQueue;
    17 
    18 Status InitQueue(SqQueue &Q)
    19 {
    20     Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
    21     if(!Q.base)
    22         exit(OVERFLOW);
    23     Q.front = Q.rear = 0;
    24     return OK;
    25 }
    26 
    27 Status DestroyQueue(SqQueue &Q)
    28 {
    29     if(Q.base)
    30         free(Q.base);
    31     else
    32         return ERROR;
    33     Q.base = NULL;
    34     Q.front = Q.rear = 0;
    35     return OK;
    36 }
    37 
    38 Status ClearQueue(SqQueue &Q)
    39 {
    40     Q.front = Q.rear = 0;
    41     return OK;
    42 }
    43 
    44 bool QueueEmpty(SqQueue Q)
    45 {
    46     if(Q.front == Q.rear)
    47         return true;
    48     else
    49         return false;
    50 }
    51 
    52 int QueueLength(SqQueue Q)
    53 {
    54     return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
    55 }
    56 
    57 Status GetHead(SqQueue Q, QElemType &e)
    58 {
    59     if(QueueEmpty(Q))
    60         return ERROR;
    61     e = Q.base[Q.front];
    62     return OK;
    63 }
    64 
    65 Status EnQueue(SqQueue &Q, QElemType e)
    66 {
    67     if((Q.rear + 1) % MAXQSIZE == Q.front)
    68         return ERROR;
    69     Q.base[Q.rear] = e;
    70     Q.rear = (Q.rear + 1) % MAXQSIZE;
    71     return OK;
    72 }
    73 
    74 Status DeQueue(SqQueue &Q, QElemType &e)
    75 {
    76     if(QueueEmpty(Q))
    77         return ERROR;
    78     e = Q.base[Q.front];
    79     Q.front = (Q.front + 1) % MAXQSIZE;
    80     return OK;
    81 }

    函数用处与之前写的链队列的基本操作函数作用是一样的,一些操作基本上也是重复之前写的顺序存储的,所以在这里没有写任何的注释(其实是懒了),不过也可能你根本看不到这句话,因为复制完代码就走了。

  • 相关阅读:
    jqueryeasyui 使用笔记
    怎么查看端口占用情况?
    Ie6 Ie7 双倍padding
    Javascript获取URL
    fckeditor,用p替代div标签设置对齐方式
    ZenCart安全建站的几个措施和步骤
    复选框二选一 javascript
    dedecms从php设置里面去掉index.html
    ajax调用webservice返回DataTable "序列化类型为“System.Reflection.Module”的对象时检测到循环引用
    无题
  • 原文地址:https://www.cnblogs.com/ABook/p/5467954.html
Copyright © 2011-2022 走看看