SqList.h
#ifndef SQLIST_H
#define SQLIST_H
template < class ElemType >
class SqList
... {
public:
SqList(); //Init
~SqList(); //Uninit
void insert(size_t i, ElemType e); //Insert
void erase(size_t i); //Delete
ElemType &operator[](size_t i); //Get
void for_each(void (*visit)(ElemType &)); //For each
class bad_pos...{}; //异常类:错误的下标
private:
static const int LIST_INIT_SIZE = 100; //初始分配量
static const int LIST_INCREMENT = 10; //每次分配增量
ElemType *head;
size_t length;
size_t listsize;
} ;
#include " SqList.cpp "
#endif
#define SQLIST_H
template < class ElemType >
class SqList
... {
public:
SqList(); //Init
~SqList(); //Uninit
void insert(size_t i, ElemType e); //Insert
void erase(size_t i); //Delete
ElemType &operator[](size_t i); //Get
void for_each(void (*visit)(ElemType &)); //For each
class bad_pos...{}; //异常类:错误的下标
private:
static const int LIST_INIT_SIZE = 100; //初始分配量
static const int LIST_INCREMENT = 10; //每次分配增量
ElemType *head;
size_t length;
size_t listsize;
} ;
#include " SqList.cpp "
#endif
SqList.cpp
#ifndef SQLIST_CPP
#define SQLIST_CPP
#include " SqList.h "
#include < cstdlib >
template < class ElemType >
const int SqList < ElemType > ::LIST_INIT_SIZE = 100 ;
template < class ElemType >
const int SqList < ElemType > ::LIST_INCREMENT = 10 ;
template < class ElemType >
SqList < ElemType > ::SqList()
... {
//初始化,分配空间
head = new ElemType[LIST_INIT_SIZE];
length = 0;
listsize = LIST_INIT_SIZE;
}
template < class ElemType >
SqList < ElemType > :: ~ SqList()
... {
//销毁元素,释放空间
delete[] head;
length = listsize = 0;
}
template < class ElemType >
void SqList < ElemType > ::insert(size_t i, ElemType e)
... {
//在第i个位置处插入新元素e
if(i < 0 || i > length + 1) throw bad_pos(); //i不合法
if(length >= listsize)
...{
//当前存储空间已满,添加分配
ElemType *newbase = new ElemType[listsize + LIST_INCREMENT];
std::memcpy(newbase, head, listsize);
head = newbase;
listsize += LIST_INCREMENT;
}
ElemType *dest = head + i;
for(ElemType *p = head + length - 1; p >= dest; p--)
*(p + 1) = *p; //依次后移元素
*dest = e; //插入e
length++; //表长+1
}
template < class ElemType >
void SqList < ElemType > ::erase(size_t i)
... {
//删除第i个位置上的元素
if(i < 0 || i > length + 1) throw bad_pos(); //i不合法
for(ElemType *p = head + i; p < head + length; p++)
*p = *(p + 1); //依次前移元素
length++; //表长-1
}
template < class ElemType >
ElemType & SqList < ElemType > :: operator [](size_t i)
... {
if(i < 0 || i > length + 1) throw bad_pos(); //i不合法
//下标操作
return head[i];
}
template < class ElemType >
void SqList < ElemType > ::for_each( void ( * visit)(ElemType & ))
... {
for(int i = 0; i < length; i++)
visit(head[i]);
}
#endif
#define SQLIST_CPP
#include " SqList.h "
#include < cstdlib >
template < class ElemType >
const int SqList < ElemType > ::LIST_INIT_SIZE = 100 ;
template < class ElemType >
const int SqList < ElemType > ::LIST_INCREMENT = 10 ;
template < class ElemType >
SqList < ElemType > ::SqList()
... {
//初始化,分配空间
head = new ElemType[LIST_INIT_SIZE];
length = 0;
listsize = LIST_INIT_SIZE;
}
template < class ElemType >
SqList < ElemType > :: ~ SqList()
... {
//销毁元素,释放空间
delete[] head;
length = listsize = 0;
}
template < class ElemType >
void SqList < ElemType > ::insert(size_t i, ElemType e)
... {
//在第i个位置处插入新元素e
if(i < 0 || i > length + 1) throw bad_pos(); //i不合法
if(length >= listsize)
...{
//当前存储空间已满,添加分配
ElemType *newbase = new ElemType[listsize + LIST_INCREMENT];
std::memcpy(newbase, head, listsize);
head = newbase;
listsize += LIST_INCREMENT;
}
ElemType *dest = head + i;
for(ElemType *p = head + length - 1; p >= dest; p--)
*(p + 1) = *p; //依次后移元素
*dest = e; //插入e
length++; //表长+1
}
template < class ElemType >
void SqList < ElemType > ::erase(size_t i)
... {
//删除第i个位置上的元素
if(i < 0 || i > length + 1) throw bad_pos(); //i不合法
for(ElemType *p = head + i; p < head + length; p++)
*p = *(p + 1); //依次前移元素
length++; //表长-1
}
template < class ElemType >
ElemType & SqList < ElemType > :: operator [](size_t i)
... {
if(i < 0 || i > length + 1) throw bad_pos(); //i不合法
//下标操作
return head[i];
}
template < class ElemType >
void SqList < ElemType > ::for_each( void ( * visit)(ElemType & ))
... {
for(int i = 0; i < length; i++)
visit(head[i]);
}
#endif