栈和队列实验

顺序栈实验:

头文件:

[cpp]  view plain copy
  1. #ifndef SeqStack_H  
  2. #define SeqStack_H  
  3. const int StackSize=10;  
  4. template <class DataType>  
  5. class SeqStack  
  6. {  
  7.     public:  
  8.         SeqStack();  
  9.         ~SeqStack(){}  
  10.         void Push(DataType x);  
  11.         DataType Pop();  
  12.         DataType GetTop();  
  13.         int Empty();  
  14.     private:  
  15.         DataType data[StackSize];  
  16.         int top;  
  17. };  
  18. #endif  
源程序:

[cpp]  view plain copy
  1. #include"SeqStack.h"  
  2.   
  3. template<class DataType>  
  4. SeqStack<DataType>::SeqStack()  
  5. {  
  6.     top=-1;  
  7. }  
  8. template<class DataType>  
  9. void SeqStack<DataType>::Push(DataType x)  
  10. {  
  11.     if(top==StackSize-1)throw"上溢";  
  12.     top++;  
  13.     data[top]=x;  
  14. }  
  15. template<class DataType>  
  16. DataType SeqStack<DataType>::Pop()  
  17. {  
  18.     DataType x;  
  19.     if(top==-1)throw"下溢";  
  20.     x=data[top--];  
  21.     return x;  
  22. }  
  23. template<class DataType>  
  24. DataType SeqStack<DataType>::GetTop ()  
  25. {  
  26.     if(top!=-1)  
  27.         return data[top];  
  28. }  
  29. template<class DataType>  
  30. int SeqStack<DataType>::Empty()  
  31. {  
  32.     if(top==-1)return 1;  
  33.     else return 0;  
  34. }  
主函数:

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3. #include"SeqStack.cpp"  
  4. void main()  
  5. {  
  6.     SeqStack<int>S;  
  7.     if(S.Empty())  
  8.         cout<<"栈空"<<endl;  
  9.     else  
  10.         cout<<"栈非空"<<endl;  
  11.     cout<<"对8和4和7和3执行入栈操作"<<endl;  
  12.     S.Push(8);  
  13.     S.Push(4);  
  14.     S.Push(7);  
  15.     S.Push(3);  
  16.     cout<<"栈顶元素为:"<<endl;  
  17.     cout<<S.GetTop()<<endl;  
  18.     cout<<"执行一次出栈操作"<<endl;  
  19.     S.Pop();  
  20.     cout<<"栈顶元素为:"<<endl;  
  21.     cout<<S.GetTop()<<endl;  
  22.     cout<<"执行一次出栈操作"<<endl;  
  23.     S.Pop();  
  24.     cout<<"栈顶元素为:"<<endl;  
  25.     cout<<S.GetTop()<<endl;  
  26.     cout<<"执行一次出栈操作"<<endl;  
  27.     S.Pop();  
  28.     cout<<"栈顶元素为:"<<endl;  
  29.     cout<<S.GetTop()<<endl;  
  30.     cout<<"执行一次出栈操作"<<endl;  
  31.     S.Pop();  
  32. }  

链队列:

头文件:

[cpp]  view plain copy
  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);  
  18.         DataType DeQueue();  
  19.         DataType GetQueue();  
  20.         int Empty();  
  21.     private:  
  22.         Node<DataType> *front,*rear;  
  23. };  
  24. #endif  
源程序:

[cpp]  view plain copy
  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;rear=s;  
  32. }  
  33.   
  34. template<class DataType>  
  35. DataType LinkQueue<DataType>::DeQueue()  
  36. {  
  37.     Node<DataType> *p=NULL;  
  38.     int x;  
  39.     if(rear==front)throw"下溢";  
  40.     p=front->next;  
  41.     x=p->data;  
  42.     front->next=p->next;  
  43.     if(p->next==NULL)rear=front;  
  44.     delete p;  
  45.     return x;  
  46. }  
  47.   
  48. template<class DataType>  
  49. DataType LinkQueue<DataType>::GetQueue()  
  50. {  
  51.     if(front !=rear)  
  52.         return front->next->data;  
  53. }  
  54.   
  55. template<class DataType>  
  56. int LinkQueue<DataType>::Empty()  
  57. {  
  58.     if(front==rear)  
  59.         return 1;  
  60.     else  
  61.         return 0;  
  62. }  
  63.       
主函数:

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值