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

    工程目录结构图:

    common.h:

     1 //#ifndef __common_h__
     2 //#define __common_h__
     3 
     4 #define OK 1
     5 #define ERROR 0
     6 #define TRUE 1
     7 #define FALSE 0 
     8 
     9 #define MAXSIZE 20
    10 
    11 typedef int Status;        //函数的返回结果,OK、ERREO、TRUE、FALSE
    12 typedef int ElemType;   //结点数据域的数据类型
    13 
    14 //#endif

    common.h:

    1 #include "common.h"
    2 
    3 Status visit(ElemType e)
    4 {
    5     printf("%d , ", e);
    6     return OK;
    7 }

    Queue.h:

    1 //循环队列的结构
    2 typedef struct
    3 {
    4     ElemType data[MAXSIZE];
    5     int front;                //头指针
    6     int rear;                //尾指针,队列不为空,指向队列尾元素的下一位
    7 }SqQueue;

    Queue.c:

     1 #include <stdio.h>
     2 #include "common.h"
     3 #include "Queue.h"
     4 
     5 Status InitQueue(SqQueue *Q)
     6 {
     7     Q->front = 0;
     8     Q->rear = 0;
     9     return OK;
    10 }
    11 
    12 Status ClearQueue(SqQueue *Q)
    13 {
    14     Q->front = Q->rear = 0;
    15     return OK;
    16 }
    17 
    18 Status QueueEmpty(SqQueue Q)
    19 {
    20     if (Q.front == Q.rear)
    21         return OK;
    22     else
    23         return FALSE;
    24 }
    25 
    26 int GetQueueLength(SqQueue Q)
    27 {
    28     return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
    29 }
    30 
    31 Status EnQueue(SqQueue *Q, ElemType e)
    32 {
    33     //队列已满
    34     if ((Q->rear + 1) % MAXSIZE == Q->front)
    35         return FALSE;
    36 
    37     Q->data[Q->rear] = e;
    38     Q->rear = (Q->rear + 1) % MAXSIZE; //队尾向后以一个元素,队满后则转移到队列头部
    39     return OK;
    40 }
    41 
    42 Status DeQueue(SqQueue *Q, ElemType *e)
    43 {
    44     if (Q->front == Q->rear)   //队列为空
    45         return FALSE;
    46 
    47     *e = Q->data[Q->front];
    48     Q->front = (Q->front + 1) % MAXSIZE;
    49 
    50     return OK;
    51 }
    52 
    53 Status GetQueueHead(SqQueue *Q, ElemType *e)
    54 {
    55     if (Q->front == Q->rear) //队列为空
    56         return FALSE;
    57     *e = Q->data[Q->front];
    58     return OK;
    59 }
    60 
    61 Status QueueTraverse(SqQueue Q)
    62 {
    63     int index = Q.front;
    64     while ((index + Q.front) != Q.rear)  //遍历完成
    65     {
    66         visit(Q.data[index]);
    67         index = (index + 1) % MAXSIZE;
    68     }
    69 
    70     return OK;
    71 }
    72 
    73 void StartQueue()
    74 {
    75     SqQueue Q;
    76     if (OK == InitQueue(&Q))
    77         printf("
    循环队列初始化成功!
    ");
    78 
    79     for (int i = 1; i <= 10; ++i)
    80         EnQueue(&Q, i * 2);
    81 
    82     printf("
    
    插入数据1-10后的队列长度为: %d
    ", GetQueueLength(Q));
    83 
    84     printf("
    
    队列中的元素是:
    ");
    85     QueueTraverse(Q);
    86 
    87     int e;
    88     DeQueue(&Q, &e);
    89     printf("
    
    队首出列: %d
    ", e);
    90     printf("
    
    队首出列后的队列长度为: %d
    ", GetQueueLength(Q));
    91 
    92     GetQueueHead(&Q, &e);
    93     printf("
    
    队首元素为: %d
    ", e);
    94 
    95     ClearQueue(&Q);
    96     printf("
    
    清空后的队列长度: %d
    ", GetQueueLength(Q));
    97 }

    main.c:

    1 int main()
    2 {
    3     //LinkListStart();
    4     //StackStart();
    5     //LinkStackStart();
    6     StartQueue();
    7     getchar();
    8     return 0;
    9 }
  • 相关阅读:
    elasticsearch(一):JAVA api操作
    elasticsearch(一):安装与配置
    Apache POI导出excel
    Java实现验证码
    Java数据库连接池原理与简易实现
    SpringMvc注解开发
    类型转换的时候,.valueOf()和.parseX(),.Xvalue()的区别
    kmp算法模板
    java 的任意进制间转换(很方便)
    海伦-秦九韶公式(利用三角形的三边求面积)
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/9256360.html
Copyright © 2011-2022 走看看