当前位置:   article > 正文

[数据结构笔记] 队列_队列的逻辑结构是什么

队列的逻辑结构是什么

一、队列的逻辑结构

队列:只允许在一段进行插入操作,而另一端进行删除操作的线性表

空队列:不含任何数据元素的队列。

允许插入(也称入队、进队)的一端称为队尾,允许删除(也称出队)的一端成为队头。

队列的操作特性:先进先出

二、顺序队列的存储结构及实现

确定不同的队空、队满的判定条件:

方法一:附设一个存储队列中元素个数的变量num,当num=0时队空,当num=Queuesize时为队满;

方法二:修改队满条件,浪费一个元素空间,队满时数组中只有一个空闲单元。

方法三:设置标志flag,当front=rear且flag=0时为队空,当front=rear且flag=1时为队满。

队满的条件:(rear+1) mod Queuesize==front

循环队列类的声明:

  1. const int QueueSize=100;
  2. template <class T>
  3. class CirQueue{
  4. public:
  5. CirQueue( );
  6. ~ CirQueue( );
  7. void EnQueue(T x);
  8. T DeQueue( );
  9. T GetQueue( );
  10. bool Empty( ){
  11. if (rear==front) return true;
  12. return false;
  13. };
  14. private:
  15. T data[QueueSize];
  16. int front, rear;
  17. };

1.入队:

  1. template <class T>
  2. void CirQueue<T>::EnQueue(T x)
  3. {
  4. if ((rear+1) % QueueSize ==front) throw "上溢";
  5. rear=(rear+1) % QueueSize;
  6. data[rear]=x;
  7. }

2.出队:

  1. template <class T>
  2. T CirQueue<T>::DeQueue( )
  3. {
  4. if (rear==front) throw "下溢";
  5. front=(front+1) % QueueSize;
  6. return data[front];
  7. }

3.读队头元素:

  1. template <class T>
  2. T CirQueue<T>::GetQueue( )
  3. {
  4. if (rear==front) throw "下溢";
  5. i=(front+1) % QueueSize;
  6. return data[i];
  7. }

4.返回队列长度:

  1. template <class T>
  2. int CirQueue<T>::GetLength( )
  3. {
  4. if (rear==front) throw "下溢";
  5. len=(rear-front+ QueueSize) % QueueSize;
  6. return len;
  7. }

二、队列的链式存储结构及实现

链队列类的声明:

  1. template <class T>
  2. class LinkQueue {
  3. public:
  4. LinkQueue();
  5. ~LinkQueue();
  6. void EnQueue(T x);
  7. T DeQueue();
  8. T GetQueue();
  9. bool Empty();
  10. private:
  11. Node<T> *front, *rear;
  12. };

1.链队列构造函数

  1. template <class T>
  2. LinkQueue<T>::LinkQueue() {
  3. front=new Node<T>;
  4. front->next=NULL;
  5. rear=front;
  6. }

2. 入队

  1. template <class T>
  2. void LinkQueue<T>::EnQueue(T x) {
  3. s=new Node<T>;
  4. s->data=x;
  5. s->next=NULL;
  6. rear->next=s;
  7. rear=s;
  8. }

3. 出队

  1. template <class T>
  2. T LinkQueue<T>::DeQueue() {
  3. if (rear==front) throw "下溢";
  4. p=front->next;
  5. x=p->data;
  6. front->next=p->next;
  7. delete p;
  8. if (front->next==NULL) rear=front;
  9. return x;
  10. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/928339
推荐阅读
相关标签
  

闽ICP备14008679号