队列的链接存储结构——链队列 图解和代码实现

来源:互联网 发布:淘宝上怎么卖二手东西 编辑:程序博客网 时间:2024/06/11 01:16

转自:http://blog.csdn.net/ggxxkkll/article/details/8662954

队列的链接存储结构——链队列

 图解:

 

 

LinkQueue.h

[cpp] view plaincopy
  1. //LinkQueue.h  
  2. #ifndef LINKQUEUE_H  
  3. #define LINKQUEUE_H  
  4.   
  5. template <class T>  
  6. struct Node  
  7. {  
  8.       T data;  
  9.       Node<T> *next;  //此处<T>也可以省略  
  10. };  
  11.   
  12. template <class T>  
  13. class LinkQueue  
  14. {  
  15. public:  
  16.     LinkQueue( );          //构造函数,初始化一个空的链队列  
  17.     ~LinkQueue( );      //析构函数,释放链队列中各结点的存储空间  
  18.     void EnQueue(T x);  //将元素x入队  
  19.     T DeQueue( );       //将队头元素出队  
  20.     T GetQueue( );     //取链队列的队头元素  
  21.     bool Empty( );     //判断链队列是否为空  
  22. private:  
  23.     Node<T> *front, *rear;  //队头和队尾指针,分别指向头结点和终端结点  
  24. };  
  25.   
  26. #endif;  


 

 

LinkQueue.cpp

[cpp] view plaincopy
  1. //LinkQueue.cpp  
  2. #include "LinkQueue.h"  
  3.   
  4. /* 
  5.  * 前置条件:队列不存在 
  6.  * 输    入:无 
  7.  * 功    能:初始化队列 
  8.  * 输    出:无 
  9.  * 后置条件:创建一个空队列 
  10.  */  
  11.   
  12. template <class T>  
  13. LinkQueue<T>::LinkQueue( )  
  14. {  
  15.     Node <T> *s;  
  16.     s=new Node<T>;  
  17.     s->next=NULL;  
  18.     front=rear=s;  
  19. }  
  20.   
  21. /* 
  22.  * 前置条件:队列存在 
  23.  * 输    入:无 
  24.  * 功    能:销毁队列 
  25.  * 输    出:无 
  26.  * 后置条件:释放队列所占用的存储空间 
  27.  */  
  28.   
  29. template <class T>  
  30. LinkQueue<T>::~LinkQueue( )  
  31. {  
  32.     while(front)  
  33.     {  
  34.         Node <T> *p;  
  35.         p=front->next;  
  36.         delete front;  
  37.         front=p;  
  38.     }  
  39. }  
  40.   
  41. /* 
  42.  * 前置条件:队列已存在 
  43.  * 输    入:元素值s 
  44.  * 功    能:在队尾插入一个元素 
  45.  * 输    出:无 
  46.  * 后置条件:如果插入成功,队尾增加了一个元素 
  47.  */  
  48.   
  49. template <class T>   
  50. void LinkQueue<T>::EnQueue(T x)  
  51. {  
  52.     Node<T> *s;  
  53.     s=new Node<T>;   
  54.     s->data=x;          //申请一个数据域为x的结点s  
  55.     s->next=NULL;  
  56.     rear->next=s;       //将结点s插入到队尾  
  57.     rear=s;  
  58. }  
  59.   
  60. /* 
  61.  * 前置条件:队列已存在 
  62.  * 输    入:无 
  63.  * 功    能:删除队头元素 
  64.  * 输    出:如果删除成功,返回被删元素值,否则,抛出删除异常 
  65.  * 后置条件:如果删除成功,队头减少了一个元素 
  66.  */  
  67.   
  68. template <class T>  
  69. T LinkQueue<T>::DeQueue()  
  70. {      
  71.     Node <T> *p; int x;  
  72.     if (rear==front) throw "下溢";  
  73.     p=front->next;   
  74.     x=p->data;                       //暂存队头元素  
  75.     front->next=p->next;             //将队头元素所在结点摘链  
  76.     if (p->next==NULL) rear=front;   //判断出队前队列长度是否为1  
  77.     delete p;  
  78.     return x;  
  79. }  
  80.   
  81. /* 
  82.  * 前置条件:队列已存在 
  83.  * 输    入:无 
  84.  * 功    能:读取队头元素 
  85.  * 输    出:若队列不空,返回队头元素 
  86.  * 后置条件:队列不变 
  87.  */  
  88.   
  89. template <class T>   
  90. T LinkQueue<T>::GetQueue()  
  91. {  
  92.     if (front!=rear)   
  93.         return front->next->data;  
  94. }  
  95.   
  96. /* 
  97.  * 前置条件:队列已存在 
  98.  * 输    入:无 
  99.  * 功    能:判断队列是否为空 
  100.  * 输    出:如果队列为空,返回1,否则,返回0 
  101.  * 后置条件:队列不变 
  102.  */  
  103.   
  104. template <class T>   
  105. bool LinkQueue<T>::Empty( )  
  106. {  
  107.     if(front==rear)   
  108.         return 1;  
  109.     else   
  110.         return 0;  
  111. }  

0 0
原创粉丝点击