不懈的追求者
lz503338451
全部博文(16)
2011年(1)
2008年(15)
Diy_os
wangzong
分类: C/C++
2008-03-28 22:17:34
#ifndef HEADER_STACK#define HEADER_STACK#include<iostream.h>#define stacksize 10#include<iostream.h> template<class T>class Stack{private: T data[stacksize]; int top;public: Stack(); int push(const T& x); T pop(); T gettop(); int Gettop(); bool Empty(); void clear();};template<class T>int Stack<T>:: Gettop(){ return top;}template<class T>Stack<T>::Stack(){ top=-1;}//Stack::就没有必要template<class T>//这里要明白这个模板的声明什么时候用到它.只要有变量有类型用到它,才要加,如果是像int Stack<T>::push(const T& x)//由于这是形参,用到了它{ if (top==stacksize-1) { cout<<"表满"<<endl; return 0; }else{ top++; data[top]=x; cout<<data[top]; cout<<"ABC"<<endl; return 1;}}template<class T>T Stack<T>::pop(){ if (top==-1) { cout<<"栈空"<<endl; return NULL; } else { T x; x=data[top]; top--; cout<<x<<endl; return x; }}template<class T>T Stack<T>::gettop(){ if (top==-1) { cout<<"栈空 "<<endl; return NULL; }else { T n; n=data[top]; return n;}}template<class T>bool Stack<T>::Empty()//作为外部的访问{ if(top==-1) {cout<<"栈空"<<endl;return true;} else return false;}template<class T>void Stack<T>::clear(){ top=-1;}#endif
#include#include#include#include"stack.h"#define N 5void main(){ Stack st; int a[N]; for(int i=0;i cin>>a[i]; for(int last=0,j=0;j { for(int p=last+1;p<=a[j];p++) st.push(p); if(last if(st.gettop()!=a[j]) {cout<<"这组数据不可能组成一列车"< st.pop(); } cout<<"可以组成一列车"<}
上一篇:字符记数
下一篇:类实现广义表
登录 注册