zoukankan      html  css  js  c++  java
  • C ~ 链式队列与循环队列

          此处的链式与循环队列可以应用于BFS和树的层序遍历。下面是对其结构和基本操作的程序描述。

    1、循环队列

        解决循环队列的队空和队满的方法:

        [1].增加一个参数count,用来记录数组中当前元素的个数;

        [2].为避免队空和满两状态混淆,少用一个存储空间,也就是数组最后一个存数空间不用,(rear+1)% QSIZE= front 时, 队满;

           a)判断是否为空队列:front==rearb)判断队列是否已满:front=(rear+1)%size 

     1 typedef struct{      //队列结构定义
     2     int front;
     3     int rear;
     4     int count;  //队列元素计数
     5     int key[QSIZE];
     6 }BFSQueue;
     7 
     8 void InitBFSQueue(BFSQueue *BFSQ)  //队列初始化
     9 {
    10     BFSQ->front=0;  //front指向队列第一个元素
    11     BFSQ->rear=0;   //rear指向队列中下一个空位
    12     BFSQ->count=0;
    13 }
    14 int EmptyBFSQueue(BFSQueue *BFSQ) //
    15 {
    16     return BFSQ->count==0;
    17 }
    18 int FullBFSQueue(BFSQueue *BFSQ)  //
    19 {
    20     return BFSQ->count==QSIZE;
    21 }
    22 
    23 void AddBFSQueue(BFSQueue *BFSQ,int num)  //插入
    24 {
    25     if(!FullBFSQueue(BFSQ))
    26     {
    27         BFSQ->count++;
    28         BFSQ->key[BFSQ->rear]=num;
    29         BFSQ->rear=(BFSQ->rear+1) % QSIZE;
    30     }
    31 }
    32 
    33 int DelBFSQueue(BFSQueue *BFSQ)  //删除
    34 {
    35     int temp;
    36     if(!EmptyBFSQueue(BFSQ))
    37     {
    38         BFSQ->count--;
    39         temp=BFSQ->key[BFSQ->front];
    40         BFSQ->front=(BFSQ->front+1) % QSIZE;
    41         return temp;
    42     }
    43     else
    44         return NULL;
    45 }
    46 /******************************************************************/

    2、链式队列

    
    
     1 typedef struct QNode{  // 队列元素结构  
     2     int key;
     3     struct QNode *next;
     4 }QNode;
     5 typedef struct{  // 队列结构 
     6     QNode *front;
     7     QNode *rear;
     8     int count;
     9 }BFSQueue;
    10 
    11 void InitBFSQueue(BFSQueue *BFSQ)  //队列初始化
    12 {
    13     BFSQ->front=NULL;  
    14     BFSQ->rear=NULL; 
    15     BFSQ->count=0;
    16 }
    17 int EmptyBFSQueue(BFSQueue *BFSQ) //
    18 {
    19     return BFSQ->count==0;
    20 }
    21 
    22 void AddBFSQueue(BFSQueue *BFSQ,int num)  //插入
    23 {
    24     QNode *np=(QNode *)malloc(sizeof(QNode));
    25     np->key=num;
    26     np->next=NULL;
    27       //BFSQ->count++;
    28 
    29     if(!EmptyBFSQueue(BFSQ))  // 队列非空
    30     {        
    31         BFSQ->rear->next=np;
    32         BFSQ->rear=np;
    33     }
    34     else                      // 队列空
    35     {
    36         BFSQ->rear=np;
    37         BFSQ->front=np;
    38     }
    39     BFSQ->count++;
    40 }
    41 int DelBFSQueue(BFSQueue *BFSQ)  //删除
    42 {
    43     int temp;
    44     QNode *tp=(QNode *)malloc(sizeof(QNode));
    45     if(!EmptyBFSQueue(BFSQ)) //队列非空
    46     {
    47           //BFSQ->count--;  //////注意,必须在队列判定之后增加或减小count的值///////
    48         tp=BFSQ->front;
    49         temp=tp->key;
    50         if(BFSQ->count==1)  //出队后队列为空
    51         {
    52             BFSQ->rear=NULL;
    53             BFSQ->front=NULL;
    54         }
    55         else  //出队后队列非空
    56         {
    57             BFSQ->front=tp->next;
    58         }        
    59         BFSQ->count--;
    60         free(tp);  //释放暂存指针
    61 
    62         return temp;
    63     }
    64     else
    65         return NULL;
    66 }
    ---  纵使山重水复,亦会柳暗花明   sunqh1991@163.com   欢迎关注,互相交流
  • 相关阅读:
    2018年7月28日杂记:迎来大三
    Spring第一课:基于XML装配bean(四),三种实例化方式:默认构造、静态工厂、实例工厂
    在Spring中使用静态工厂时发生的无法得到对象的问题
    队列的实现——java
    队列的实现——c++
    栈的实现——java
    栈的实现——c++
    双向链表的实现——java
    双向链表的实现——c++
    C++类模板 template <class T>
  • 原文地址:https://www.cnblogs.com/wjcx-sqh/p/5929927.html
Copyright © 2011-2022 走看看