模板类实现顺序栈
#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){for(int i=len;i>0;i--){data[i]=data[i-1];}data[0]=date;len++;}elsecout << "不合理,重写!" << endl;
}
template <typename T> //头删函数
void Seqlite<T>::head_dele(){if(len==0){cout << "啥也没有啊,删啥" << endl;}else{cout << data[0] << "已弹出" << endl;for(int i=0;i<len-1;i++){data[i]=data[i+1];}len--;}
}template <typename T>
void Seqlite<T>::show(){ //展示函数if(this->len==0){cout << "啥也妹有" << endl;}for(int i=0;i<this->len;i++){cout << this->data[i] << '\t' ;}cout << endl;
}
int main()
{Seqlite<string> s1;s1.head_inst("nihao");s1.head_inst("woshuode");s1.head_inst("给叶思");s1.show();s1.head_dele();s1.show();return 0;
}
模板类实现链队列
#include <iostream>
//实现链队列
using namespace std;
template <typename T>
class Link{T data;Link* next=nullptr;
public:void head_inst(T date);void tail_dele();void show();
};
template <typename T>
void Link<T>::head_inst(T date){Link* ack=new Link;ack->data=date;if(this->next!=nullptr){ack->next=next;}next=ack;
}
template <typename T>
void Link<T>::tail_dele(){if(next==nullptr){cout << "啥也妹有" << endl;return;}Link* p=this;while(p->next->next){p=p->next;}cout << (p->next)->data << "已出队" << endl;Link* q=p->next;p->next=nullptr;delete(q);}
template <typename T>
void Link<T>::show(){if(next==nullptr){cout << "啥也没有啊" << endl;return ;}Link* p=this;while(p->next!=nullptr){cout << p->next->data << '\t' ;p=p->next;}cout << endl;
}
int main()
{Link<int> H;H.head_inst(19);H.head_inst(89);H.head_inst(119);H.show();H.tail_dele();H.show();H.tail_dele();return 0;
}