我的简单链式队列

来源:互联网 发布:pc蛋蛋牛人算法2017 编辑:程序博客网 时间:2024/05/21 15:11

我的简单链式队列


 最近老师要我们写一个二叉树的结构,并且用程序将它实现。我想这是一个好时机,我就开始写一个二叉树了。当然,我写二叉树的意图不是要给老师看的,而是想要为自己的游戏编程添砖加瓦。所以我从星期一的下午开始就开始研究二叉树的结构,一直到了今天。还没有做完。
 今天研究二叉树的层序遍历的时候,我一直想不出怎样遍历。后来上网查了资料,它必须使用队列这种结构。我想到了自己以前写的队列。但是打开电脑一看,竟然是顺序存储形式的队列。这怎么行呢?如果是顺序的话,肯定要浪费空间的,而且操作比较繁琐,需要臆想一个循环的队列,并且需要考虑是否“假”溢出的问题。基于以上的顾虑,我还是重新写一个我自己的队列,它的存储结构是链式的,这样的话会缓解上述问题。并且更容易理解。说做就做,我今天花了一个小时开始调试了我的简单队列。下面就是我的队列的代码:

Code:
  1. #ifndef _JCHAINQUEUE_H_   
  2. #define _JCHAINQUEUE_H_   
  3.   
  4. // 链式队列节点结构体   
  5. template <typename CustomType>   
  6. struct JQueueNode   
  7. {   
  8.  JQueueNode():link(0){}// 默认构造函数   
  9.  ~JQueueNode(){}// 默认析构函数   
  10.  CustomType elem;   
  11.  JQueueNode* link;   
  12. };   
  13.   
  14. // 链式队列类   
  15. template <typename CustomType>   
  16. class JChainQueue   
  17. {   
  18. public:   
  19.  JChainQueue():front(0), rear(0){}// 默认构造函数   
  20.  ~JChainQueue(){}// 默认析构函数   
  21.  bool EnterQueue( CustomType obj );// 将元素压入队列   
  22.  CustomType DeleteQueue( void );// 将队列头元素弹出   
  23.  bool IsEmpty( void );// 判断队列是否为空   
  24.  CustomType GetFront( void );// 取出队列头元素   
  25. private:   
  26.  JQueueNode<CustomType>* front;   
  27.  JQueueNode<CustomType>* rear;   
  28. };   
  29.   
  30. // 将元素压入队列   
  31. template <typename CustomType>   
  32. bool JChainQueue<CustomType>::EnterQueue( CustomType obj )   
  33. {   
  34.  if ( rear == 0 )   
  35.   front = rear = new JQueueNode<CustomType>;// 初始化   
  36.  rear->elem = obj;   
  37.  rear->link = new JQueueNode<CustomType>;   
  38.  rear = rear->link;   
  39.  return true;   
  40. }   
  41.   
  42. // 将队列头元素弹出   
  43. template <typename CustomType>   
  44. CustomType JChainQueue<CustomType>::DeleteQueue( void )   
  45. {   
  46.  JQueueNode<CustomType> temp; 
  47.  if ( IsEmpty() ) return temp.elem;  // 加入了安全的措施(更新于10月10日21:34) 
  48.  temp.elem = front->elem;   
  49.  temp.link = front->link;   
  50.  delete front;   
  51.  front = temp.link;   
  52.  return temp.elem;   
  53. }   
  54.   
  55. // 判断队列是否为空   
  56. template <typename CustomType>   
  57. bool JChainQueue<CustomType>::IsEmpty( void )   
  58. {   
  59.  if ( front == rear )   
  60.   return true;   
  61.  else return false;   
  62. }   
  63.   
  64. // 取出队列头元素   
  65. template <typename CustomType>   
  66. CustomType JChainQueue<CustomType>::GetFront( void )   
  67. {   
  68.  return front->elem;   
  69. }   
  70. #endif   
  71.   

 测试一下,我使用了两个实例,一个是用最简单的结构“char”型来对其实例化,另外一个稍微复杂些,使用了一个结构体来验证。经过一步又一步的调试,我终于得出了比较稳定的链式队列结构,或者说我的队列健壮性(鲁棒性)还是不错的。
 下面是我的测试:
 

Code:
  1. #include<iostream>   
  2. #include"JChainQueue.h"   
  3. using namespace std;   
  4.   
  5. // 测试②   
  6. struct stCustom   
  7. {   
  8.   
  9.  int a;   
  10.  char b[20];   
  11.  unsigned long c;   
  12. };   
  13.   
  14.   
  15. int main( int argc, char** argv)   
  16. {   
  17.  /*--------------------测试①------------------------  
  18.  JChainQueue<char> temporary;  
  19.  char a = 'A', b = 'B', c = 'C';  
  20.  temporary.EnterQueue( a );  
  21.  temporary.EnterQueue( b );  
  22.  cout<<"出队列的是:"<<temporary.DeleteQueue();  
  23.  cout<<"现在队列头是:"<<temporary.GetFront();  
  24.  ----------------------------------------------------*/  
  25.   
  26.  // 测试②   
  27.  JChainQueue<stCustom> temporary2;   
  28.  stCustom obj[3] =   
  29.  {   
  30.   1, "这个好!", 31415926,   
  31.   2, "这个很好!", 27145082,   
  32.   3, "这个非常好!", 4529392,   
  33.  };   
  34.  temporary2.EnterQueue( obj[0] );   
  35.  temporary2.EnterQueue( obj[1] );   
  36.  cout<<"出队列的是:"<<temporary2.DeleteQueue().a<<"号/n";   
  37.  cout<<"现在队列头是:"<<temporary2.GetFront().a<<"号,它的内容是:"<<temporary2.GetFront().b<<'/n';   
  38.  return 0;   
  39. }   
  40.   

测试的结果如下图:



有了队列,我们就可以对我们的二叉树进行遍历的操作了,太好了,我要升级了!下一次我就会介绍我制作的二叉树。这个二叉树可是我三天来的成果啊!!!

原创粉丝点击