• 两种常用的队列


    与栈相反,队列是一种先进先出的线性表。它只允许在表的一端进行插入,而在另一端删除元素。

    和线性表类似,队列也可以有两种存储表示。

    用链表表示的队列简称链队列。下面是带头结点的单链队列的实现

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 
     4 typedef char QElemType;
     5 //单链队列节点 
     6 typedef struct QNode
     7 {
     8     QElemType data;
     9     struct QNode *next;
    10 }QNode,*QueuePtr;
    11 //单链队列 
    12 typedef struct
    13 {
    14     QueuePtr front;   //队头指针 
    15     QueuePtr rear;    //队尾指针 
    16 }LinkQueue;
    17 
    18 //初始化单链队列 
    19 void InitQueue(LinkQueue &Q)
    20 {
    21     Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
    22     Q.front->next=NULL;
    23 }
    24 
    25 //销毁单链队列 
    26 void DestroyQueue(LinkQueue &Q)
    27 {
    28     while(Q.front)
    29     {
    30         Q.rear=Q.front->next;
    31         free(Q.front);
    32         Q.front=Q.rear;
    33     }
    34 }
    35 
    36 //入队列
    37 void EnQueue(LinkQueue &Q,QElemType e)
    38 {
    39     QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
    40     p->data=e;
    41     p->next=NULL;
    42     Q.rear->next=p;
    43     Q.rear=p;
    44 } 
    45 
    46 //出队列
    47 bool DeQueue(LinkQueue &Q,QElemType &e)
    48 {
    49     if(Q.front==Q.rear)
    50         return false;
    51     QueuePtr p=Q.front->next;
    52     e=p->data;
    53     Q.front->next=p->next;
    54     //如果队列只有一个元素 
    55     if(p==Q.rear)
    56         Q.front=Q.rear;
    57     free(p);
    58 } 
    View Code

    队列的顺序表示一般实现为循环队列。下面是循环队列的实现

    因为Q.front=Q.rear不能区分队列是空还是满,所以下面采用少用一个元素空间来解决这个问题。

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 #define MAXQSIZE 100   //最大队列长度
     4 
     5 typedef char QElemType;
     6 //定义循环队列
     7 typedef struct
     8 {
     9     QElemType *base;   //
    10     int front;         //头指针,若队列非空,指向队列头元素 
    11     int rear;          //尾指针,若队列非空,指向队列尾元素的下一个位置 
    12 }SqQueue;
    13 
    14 //初始化
    15 void InitQueue(SqQueue &Q)
    16 {
    17     Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
    18     Q.front=Q.rear=0;
    19 } 
    20 
    21 //获取队列的长度
    22 int QueueLength(SqQueue Q)
    23 {
    24     return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
    25 } 
    26 
    27 //入队列
    28 bool EnQueue(SqQueue &Q,QElemType e)
    29 {
    30     //判断队列是否已经满了 
    31     if((Q.rear+1)%MAXQSIZE==Q.front)
    32         return false;
    33     Q.base[Q.rear]=e;
    34     Q.rear=(Q.rear+1)%MAXQSIZE;
    35     return true;
    36 } 
    37 
    38 //出队列
    39 bool DeQueue(SqQueue &Q,QElemType &e)
    40 {
    41     //判断队列是否为空
    42     if(Q.rear==Q.front)
    43         return false;
    44     e=Q.base[Q.front];
    45     Q.front=(Q.front+1)%MAXQSIZE;
    46     return true; 
    47 } 
    View Code
  • 相关阅读:
    使用新建项目,引用底层库,运行时提示http://www.xinxizhan.cn/none-authorization.html。无授权解决方法
    treeviewhelper用法,找child的UIElement
    用#FFFF2222的string生成color
    DataTemplate.LoadContent Method将resource中的datatemplate转换为UIElement,可以用于对象添加
    Server.Mappath
    回调函数
    用where进行条件查询
    NPOI用法。
    [转].tostring设置格式。C# tostring 格式化输出
    【转】灵活运用 SQL SERVER FOR XML PATH。用于方便处理生成视图
  • 原文地址:https://www.cnblogs.com/runnyu/p/4705153.html
Copyright © 2020-2023  润新知