(实验三)《数据结构》第三章 循环队列与链队列验证

前端之家收集整理的这篇文章主要介绍了(实验三)《数据结构》第三章 循环队列与链队列验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一..实验目的

巩固栈和队列数据结构,学会运用栈和队列。

1.回顾栈和队列的逻辑结构和受限操作特点,栈和队列的物理存储结构和常见操作。

2.学习运用栈和队列的知识来解决实际问题。

3.进一步巩固程序调试方法

4.进一步巩固模板程序设计。

二.实验时间

准备时间为第5周到第6周,具体集中实验时间为6周第2次课。2个学时。

三..实验内容

1.自己选择顺序或链式存储结构,定义一个空栈类,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。

2.自己选择顺序或链式存储结构,定义一个空栈队列,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。

3.编程实现一个十进制数转换成二进制数。要求,要主程序中输出一个10进度数,输出其对应的2进制数序列。

前两题是必做题,第3题是选做题。


2.1、循环队列代码

  1. /**循环队列头文件**/
  2. const int MaxSize=100;
  3. template<class DataType>
  4. class Duilie
  5. {
  6. public:
  7. Duilie();
  8. ~Duilie(){};
  9. void Insert(DataType x);
  10. DataType Getdulie();
  11. DataType Outduilie();
  12. int Empty();
  13. private:
  14. DataType data[MaxSize];
  15. int front,rear;
  16. int flag;
  17. };
  18.  
  19. /**循环队列源文件**/
  20. #include<iostream>
  21. using namespace std;
  22. #include"Duilie1.h"
  23.  
  24. template<class DataType>
  25. Duilie<DataType>::Duilie()
  26. {
  27. front=rear=MaxSize-1;
  28. }
  29.  
  30. template<class DataType>
  31. void Duilie<DataType>::Insert(DataType x)
  32. {
  33. if(flag==1&&front==rear)throw"Over" ;
  34. flag=1;
  35. rear=(rear+1)%MaxSize;
  36. data[rear]=x;
  37. }
  38.  
  39. template<class DataType>
  40. DataType Duilie<DataType>::Getdulie()
  41. {
  42. if(rear==front)throw"Under";
  43. int i=(front+1)%MaxSize;
  44. return data[i];
  45. }
  46.  
  47. template<class DataType>
  48. DataType Duilie<DataType>::Outduilie()
  49. {
  50. if(rear==front&&flag==0)throw"Under";
  51. flag=0;
  52. front=(front+1)%MaxSize;
  53. return data[front];
  54. }
  55.  
  56. template<class DataType>
  57. int Duilie<DataType>::Empty()
  58. {
  59. if(front==rear)
  60. return 1;
  61. else
  62. return 0;
  63. }
  64.  
  65. /**循环队列主函数文件**/
  66. #include<iostream>
  67. using namespace std;
  68. #include"Duilie1.cpp"
  69.  
  70. void main()
  71. {
  72. Duilie<int> D;
  73. if(D.Empty())
  74. cout<<"Empty"<<endl;
  75. else
  76. cout<<"Not Empty"<<endl;
  77. cout<<"Insert the number 12 and 18!"<<endl;
  78. D.Insert(12);
  79. D.Insert(18);
  80. cout<<"The head is:"<<D.Getdulie()<<endl;
  81. cout<<"Let the head-number out!"<<endl;
  82. D.Outduilie();
  83. cout<<"Then the new head is:"<<D.Getdulie()<<endl;
  84. if(D.Empty())
  85. cout<<"Empty"<<endl;
  86. else
  87. cout<<"Not Empty"<<endl;
  88. }

结果如下:

