1、顺序表、栈、队列都更改成模板类
(1)顺序表
#include <iostream>
#include <cstring>using namespace std;template <typename T1,typename T2,typename T3>
class My_string
{
private:T1 *ptr; //指向字符数组的指针T2 size; //字符串的最大容量T3 len; //字符串当前容量public://无参构造My_string():size(15){this->ptr = new char[size];this->ptr[0] = '\0'; //表示串为空串this->len = 0;}//有参构造My_string(const T1 src){len = strlen(src);size = len+1;ptr = new char[size];strcpy(ptr,src);}My_string(T2 num, T1 value): size(num+1),len(num){ptr = new char[size];for(int i=0;i<num;i++){ptr[i] = value;}ptr[num] = '\0';}//拷贝构造My_string(const T1 &other):size(other.size),len(other.len){ptr = new char[size];strcpy(ptr,other.ptr);}//拷贝赋值template <typename T>My_string & operator =(const T &other){if(this ==&other){return *this;}delete [] ptr;size = other.size;len = other.len;ptr = new char[size];strcpy(ptr,other.ptr);return *this;}//析构函数~My_string(){delete [] ptr;}//判空bool empty(){return len ==0;}//尾插template <typename T>void push_back(T value){if(len+1>=size){size *=2;char* new_ptr = new char[size];strcpy(new_ptr,ptr);delete []ptr;ptr = new_ptr;}ptr[len] = value;len++;ptr[len] = '\0';}//尾删void pop_back(){if(len>0){len--;ptr[len]='\0';}}//at函数实现template <typename T>char &at(T index){if(index<0 ||index>=len){//throw out_of_range("超出");}return ptr[index];}//清空函数void clear(){len = 0;ptr[0] = '\0';}//返回C风格字符串char* data(){return ptr;}//返回实际长度int get_length(){return len;}//返回当前最大容量int get_size(){return size;}void show(){cout<<ptr<<" ";}};int main()
{My_string <char,int,int>s1;s1.push_back<char>('d');s1.show();}
(2)栈
#include <iostream>using namespace std;template <typename T1,typename T2,typename T3>
class My_stack
{
private:T1 *ptr;T2 size;T3 top;public:My_stack():size(20),top(-1){ptr = new char[size];}My_stack(int num){size = num;ptr = new char[num];top = -1;}template <typename T>My_stack(const T &other){top = other.top;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}}template <typename T>My_stack &operator=(const T &other){if(this==&other){return *this;}delete [] ptr;top = other.top;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}return *this;}char My_top(){return ptr[top];}void My_empty(){if(top==-1){cout<<"栈为空"<<endl;}else{cout<<"栈不为空"<<endl;}}int My_size(){return top+1;}template <typename T>void My_push(const T dat){if(top+1==size){cout<<"栈满不能继续添加"<<endl;}else{ptr[++top] = dat;}}void My_pop(){ptr[top--] = 0;}};int main()
{My_stack <char,int,int>my_stack;my_stack.My_push('1');my_stack.My_push('2');my_stack.My_push('3');cout << "Top: " << my_stack.My_top() <<endl;my_stack.My_pop();my_stack.My_empty();cout << "Size: " << my_stack.My_size() <<endl;return 0;
}
(3)队列
#include <iostream>using namespace std;#include <iostream>
#include <stdexcept>template<typename T1,typename T2,typename T3,typename T4>
class My_queue
{
private:T1 *ptr;T2 size;T3 back;T4 fro;public:My_queue():size(20),back(0),fro(0){ptr = new char[size];}template<typename T>My_queue(T num){size = num;ptr = new char[num];back = 0;fro = 0;}~My_queue(){delete [] ptr;}template<typename T>My_queue(const T &other){fro = other.fro;back = other.back;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}}template<typename T>My_queue &operator=(const T &other){if(this==&other){return *this;}delete [] ptr;fro = other.fro;back = other.back;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}return *this;}char My_front(){return ptr[fro];}char My_back(){return ptr[back-1];}bool My_empty(){return fro == back;}int My_size(){return back - fro;}template<typename T>void My_push(const T dat){if(back+1==size){cout<<"栈满不能继续添加"<<endl;}else{ptr[back++] = dat;}}void My_pop(){ptr[fro++] = 0;}};int main()
{My_queue <char,int,int,int> my_queue;my_queue.My_push('c');my_queue.My_push('2');cout << "Front: " << my_queue.My_front() <<endl; // 输出 1cout<<"back:"<<my_queue.My_back()<<endl;cout << "Size: " << my_queue.My_size() << endl; // 输出 1return 0;
}
2、思维导图