我的简单链式队列

我的简单链式队列


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

Code:
  1. #ifndef_JCHAINQUEUE_H_
  2. #define_JCHAINQUEUE_H_
  3. //链式队列节点结构体
  4. template<typenameCustomType>
  5. structJQueueNode
  6. {
  7. JQueueNode():link(0){}//默认构造函数
  8. ~JQueueNode(){}//默认析构函数
  9. CustomTypeelem;
  10. JQueueNode*link;
  11. };
  12. //链式队列类
  13. template<typenameCustomType>
  14. classJChainQueue
  15. {
  16. public:
  17. JChainQueue():front(0),rear(0){}//默认构造函数
  18. ~JChainQueue(){}//默认析构函数
  19. boolEnterQueue(CustomTypeobj);//将元素压入队列
  20. CustomTypeDeleteQueue(void);//将队列头元素弹出
  21. boolIsEmpty(void);//判断队列是否为空
  22. CustomTypeGetFront(void);//取出队列头元素
  23. private:
  24. JQueueNode<CustomType>*front;
  25. JQueueNode<CustomType>*rear;
  26. };
  27. //将元素压入队列
  28. template<typenameCustomType>
  29. boolJChainQueue<CustomType>::EnterQueue(CustomTypeobj)
  30. {
  31. if(rear==0)
  32. front=rear=newJQueueNode<CustomType>;//初始化
  33. rear->elem=obj;
  34. rear->link=newJQueueNode<CustomType>;
  35. rear=rear->link;
  36. returntrue;
  37. }
  38. //将队列头元素弹出
  39. template<typenameCustomType>
  40. CustomTypeJChainQueue<CustomType>::DeleteQueue(void)
  41. {
  42. JQueueNode<CustomType>temp;
  43. if ( IsEmpty() ) return temp.elem;// 加入了安全的措施(更新于10月10日21:34)
  44. temp.elem=front->elem;
  45. temp.link=front->link;
  46. deletefront;
  47. front=temp.link;
  48. returntemp.elem;
  49. }
  50. //判断队列是否为空
  51. template<typenameCustomType>
  52. boolJChainQueue<CustomType>::IsEmpty(void)
  53. {
  54. if(front==rear)
  55. returntrue;
  56. elsereturnfalse;
  57. }
  58. //取出队列头元素
  59. template<typenameCustomType>
  60. CustomTypeJChainQueue<CustomType>::GetFront(void)
  61. {
  62. returnfront->elem;
  63. }
  64. #endif

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

Code:
  1. #include<iostream>
  2. #include"JChainQueue.h"
  3. usingnamespacestd;
  4. //测试②
  5. structstCustom
  6. {
  7. inta;
  8. charb[20];
  9. unsignedlongc;
  10. };
  11. intmain(intargc,char**argv)
  12. {
  13. /*--------------------测试①------------------------
  14. JChainQueue<char>temporary;
  15. chara='A',b='B',c='C';
  16. temporary.EnterQueue(a);
  17. temporary.EnterQueue(b);
  18. cout<<"出队列的是:"<<temporary.DeleteQueue();
  19. cout<<"现在队列头是:"<<temporary.GetFront();
  20. ----------------------------------------------------*/
  21. //测试②
  22. JChainQueue<stCustom>temporary2;
  23. stCustomobj[3]=
  24. {
  25. 1,"这个好!",31415926,
  26. 2,"这个很好!",27145082,
  27. 3,"这个非常好!",4529392,
  28. };
  29. temporary2.EnterQueue(obj[0]);
  30. temporary2.EnterQueue(obj[1]);
  31. cout<<"出队列的是:"<<temporary2.DeleteQueue().a<<"号/n";
  32. cout<<"现在队列头是:"<<temporary2.GetFront().a<<"号,它的内容是:"<<temporary2.GetFront().b<<'/n';
  33. return0;
  34. }

测试的结果如下图:



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值