头文件:
//头文件
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include <iostream>
using namespace std;
template <class elemType>
class List{
public:
virtual int length() const=0;
virtual int Search(const elemType &x) const=0;
virtual elemType visit(int i) const=0;
virtual void Insert(int i,const elemType &x)=0;
virtual void Remove(int i)=0;
virtual void Clear()=0;
virtual void traverse() const=0;
virtual ~List(){};
};
class OutOfBound{};
class IllegalSize{};
template <class elemType>
class seqList:public List<elemType>{
private:
elemType *data;
int currentLength;
int maxSize;
void doubleSpace();
public:
seqList(int initSize=10);
~seqList(){delete [] data;}
int length() const;
int Search(const elemType &x) const;
elemType visit(int i) const;
void Insert(int i,const elemType &x);
void Remove(int i);
void Clear();
void traverse() const;
};
template <class elemType>
class linkList:public List<elemType>
{
private:
struct node{
elemType data;
node *next;
node(const elemType &x,node *p=NULL){
data=x; next=p;
}
node():next(NULL){}
~node(){}
};
node *head;
public:
linkList();
~linkList(){Clear(); delete head;}
int length() const;
int Search(const elemType &x) const;
elemType visit(int i) const;
void Insert(int i,const elemType &x);
void Remove(int i);
void Clear();
void traverse() const;
};
template <class elemType>
class dlinkList:public List<elemType>{
private:
struct node{
elemType data;
node *prev,*next;
node(const elemType &x,node *p=NULL,node *n=NULL){data=x;next=n;prev=p;}
node():next(NULL),prev(NULL){}
~node(){}
};
node *head,*tail;
int currentLength;
public:
dlinkList();
~dlinkList(){Clear();delete head; delete tail;}
void Clear();
int length() const {return currentLength;}
void Insert(int i,const elemType &x);
void Remove(int i);
int Search(const elemType &x) const;
elemType visit(int i) const;
void traverse() const;
};
#include "seqList.cpp"
#include "linkList.cpp"
#include "dlinkList.cpp"
#endif // LIST_H_INCLUDED
顺序实现:
#ifndef cpp1_h
#define cpp1_h
#include "list.h"
#include <iostream>
using namespace std;
template <class elemType>
seqList<elemType>::seqList(int initSize){
data=new elemType[initSize];
if(!data) throw IllegalSize();
maxSize=initSize;
currentLength=0;
}
template <class elemType>
int seqList<elemType>::length() const{
return currentLength;
}
template<class elemType>
int seqList<elemType>::Search(const elemType &x) const{
int i;
for (i=0;i<currentLength;i++)
if(data[i]==x) break;
if(i==currentLength) return -1;
else return i;
}
template<class elemType>
elemType seqList<elemType>::visit(int i) const{
if (i<0 || i>=currentLength) throw OutOfBound();
return data[i];
}
template <class elemType>
void seqList<elemType>::Insert(int i,const elemType &x){
if(i>currentLength || i<0) return;
if(currentLength==maxSize) doubleSpace();
for (int j=currentLength;j>i;j--)
data[j]=data[j-1];
data[i]=x;
currentLength++;
}
template <class elemType>
void seqList<elemType>::doubleSpace(){
elemType *tmp=new elemType[maxSize*2];
if (!tmp) throw IllegalSize();
for (int j=0;j<currentLength;j++) tmp[j]=data[j];
delete [] data;
data=tmp;
maxSize=maxSize*2;
}
template <class elemType>
void seqList<elemType>::Remove(int i){
if (i<0 || i>=currentLength) return;
for (int j=i;j<currentLength-1;j++)
data[j]=data[j+1];
currentLength--;
}
template <class elemType>
void seqList<elemType>::Clear(){
currentLength=0;
}
template <class elemType>
void seqList<elemType>::traverse() const{
cout<<endl;
for(int i=0;i<currentLength;i++)
cout<<data[i]<<' ';
}
#endif // cpp1_h
单链表:
#ifndef cpp2_h
#define cpp2_h
#include "list.h"
template <class elemType>
linkList<elemType>::linkList(){
head=new node();
}
template <class elemType>
int linkList<elemType>::length() const{
int len=0; node *p=head->next;
while(p!=NULL){len++; p=p->next;}
return len;
}
template <class elemType>
int linkList<elemType>::Search(const elemType &x) const{
node *p=head->next;
int i=0;
while(p!=NULL && p->data!=x){p=p->next;i++;}
if(p==NULL) return -1;
else return i;
}
template <class elemType>
void linkList<elemType>::Insert(int i,const elemType &x){
if (i<0) return;
node *tmp,*p=head;
for (int j=0;p&&j<i;j++) p=p->next;
if (!p) return;
tmp=new node(x,p->next);
p->next=tmp;
}
template <class elemType>
void linkList<elemType>::Remove(int i)
{
if (i<0) return;
node *tmp,*p=head;
for (int j=0;p&&j<i;j++) p=p->next;
if (!p || !p->next) return;
tmp=p->next;
p->next=tmp->next;
delete tmp;
}
template <class elemType>
void linkList<elemType>::Clear(){
node *p=head->next,*q;
head->next=NULL;
while(p!=NULL){
q=p->next;
delete p;
p=q;
}
}
template <class elemType>
elemType linkList<elemType>::visit(int i) const{
if (i<0) throw OutOfBound();
node *p=head->next;
for (int j=0;p&&j<i;j++) p=p->next;
if (!p) throw OutOfBound();
return p->data;
}
template <class elemType>
void linkList<elemType>::traverse() const{
node *p=head->next;
cout<<endl;
while(p!=NULL){
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
#endif // cpp2_h
双链表:
#ifndef cpp3_h
#define cpp3_h
#include "list.h"
#include <iostream>
using namespace std;
template <class elemType>
dlinkList<elemType>::dlinkList(){
head=new node();
head->next=tail=new node();
tail->prev=head;
currentLength=0;
}
template <class elemType>
void dlinkList<elemType>::Clear(){
node *p=head->next,*q;
head->next=tail;
tail->prev=head;
while(p!=tail){
q=p->next;
delete p;
p=q;
}
currentLength=0;
}
template <class elemType>
int dlinkList<elemType>::Search(const elemType &x) const{
node *p=head->next;
int i;
for(i=0;p!=tail&&p->data!=x;i++) p=p->next;
if (p==tail) return -1;
else return i;
}
template <class elemType>
elemType dlinkList<elemType>::visit(int i) const{
if (i<0) throw OutOfBound();
node *p=head->next;
for (int j=0;p!=tail&&j<i;j++) p=p->next;
if (p==tail) throw OutOfBound();
return p->data;
}
template <class elemType>
void dlinkList<elemType>::traverse() const{
node *p=head->next;
cout<<endl;
while(p!=tail){
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
template <class elemType>
void dlinkList<elemType>::Insert(int i,const elemType &x){
if (i<0) return;
node *tmp,*p=head;
for (int j=0;p!=tail&&j<i;j++) p=p->next;
if (p==tail) return;
tmp=new node(x,p,p->next);
p->next=tmp;
tmp->next->prev=tmp;
}
template <class elemType>
void dlinkList<elemType>::Remove(int i){
if (i<0) return;
node *tmp,*p=head;
for (int j=0;p!=tail&&j<i;j++) p=p->next;
if (p==tail || p->next==tail) return;
tmp=p->next;
p->next=tmp->next;
tmp->next->prev=p;
delete tmp;
}
#endif // cpp3_h
主函数(测试用):
#include <iostream>
#include "list.h"
using namespace std;
int main()
{
seqList<int> seq;
linkList<int> link;
dlinkList<int> dlink;
seq.Insert(0,2);
seq.traverse();
seq.Remove(0);
seq.traverse();
link.Insert(0,8);
link.Insert(1,7);
link.traverse();
cout<<link.visit(0);
cout<<link.visit(1);
cout<<link.length();
link.Remove(0);
cout<<link.visit(0);
cout<<link.length();
cout<<endl;
dlink.Insert(0,1);
dlink.Insert(0,2);
dlink.traverse();
dlink.Remove(1);
dlink.traverse();
dlink.Remove(0);
return 0;
}