#include<iostream>
using namespace std;
template <class ElemType>
void Show(const ElemType &e)
// 操作结果: 显示数据元素
{
cout << e << " ";
}
// 结点类模板
template <class ElemType>
struct Node
{
// 数据成员:
ElemType data; // 数据成分
Node<ElemType> *next; // 指针成分
// 构造函数模板:
Node(); // 无参数的构造函数模板
Node(const ElemType &e, Node<ElemType> *link = NULL); // 已知数据元素值和指针建立结点
};
// 结点类模板的实现部分
template<class ElemType>
Node<ElemType>::Node()
// 操作结果:构造指针成分为空的结点
{
next = NULL;
}
template<class ElemType>
Node<ElemType>::Node(const ElemType &e, Node<ElemType> *link)
// 操作结果:构造一个数据成分为e和指针成分为link的结点
{
data = e;
next = link;
}
// 简单线性链表类模板
template <class ElemType>
class SimpleLinkList
{
protected:
// 数据成员:
Node<ElemType> *head; // 头结点指针
// 辅助函数模板:
Node<ElemType> *GetElemPtr(int position) const; // 返回指向第position个结点的指针
public:
// 抽象数据类型方法声明及重载编译系统默认方法声明:
SimpleLinkList(); // 无参数的构造函数模板//
virtual ~SimpleLinkList(); // 析构函数模板//
int Length() const; // 求线性表长度 //
bool Empty() const; // 判断线性表是否为空//
void Clear(); // 将线性表清空//
void Traverse(void(*visit)(const ElemType &)) const; // 遍历线性表/
bool GetElem(int position, ElemType &e) const; // 求指定位置的元素 //
bool SetElem(int position, const ElemType &e); // 设置指定位置的元素值//
bool Delete(int position, ElemType &e); // 删除元素 //
bool Delete(int position); // 删除元素 //
bool Insert(int position, const ElemType &e); // 插入元素///
SimpleLinkList(const SimpleLinkList<ElemType> &source); // 复制构造函数模板//
SimpleLinkList<ElemType> &operator =(const SimpleLinkList<ElemType> &source); // 重载赋值运算符//
};
template <class ElemType>
Node<ElemType> * SimpleLinkList<ElemType>::GetElemPtr(int position) const
{
int nowPosition = 0;
Node<ElemType> * now = head;
while (nowPosition != position && now != NULL)
{
now = now->next;
nowPosition++;
}
return now;
}
template <class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList()
// 操作结果:构造一个空链表
{
head = new Node<ElemType>; // 构造头结点,空链表只有头结点
}
template <class ElemType>
SimpleLinkList<ElemType>::~SimpleLinkList()
// 操作结果:销毁线性表
{
Clear(); // 清空线性表
delete head; // 释放头结点所点空间
}
template <class ElemType>
int SimpleLinkList<ElemType>::Length() const
// 操作结果:返回线性表元素个数
{
int count = 0; // 计数器
for (Node<ElemType> *temPtr = head->next; temPtr != NULL; temPtr = temPtr->next)
{ // 用temPtr依次指向每个元素
count++; // 对线性表的每个元素进行计数
}
return count; // 返回元素个数
}
template <class ElemType>
bool SimpleLinkList<ElemType>::Empty() const
// 操作结果:如线性表为空,则返回true,否则返回false
{
return head->next == NULL;
}
template <class ElemType>
void SimpleLinkList<ElemType>::Clear()
// 操作结果:清空线性表
{
while (!Empty())
{ // 表性表非空,则删除第1个元素
Delete(1); // 删除第1个元素
}
}
template <class ElemType>
bool SimpleLinkList<ElemType>::GetElem(int position, ElemType &e) const
{
if (position < 1 || position > Length())
{ // position范围错
return false; // 元素不存在
}
else
{ // position合法
Node<ElemType> *temPtr = GetElemPtr(position); // 取出指向第position个结点的指针
e = temPtr->data; // 用e返回第position个元素的值
return true; // 元素存在
}
}
template <class ElemType>
bool SimpleLinkList<ElemType>::SetElem(int position, const ElemType &e)
// 操作结果:将线性表的第position个元素赋值为e,
// position的取值范围为1≤position≤Length(),
// position合法时返回true,否则返回false
{
if (position < 1 || position > Length())
{ // position范围错
return false; // 范围错
}
else
{ // position合法
Node<ElemType> *temPtr = GetElemPtr(position); // 取出指向第position个结点的指针
temPtr->data = e; // 设置第position个元素的值
return true; // 设置元素成功
}
}
template <class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList(const SimpleLinkList<ElemType> &source)
// 操作结果:由线性表source构造新线性表——复制构造函数模板
{
int sourceLength = source.Length(); // source的长度
ElemType tempElem; // 临时元素
head = new Node<ElemType>; // 初始化空线性表,空链表只有头结点
for (int temPos = 1; temPos <= sourceLength; temPos++)
{ // 复制数据元素
source.GetElem(temPos, tempElem); // 取出第temPos个元素
Insert(Length() + 1, tempElem); // 将tempElem插入到当前线性表
}
}
template <class ElemType>
SimpleLinkList<ElemType> &SimpleLinkList<ElemType>::operator =(const SimpleLinkList<ElemType> &source)
// 操作结果:将线性表source赋值给当前线性表——重载赋值运算符
{
if (&source != this)
{
int sourceLength = source.Length(); // source的长度
ElemType tempElem; // 临时元素
Clear(); // 清空当前线性表
for (int temPos = 1; temPos <= sourceLength; temPos++)
{ // 复制数据元素
source.GetElem(temPos, tempElem); // 取出第temPos个元素
Insert(Length() + 1, tempElem); // 将tempElem插入到当前线性表
}
}
return *this;
}
template <class ElemType>
void SimpleLinkList<ElemType>::Traverse(void(*visit)(const ElemType &)) const
{
for (Node<ElemType> * temptr = head->next; temptr != NULL; temptr = temptr->next) {
(*visit)(temptr->data);
}
}
template <class ElemType>
bool SimpleLinkList<ElemType>::Delete(int position, ElemType &e) {
bool result = false;
if (position < 1 || position > Length()) {
}
else {
Node<ElemType>* E = GetElemPtr(position);
e = E -> data;
Node<ElemType> * temptr = GetElemPtr(position - 1);
temptr->next = E->next;
result = true;
}
return result;
}
template <class ElemType>
bool SimpleLinkList<ElemType>::Delete(int position) {
bool result = false;
if (position < 1 || position > Length()) {
}
else {
Node<ElemType> * temptr = GetElemPtr(position - 1);
Node<ElemType> *nextptr = temptr->next;
temptr->next = nextptr->next;
delete nextptr;
result = true;
}
return result;
}
template <class ElemType>
bool SimpleLinkList<ElemType>::Insert(int position, const ElemType &e) {
bool result = false;
if (position < 1 || position > Length() + 1) {}
else {
Node<ElemType> * pre = GetElemPtr(position - 1);
Node<ElemType> * ins = new Node<ElemType>(e,NULL);
ins->next = pre->next;
pre->next = ins;
result = true;
}
return result;
}
也是从教科书扒下来的,侵删