定义linearlist基类
// // Created by lenovo on 2018/4/22. // #ifndef LINKNODE_LINEARLIST_H #define LINKNODE_LINEARLIST_H template<class T> class linearlist{ public: linearlist(); ~linearlist(); virtual int size() const=0;//确定表的体积 virtual int length() const=0;//确定表长度 virtual int search(T &x) const=0;//搜寻给定的X virtual int locate(int i) const=0;//在表中定位第i个元素的位置 virtual bool getdata(int i,T& x) const=0;//取表中第i个元素的值 virtual void setdata(int i,T& x) const=0;//修改第i个表项的值 virtual bool insert(int i,T& x) const=0;//在表弟i项后插入值 virtual bool remove(int i,T& x) const=0;//删除表中第i项,以x来返回 virtual bool isempty() const=0;//判断空 virtual bool isfull() const=0;//判断满 virtual void sort()=0;//排序 virtual void inout()=0;//输入 virtual void output()=0;//输出 virtual linearlist<T> operator=(linearlist<T>& L)=0;//复制,重载 };
#endif //LINKNODE_LINEARLIST_H
定义派生类list
// // Created by lenovo on 2018/4/22. // #ifndef LINKNODE_LINKNODE_H #define LINKNODE_LINKNODE_H #include <iostream> #include "linearlist.h" template <class T> struct linknode{ T data;//数据域 linknode<T> *link;//指针域 linknode(linknode<T> *ptr=NULL){link=ptr;}//初始化指针 linknode(const T&item,linknode<T> *ptr=NULL) {data=item;link=ptr;}//初始化指针与数据 }; template <class T> class list:public linearlist<T>{ public: list(){first=new linknode<T>;}//构造函数 list(const T&x){first=new linknode<T>(x);}//构造函数(数组) list(list<T>&L);//复制构造函数 ~list(){makeempty();}//析构 void makeempty(); int length()const;//计算长度 linknode<T>*gethead()const {return first;}//得到头指针地址 linknode<T>*search(T x);//搜索x,并返回该地址 linknode<T>*locate(int i);//查找i,并返回该地址 bool getdata(int i,T& x);//取出第i个元素的值 void setdata(int i,T& x);//用x修改第i项的值 bool insert(int i,T& x);//在第i个元素后插入x bool remove(int i,T& x);//删除第i项的值 bool isempty()const {return first->link==NULL?true:false;} bool isfull()const{return false;} void sort(); void input(); void output(); list<T>&operator=(list<T>&L);//赋值 protected: linknode<T>*first; }; #endif //LINKNODE_LINKNODE_H
list.cpp
// // Created by lenovo on 2018/4/22. // #include "linknode.h" #include <iostream> #include <cstdlib> using namespace std; template<class T> list<T>::list(list<T> &L) { T value; linknode<T>*srcptr=L.gethead();//被复制表的附加头结点位置 linknode<T>*destptr=first=new linknode<T>; while(srcptr->link!=NULL){//逐个结点复制 value=srcptr->link->data; destptr->link=new linknode<T>(value); destptr=destptr->link; srcptr=srcptr->link; } destptr->link=NULL; } template <class T> void list<T>::makeempty() { linknode<T> *q; while(first->link!=NULL){ q=first->link; first->link=q->link; delete q; } } template <class T> int list<T>::length() const { linknode*p=first->link; int count=0; while (p!=NULL){ p=p->link; count++; } return count; } template <class T> linknode<T>* list<T>::search(T x) { linknode<T>*current=first->link; while(current!=NULL) if(current->data==x) break; else current=current->link; return current; } template <class T> linknode<T>* list<T>::locate(int i) { if(i<0) return false; linknode<T>* current=first; int k=0; while(current!=NULL && k<i){ current=current->link; k++; } return current; } template <class T> bool list<T>::getdata(int i, T &x) { if(i<=0) return false; linknode<T>*current=locate(i); if(current==NULL) return false; else{ x=current->data; return true; } } template <class T> void list<T>::setdata(int i, T &x) { if(i<=0) return ; linknode<T>*current=locate(i); if(current==NULL) return; else current->data=x; } template <class T> bool list<T>::insert(int i, T &x) { linknode<T>*current=locate(i); if(current==NULL) return false; linknode<T>*newnode=new linknode<T>(x); if(newnode==NULL){ cerr<<"存储分配错误"<<endl; exit(1); } newnode->link=current->link; current->link=newnode; return true; } template <class T> bool list<T>::remove(int i, T &x) { linknode<T>*current=locate(i-1); if(current==NULL || current->link==NULL) return false; linknode<T>*del=current->link; current->link=del->link; x=del->data; delete del; return true; } template <class T> void list<T>::output() { linknode<T>*current=first->link; while(current!=NULL){ cout<<current->data<<endl; current=current->link; } } template <class T> list<T>&list<T>::operator=(list<T>&L){ T value; linknode<T>*srcptr=L.gethead(); linknode<T>*destptr=first=new linknode<T>; while (srcptr->link!=NULL){ value=srcptr->link->data; destptr->link=new linknode<T>(value); destptr=destptr->link; srcptr=srcptr->link; } destptr->link=NULL; return *this; }