使用模板类,实现顺序栈。
#include <iostream>using namespace std;
template <typename T>
class Seqlite
{T data[30];int len=0;
public:void head_inst(T date);void head_dele();void show();
};
template <typename T> //头插
void Seqlite<T>::head_inst(T date)
{if(len>=30){cout << "be already full" << endl;return;}for(int i=len;i>0;i--)data[i]=data[i-1];data[0]=date;len++;
}
template <typename T> //头删
void Seqlite<T>::head_dele(){if(len==0){cout << "empty" << endl;return;}for(int i=0;i<len-1;i++)data[i]=data[i+1];cout << data[0] << " is out " << endl;len--;
}
template <typename T>
void Seqlite<T>::show()
{if(len==0){cout << "empty" << endl;return;}for(int i=0;i<len;i++)cout << data[i] << "\t" ;cout << endl;
}
int main()
{Seqlite<int> s1;s1.head_inst(1);s1.head_inst(2);s1.head_inst(3);s1.show();s1.head_dele();s1.show();return 0;
}