2.2,链队列

  1. /***头文件***/
  2. template<class DataType>
  3. struct Node
  4. {
  5. DataType data;
  6. Node<DataType> *next;
  7. };
  8.  
  9. template<class DataType>
  10. class Liandui
  11. {
  12. public:
  13. Liandui();
  14. ~Liandui();
  15. void Insert(DataType x);
  16. DataType OutLian();
  17. DataType GetLian();
  18. int Empty();
  19. private:
  20. Node<DataType> *front,*rear;
  21. };
  22.  
  23. /***源文件***/
  24. #include"liandui.h"
  25.  
  26. template<class DataType>
  27. Liandui<DataType>::Liandui()
  28. {
  29. Node<DataType> * s=NULL;
  30. s=new Node<DataType>;
  31. s->next=NULL;
  32. rear=s;
  33. front=s;
  34. }
  35.  
  36. template<class DataType>
  37. Liandui<DataType>::~Liandui()
  38. {
  39. Node<DataType>*p=NULL;
  40. while(front!=NULL)
  41. {
  42. p=front->next;
  43. delete front;
  44. front=p;
  45. }
  46. }
  47.  
  48. template<class DataType>
  49. void Liandui<DataType>::Insert(DataType x)
  50. {
  51. Node<DataType>*k=NULL;
  52. k=new Node<DataType>;
  53. k->data=x;
  54. k->next=NULL;
  55. rear->next=k;
  56. rear=k;
  57. }
  58.  
  59. template<class DataType>
  60. DataType Liandui<DataType>::OutLian()
  61. {
  62. Node<DataType>*m=NULL;
  63. m=new Node<DataType>;
  64. if(rear==front)throw"下溢";
  65. m=front->next;
  66. int x;
  67. x=m->data;
  68. front->next=m->next;
  69. if(m->next==NULL)rear=front;
  70. delete m;
  71. return x;
  72. }
  73.  
  74. template<class DataType>
  75. DataType Liandui<DataType>::GetLian()
  76. {
  77. if(front!=rear)
  78. return front->next->data;
  79. }
  80.  
  81. template<class DataType>
  82. int Liandui<DataType>::Empty()
  83. {
  84. if(front==rear)
  85. return 1;
  86. else
  87. return 0;
  88. }
  89.  
  90.  
  91. /***主函数***/
  92. #include<iostream>
  93. using namespace std;
  94. #include"liandui.cpp"
  95.  
  96. void main()
  97. {
  98. Liandui<int> L;
  99. if(L.Empty())
  100. cout<<"队列为空"<<endl;
  101. else
  102. cout<<"队列非空"<<endl;
  103. cout<<"进行插入数值12、18和21操作"<<endl;
  104. try{
  105. L.Insert(12);
  106. L.Insert(18);
  107. L.Insert(21);}
  108. catch(char *q)
  109. {
  110. cout<<q<<endl;
  111. }
  112. cout<<"队头元素为:"<<L.GetLian()<<endl;
  113. cout<<"执行第一次出队操作"<<endl;
  114. try{
  115. L.OutLian();
  116. }
  117. catch(char *q)
  118. {
  119. cout<<q<<endl;}
  120. cout<<"此时队头元素为:"<<L.GetLian()<<endl;
  121. cout<<"判断队列是否为空:";
  122. if(L.Empty())
  123. cout<<"队列为空"<<endl;
  124. else
  125. cout<<"队列非空"<<endl;
  126. }


3、十进制转化为二进制数,代码如下:

  1. <span style="font-size:14px;">#include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. int x,z,flag=1;
  7. int i=0;
  8. int a[100];
  9. cout<<"请输入一个十进制的数:";
  10. cin>>x;
  11. if(x<=0)
  12. exit(0);
  13. while(x>0){
  14. z=x%2;
  15. x=x/2;
  16. a[i]=z;
  17. i++;
  18. };
  19. cout<<"转换后的数值为:";
  20. for(i=100;i>=0;i--)
  21. if(a[i]==0||a[i]==1)
  22. cout<<a[i];
  23. cout<<endl;
  24. }
  25. </span>
结果如下:


总结:

万变不离其宗,都是算法的问题,掌握好线性表很重要,第二章要花多一点时间,代码要多看几遍,而且通过选做题反映出了我自己一些计算机的基础知识例如数制之间的转换已经忘得差不多了

猜你在找的数据结构相关文章