zoukankan      html  css  js  c++  java
  • 数据结构第十一篇——链队列

    链式存储的队列称为链队列。和链栈类似,用单链表来实现链队,根据队列的FIFO原则,为了操作上的方便,分别需要一个头指针和尾指针。队头在链头,队尾在链尾。链式队列在进队时无队满问题,但有队空问题。队空条件为front->next==NULL。

    链队描述如下:

     1 struct Qnode
     2 {
     3     Data data;
     4     QNode *next;
     5 };
     6 
     7 class Queue
     8 {
     9     QNode *front;
    10     QNode *rear;
    11     
    12     public:
    13         Queue()
    14         {
    15             rear=front=new QNode;
    16             front->next=NULL;
    17         }
    18         ~Queur()
    19         {
    20             ClearQueue();
    21             delete front;
    22         }
    23         
    24         void EnQueue(Data item);    //入队 
    25         Data DeQueue();            //出队 
    26         Data GetFront();            //取队头元素值 
    27         void ClearQueue();            //清空队列 
    28         bool IsEmpty();                //判断空否 
    29         int  Length();                 //求元素个数 
    30 }

    链队的操作算法描述如下:

     1 //入队操作
     2 void Queue::EnQueue(Data item)
     3 {
     4     rear->=next=new QNode;
     5     rear=rear->next;
     6     rear->data=item;
     7     rear->next=NULL: 
     8 }
     9 
    10 //出队操作
    11 Data Queue::DeQueue()
    12 {
    13     QNode *p;
    14     if(!IsEmpty())
    15     {
    16         p=front->next;
    17         Data retvalue=p->data;
    18         front->next=p->next;
    19         delete p;
    20         
    21         if(front->next==NULL)
    22         real=front;
    23         return retvalue;
    24     }
    25     else
    26     {
    27         cout<<"the Queue is empty"<<endl;
    28         exit(0);
    29     }
    30 } 
    31 
    32 //取队头元素的值
    33 Data Queue::GetFront()
    34 {
    35     if(!IsEmpty())
    36     return front->next->data;
    37     else
    38     {
    39         cout<<"the Queue is empty"<<endl;
    40         exit(0);
    41     }
    42 }
    43 
    44 //清空队列
    45 void Queue::ClearQueue()
    46 {
    47     rear=front->next;
    48     while(rear!=NULL)
    49     {
    50         front->next=rear->next;
    51         delete rear;
    52         rear=front->next;
    53     }
    54     rear=front;
    55 } 
    56 
    57 //判断空否 
    58 bool Queue::IsEmpty()
    59 {
    60     return (front->next==NULL);
    61 }
    62 
    63 //计算队列长度 
    64 int Queue::Length()
    65 {
    66     int cnt=0;
    67     QNode *p=pront->next;
    68     while(p)
    69     {
    70         cnt++;
    71         p=p->next;
    72     }
    73     return cnt;
    74 }

     优先级队列

      队列是以FIFO顺序访问元素的数据结构,它从中删除“最老的”元素。实际应用中常需要另外一种队列,它删除优先级最高的元素。这种结构被称为优先级队列,主要有插入和删除操作。

      具体实现不再细说,和普通队列仅在删除操作有些许差别。

  • 相关阅读:
    常见概念
    网站扒取
    图书管理系统易错点整理
    CSIC_716_2020104【Django入门---静态文件、request对象方法、ORM增删改查】
    CSIC_716_2020103【Django入门---HttpResponse、render、redirect】
    CSIC_716_2020102【前端框架Bootstrap】
    CSIC_716_2020101【???】
    CSIC_716_20191231【jQuery基础入门】
    CSIC_716_20191230【前端的BOM与DOM】
    ss
  • 原文地址:https://www.cnblogs.com/tenjl-exv/p/7619963.html
Copyright © 2011-2022 走看看