《数据结构》实验三: 栈和队列实验 补
2、自己选择顺序或链式存储结构,定义一个空栈队列,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。 头文件 # ifndef LinkQueue_H # define LinkQueue_H template<class DataType> struck Node { DataType data; Node<DataType> *next; }; template<class DataType> classLinkQueue { public: LinkQueue(); ~LinkQueue(); voidEnQueue(DataType x); DataType DeQueue(); DataType GetQueue(); int Empty(); pirvate: Node<DataType> *front,*rear; }; #endif;
源程序文件 # include"LinkQueue.h" 主函数文件 #include<iostream> using namespace std; # include"LinkQueue.cpp" void main() { LinkQueue<int>Q; if(Q.Empty()) cout<<"队列为空"<<endl; else cout<<"队列非空"<<endl; cout<<"元素10和15执行入队操作:"<<endl; try { Q.EnQueue(10); Q.EnQueue(15); } catch(char* wrong) { cout<<wrong<<endl; } cout<<"查看队头元素:"<<endl; cout<<Q.GetQueue()<<endl; cout<<"执行出队操作:"<<endl; try { Q.DeQueue(); } catch(char*wrong) { cout<<wrong<<endl; } cout<<"查看队头元素:"<<endl; cout<<Q.GetQueue()<<endl; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |