模板设计
首先创建一个模板类list,这个list中有保护成员变量:头指针、尾指针,作为链表结构,连接所有元素,在设计的过程中,增添整个类的变量指针,可以在成员函数中使用,避免了每次都需要定义的麻烦。有保护成员整形变量rightcnt记录该list中有多少个元素,有迭代器指针成员函数,返回该list的头指针和尾指针,方便我们使用遍历整个list,并对该链表进行增删查改。 其成员函数依然包括其他关于该list是否为空,返回该链表首位元素是和其末尾元素等等。
功能设计
代码
main.cpp
#include "mylist.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout<<"创建list链表L"<<endl;
LList<int> l;
l.push_front(12);l.print();
l.push_front(13);l.print();
l.push_back(14);l.print();
l.push_back(16);l.print();
l.push_back(18);l.print();
int i = 13;
cout<<"输出该列表中有多少个元素:"<<l.size()<<endl;
l.remove(i);cout<<"移除元素13:"<<endl;l.print();
cout<<"移除迭代器指向的第一位元素:"<<endl;l.erase(l.begin());l.print();
cout<<"移除列表首位元素"<<endl;l.pop_front();l.print();
cout<<"移除列表末尾元素"<<endl; l.pop_back();l.print();
cout<<"输出最后一位元素:"<<l.back()<<" 输出第一位元素: "<<l.front()<<endl;
cout<<"通过迭代器输出列表所有元素:"<<endl;
for (LList<int>::iterator iter = l.begin(); iter != l.end(); iter = iter->next)
{
cout<< iter->next->element <<" ";
}
cout<<endl;
cout<<"输出该列表最多有多少位: "<<l.max_size()<<endl;
l.clear();cout<<"清空列表"<<endl;
cout<<"输出列表是否为空:"<<l.empty()<<endl;
return 0;
}
mylist.h
#include <iostream>
using namespace std;
template<class Elem> class List {
public:
virtual bool push_front(const Elem&)=0;
virtual bool push_back(const Elem&)=0;
virtual bool remove(Elem& it)=0;
virtual void print() const =0;
};
template <class Elem> class Link{
public:
Elem element;
Link *next;
Link(const Elem& elemval,Link<Elem> *nextval=NULL)
{ element=elemval; next=nextval; }
Link(Link* nextval=NULL) {next=nextval;}
};
template <class Elem> class LList: public List<Elem>{
protected:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
Link<Elem>* mid;
int leftcnt;
int rightcnt;
int maxx = 100;
void init(){
fence =tail=head= mid = new Link<Elem>;
leftcnt=rightcnt=0;
}
void removeall() {
while(head!=NULL){
fence=head;
head=head->next;
delete fence;
}
}
public:
typedef Link<Elem>* iterator;
iterator begin(){return head;}
iterator end(){return tail;}
iterator rbegin(){return tail;}
iterator rend(){return head;}
LList(int size=50) {init();}
~LList() {removeall();}
bool push_front(const Elem& item){
fence->next=new Link<Elem>(item,fence->next);
if(tail==fence) tail=fence->next;
rightcnt++;
return true;
}
bool pop_front( ){
if(head != NULL )
{
mid = head ->next;
head = mid ;
rightcnt--;
return true;
}
return false;
}
void clear(){removeall();}
bool push_back(const Elem& item){
tail=tail->next=new Link<Elem>(item,NULL);
rightcnt++;
return true;
}
int size(){
mid = head;
int count =0 ;
while(mid != tail){
mid = mid->next;
count++;
}
return count;
}
void resize() {
maxx = maxx * 2;
}
bool pop_back( ){
if(empty()==0 ){
// cout<<"empty:"<<empty()<<endl;
mid = head;
while(mid->next != tail){
mid = mid->next;
}
// cout<<"mid:"<<mid->element<<endl;
mid ->next = NULL;
tail = mid ;
rightcnt--;
return true;
}
return false;
}
bool erase( iterator a ){
if( a==head ) pop_front( );
else if( a== head ) pop_back( );
return true;
}
int max_size(){
return maxx;
}
bool remove( Elem& it){
if(fence->next==NULL) return false;
it=fence->next->element;
Link<Elem>* ltemp=fence->next;
fence->next=ltemp->next;
if(tail==ltemp) tail=fence;
delete ltemp;
rightcnt--;
return true;
}
bool empty(){
if( head == tail ) return true;
return false;
}
Elem back() const { return tail->element;}
Elem front() const{return head->next->element;}
int leftLenght() const { return leftcnt;}
int rightLenght() const { return rightcnt;}
void print() const {
Link<Elem>* temp=head;
cout<<"<";
// while(temp!=fence){
// cout<<temp->next->element <<" ";
// temp=temp->next;
// }
cout<<"|";
while(temp->next !=NULL){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<">\n";
}
};