数据结构第三次实验

来源:互联网 发布:淘宝的商家编码是什么 编辑:程序博客网 时间:2024/06/05 15:02

《数据结构》实验三:    栈和队列实验

一..实验目的

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

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

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

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

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

二.实验时间

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

三..实验内容

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

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

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

源代码如下:

头文件:

[cpp] view plaincopyprint?
  1. #ifndef LinkQueue_H  
  2. #define LinkQueue_H  
  3.   
  4. template <class DataType>  
  5. struct Node  
  6. {  
  7.     DataType data;  
  8.     Node<DataType> * next;  
  9. };  
  10.   
  11. template<class DataType>  
  12. class LinkQueue  
  13. {  
  14.     public:  
  15.         LinkQueue();    //构造函数,初始化一个空的链队列  
  16.         ~LinkQueue();   //析构函数,释放链队列中各结点的存储空间  
  17.         void EnQueue(DataType x);    //将元素x入队  
  18.         DataType DeQueue();     //将队头元素出队  
  19.         DataType GetQueue();    //去链队列的队头元素  
  20.         int Empty();            //判断链队列是否为空  
  21.     private:  
  22.         Node<DataType> * front, *rear;  
  23. };  
  24. #endif  

源文件:LinkQueue.cpp

[cpp] view plaincopyprint?
  1. #include"LinkQueue.h"  
  2.   
  3. template<class DataType>  
  4. LinkQueue<DataType>::LinkQueue()  
  5. {  
  6.     Node<DataType> *s = NULL;  
  7.     s= new Node<DataType>;  
  8.     s->next=NULL;  
  9.     front=rear=s;  
  10. }  
  11.   
  12. template<class DataType>  
  13. LinkQueue<DataType>::~LinkQueue()  
  14. {  
  15.     Node<DataType> *p=NULL;  
  16.     while(front!=NULL)  
  17.     {  
  18.         p=front->next;  
  19.         delete front;  
  20.         front=p;  
  21.     }  
  22. }  
  23.   
  24. template<class DataType>  
  25. void LinkQueue<DataType>::EnQueue(DataType x)  
  26. {  
  27.     Node<DataType> *s =NULL;  
  28.     s=new Node<DataType>;  
  29.     s->data=x;  
  30.     s->next=NULL;  
  31.     rear->next=s;  
  32.     rear=s;  
  33. }  
  34.   
  35. template<class DataType>  
  36. DataType LinkQueue<DataType>::DeQueue()  
  37. {  
  38.     Node<DataType> * p=NULL;  
  39.     int x;  
  40.     if(rear==front)throw"下溢";  
  41.     p=front->next;  
  42.     x=p->data;  
  43.     front->next=p->next;  
  44.     if(p->next=NULL) rear = front;  
  45.     delete p;  
  46.     return x;  
  47. }  
  48.   
  49. template<class DataType>  
  50. DataType LinkQueue<DataType>::GetQueue()  
  51. {  
  52.     if(front!=rear)  
  53.         return front->next->data;  
  54. }  
  55.   
  56. template<class DataType>  
  57. int LinkQueue<DataType>::Empty()  
  58. {  
  59.     if(front==rear)  
  60.         return 1;  
  61.     else  
  62.         return 0;  
  63. }  

源文件LinkQueue_main.cpp:

[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. using namespace std;  
  3. #include "LinkQueue.cpp"  
  4.   
  5. void main()  
  6. {  
  7.     LinkQueue<int> Q;  
  8.     if(Q.Empty())  
  9.         cout<<"队列为空"<<endl;  
  10.     else  
  11.         cout<<"队列非空"<<endl;  
  12.     cout<<"元素10和15执行入队操作:"<<endl;  
  13.     try  
  14.     {  
  15.         Q.EnQueue(10);  
  16.         Q.EnQueue(15);  
  17.     }  
  18.     catch(char *wrong)  
  19.     {  
  20.         cout<<wrong<<endl;  
  21.     }  
  22.     cout<<"查看队头元素:"<<endl;  
  23.     cout<<Q.GetQueue()<<endl;  
  24.     cout<<"执行出队操作:"<<endl;  
  25.     try  
  26.     {  
  27.         Q.DeQueue();  
  28.     }  
  29.     catch(char * wrong)  
  30.     {  
  31.         cout<<wrong<<endl;  
  32.     }  
  33.     cout<<"查看队头元素:"<<endl;  
  34.     cout<<Q.GetQueue<<endl;  
  35. }  

0 0
原创粉丝点击