作业:
将之前实现的顺序表、栈、队列都更改成模板类
顺序表
#include <iostream>using namespace std;template<typename T>class SeqList
{
private:T *ptr;int size; //总长度int len = 0; //当前顺序表实际长度public://初始化void init(int n){this->ptr = new T[n];this->len = 0;this->size = n;}//判空bool empty(){return this->len == 0;}//判满bool full(){return this->len == this->size;}//尾插void push_back(T e){//判断是否满了if(this->full()){return ;}this->ptr[len++] = e;}//插入void insert(T index){//判断是否满了if(this->full()){return ;}int i,pos;cout << "pos>>";cin >> pos;for(i=this->len; i>=pos; i--){this->ptr[i] = this->ptr[i-1];}this->ptr[i] = index;this->len++;}//任意位置删除void del(int index){if(empty() == 1){return;}int i;for(i = index - 1; i < this->len; i++){this->ptr[i] = this->ptr[i+1];}this->len--;}//尾删void pop_back(int index){if(empty() == 1){return;}int i;for(i = 0; i < index; i++){this->ptr[this->len--] = 0;}}//求长度void my_size(){cout << "len = " << this->len <<endl;}//获取任意位置元素T & at(int index){static T num;num = this->ptr[index-1];return num;}//将顺序表进行排序void sort(bool flag){int i,j;if(flag){for(i = 0; i < this->len; i++){for(j = 0; j < this->len - 1- i;j++){if(this->ptr[j]>this->ptr[j+1]){T temp = this->ptr[j];this->ptr[j] = this->ptr[j+1];this->ptr[j+1] = temp;}}}}else{for(i = 0; i < this->len; i++){for(j = 0; j < this->len - 1- i;j++){if(this->ptr[j]<this->ptr[j+1]){T temp = this->ptr[j];this->ptr[j] = this->ptr[j+1];this->ptr[j+1] = temp;}}}}}//定义展示函数void show(){//判空if(empty() == 1){return;}for(int i=0; i<this->len; i++){cout<<this->ptr[i]<<" ";}cout<<endl;}
};
int main()
{SeqList<int> s1; //实例化一个顺序表对象s1.init(10);s1.show();s1.insert(8);s1.insert(7);s1.insert(9);s1.insert(2);s1.show();cout<<s1.at(2)<<endl;s1.del(1);s1.show();return 0;
}
运行结果:
栈
#include <iostream>using namespace std;template<typename T>
class Stack //定义一个栈的模板类
{
private:T *ptr; //栈int num; //栈顶元素数int max; //栈的空间初始大小
public:Stack():num(-1),max(20){ptr = new T[max]; //申请空间}Stack(int t,int m):num(t),max(m){ptr = new T[max]; //申请栈空间for(int i=0;i<num+1;i++){cout<<"input>>";cin>>ptr[i];}}~Stack(){delete [] ptr;}Stack(Stack &s):ptr(new T(*s.ptr)),num(s.num),max(s.max){}//栈顶元素访问T top(){return ptr[num];}//判空bool empty(){return num==-1;}//元素数int size(){return num+1;}//向栈顶插入元素void push(T n){//判满if(num==max-1){cout<<"full"<<endl;return;}++this->num;this->ptr[num] = n;}//删除栈顶元素void pop(){//判空if(empty()){return;}this->num--;}void show(){for(int i=0;i<num+1;i++){cout<<ptr[i]<<" ";}cout<<endl;}
};
int main()
{Stack <string> s1(4,10); //有参构造s1.show();s1.pop();s1.show();string buff;cout<<"push>>";cin>>buff;s1.push(buff);s1.show();cout<<s1.size()<<" "<<s1.top()<<endl;return 0;
}
运行结果:
队列
#include <iostream>
#include <stack>
using namespace std;
//队列模板类
template<typename T>
class Queue
{
private:int max; //队列最大容量int num; //队列内元素数T *ptr; //容器
public:Queue():max(20),num(0){ptr = new T[this->max];}~Queue(){delete [] ptr; //释放空间}Queue(Queue &q):max(q.max),num(q.num),ptr(new T(*q.ptr)){} //深拷贝T front(){if(empty()){cout<<"null"<<endl;T a;return a;}return ptr[0];}T back(){if(empty()){cout<<"null"<<endl;T a;return a;}return ptr[num-1];}bool empty(){return num==0;}int size(){return num;}void push(T n){//判满if(num>=max){cout<<"full"<<endl;return;}this->num++;this->ptr[num-1] = n;}void pop(){//判空if(empty()){cout<<"null"<<endl;return;}for(int i=0;i<this->num;i++){this->ptr[i]=this->ptr[i+1];}this->num--;}void show(){for(int i=0;i<num;i++){cout<<ptr[i]<<" ";}cout<<endl;}
};
int main()
{Queue<string> q;//判空if(q.empty()){q.push("hello");q.push("world");q.push("I");q.push("love");q.push("china");q.show();}cout<<q.back()<<endl;cout<<q.front()<<endl;cout<<q.size()<<endl;q.pop();q.show();return 0;
}
运行结果:
知识梳理:
C++11新特性之智能指针(重要)