zoukankan      html  css  js  c++  java
  • 队列基础知识

    利用标志位tag实现顺序循环队列:

      1 /* */
      2 # include <stdio.h>
      3 # include <math.h>
      4 # include <stdlib.h>
      5 # define QUEUESIZE 100
      6 # define MAXSIZE 100
      7 typedef int DataType;
      8 
      9 typedef struct
     10 {
     11     int queue[QUEUESIZE];
     12     int front, rear;
     13     int tag;
     14 }SCQueue;
     15 
     16 void PrintData( DataType e);
     17 int CheckType(DataType e);
     18 void InitQueue(SCQueue *SCQ)///初始化为空队列
     19 {
     20     SCQ->front = SCQ->rear = 0;///对头指针和队尾指针都置为0
     21     SCQ->tag = 0;///标志置为0;
     22 }
     23 
     24 int QueueEmpty(SCQueue SCQ)///判断顺序循环队列是否为空,是则返回1,否则返回0
     25 {
     26     if( SCQ.front==SCQ.rear && SCQ.tag==0 )///队头指针和队尾指针都为0,且标志位为0表示队列已空
     27     {
     28         return 1;
     29     }
     30     else
     31     {
     32         return 0;
     33     }
     34 }
     35 
     36 int EnQueue(SCQueue *SCQ, DataType e)///将元素e插入顺序循环队列SQ中, 插入成功返回1, 否则返回0
     37 {
     38     if( SCQ->front==SCQ->rear && SCQ->tag==1 )
     39     {
     40         printf("顺序循环队列已满, 不能如队!");
     41         return 0;
     42     }
     43     else
     44     {
     45         SCQ->queue[SCQ->rear] = e;
     46         SCQ->rear = SCQ -> rear + 1;
     47         SCQ->tag = 1;
     48         return 1;
     49     }
     50 }
     51 
     52 int DeQueue(SCQueue *SCQ, DataType *e)///删除循环队列中的队头元素, 并将该元素赋值给e, 删除成功返回1, 否则返回0
     53 {
     54     if( QueueEmpty(*SCQ) )
     55     {
     56         printf("顺序循环队列已经是空列, 不能再进行出队操作!");
     57         return 0;
     58     }
     59     else
     60     {
     61         *e = SCQ->queue[SCQ->front];
     62         SCQ->front = SCQ->front+1;
     63         SCQ->tag = 0;
     64         return 1;
     65     }
     66 }
     67 
     68 void DisplayQueue(SCQueue SCQ)///输出顺序循环队列中的元素
     69 {
     70     int i;
     71     if( QueueEmpty(SCQ) )
     72     {
     73         return ;
     74     }
     75     if( SCQ.front<SCQ.rear )
     76     {
     77         for( i=SCQ.front; i<SCQ.rear; i++)
     78         {
     79             printf("%4d", SCQ.queue[i]);
     80         }
     81     }
     82     else
     83     {
     84         for( i=SCQ.front; i<SCQ.rear+QUEUESIZE; i++ )
     85         {
     86             printf("%4d", SCQ.queue[i%QUEUESIZE]);
     87         }
     88     }
     89     printf("
    ");
     90     return ;
     91 }
     92 
     93 int main()
     94 {
     95     SCQueue Q;
     96     int e;
     97     int a[] = {1, 2, 3, 4}, i;
     98     InitQueue(&Q);
     99     for( i=0; i<sizeof(a)/sizeof(a[0]); i++ )
    100     {
    101         EnQueue(&Q, a[i]);
    102     }
    103     printf("队列中元素: ");
    104     DisplayQueue(Q);
    105     i= 0;
    106     while( !QueueEmpty(Q) )
    107     {
    108         printf("队头元素第%d次出队
    ", ++i);
    109         DeQueue(&Q, &e);
    110         printf("出队的元素: ");
    111         printf("%d
    ", e);
    112     }
    113 }
    114 
    115 void PrintData(DataType e)///数据的输出(为了使调用函数不需要格式控制符
    116 {
    117     char n;
    118     n = CheckType(e);
    119     switch(n)
    120     {
    121     case 1:
    122         printf("%4c
    ", e);
    123         break;
    124     case 2:
    125         printf("%4d
    ", e);
    126         break;
    127     case 3:
    128         printf("%8.2f
    ", e);
    129         break;
    130     }
    131 }
    132 
    133 int CheckType(DataType e)///判断e是浮点数,整数还是字母字符
    134 {
    135     char str[MAXSIZE];
    136     int a;
    137     float b;
    138     if( fabs(e-(int)e>1e-6))///是浮点数
    139     {
    140         return 3;
    141     }
    142     if( e>='A' && e<='Z' || e>='a' && e<='z' )///是字母字符
    143         {
    144             return 1;
    145         }
    146         else///是整数
    147         {
    148             return 2;
    149         }
    150 }
  • 相关阅读:
    WPF Prism8.x源码解析-IDialogService
    .NET 中的正则表达式最佳做法(官方转载)
    .NET 5 中操作注册表方法
    在 .NET 5 及更高版本中比较字符串时的行为更改(官方转载)
    有关比较 .NET 中字符串的最佳做法(官方转载)
    C#创建Windows服务
    [log4j]log4j简单配置
    python基础知识之字符编码与转换
    python基础知识之集合
    python基础知识之字典的操作
  • 原文地址:https://www.cnblogs.com/wsy107316/p/10713430.html
Copyright © 2011-2022 走看看