标签:
/**************************
* Date : 2015-07-19
* Description: chain.h
**************************/
#ifndef _CHAIN_H
#define _CHAIN_H
#include <iostream>
template<class T> class Chain;
// class ChainNode
template<class Type>
class ChainNode {
friend Chain<Type>;
private:
Type data; // 数据域
ChainNode<Type> *link; // 链域
};
//
// class Chain
template<class Type>
class Chain{
public:
// 创建一个空的链表
Chain() {first = 0;}
// 链表删除
~Chain();
//如果链表为空则返回true,否则返回false
bool IsEmpty() const {return first == 0;}
// 返回链表的长度
int Length() const;
//返回元素x在表中的位置;如果x不在表中,则返回0
int Search(const Type& x) const;
//查找表中第k个元素,并把它保存到x中;如果不存在,则返回false
bool Find(int k, Type& x) const;
//删除第k个元素,并把它保存到x中;函数返回修改后的线性表
Chain<Type>& Delete(int k, Type& x);
//在第k个元素之后插入x;函数返回修改后的线性表
Chain<Type>& Insert(int k, const Type& x);
// 链表打印
void Output(std::ostream& os) const;
private:
ChainNode<Type> *first;
};
// << 重载
template<class Type>
std::ostream& operator<<(std::ostream &, const Chain<Type> &);
#include "chain.cpp"
#endif // _CHAIN_H
/**************************
* Date : 2015-07-19
* Description: chain.cpp
**************************/
#ifndef _CHAIN_CPP
#define _CHAIN_CPP
#include "chain.h"
//
template<class Type>
Chain<Type>::~Chain()
{
ChainNode<Type> *next;
while (first)
{
next = first->link;
delete first;
first = next;
}
}
//
template<class Type>
int Chain<Type>::Length() const
{
ChainNode<Type> * curr = first;
int length = 0;
while(curr)
{
++length;
curr = curr->link;
}
return length;
}
//
template<class Type>
bool Chain<Type>::Find(int k, Type& x) const
{
if(k < 1) return false;
ChainNode<Type> *curr = first;
int index = 1;
while(index < k && curr)
{
curr = curr->link;
++index;
}
if(curr)
{
x = curr->data;
return true;
}
return false;
}
//
template<class Type>
int Chain<Type>::Search(const Type& x) const
{
ChainNode<Type> *curr = first;
int index = 1;
while (curr && curr->data != x)
{
curr = curr->link;
++index;
}
if(curr)
return index;
return 0;
}
//
template<class Type>
void Chain<Type>::Output(std::ostream& os) const
{
ChainNode<Type> *curr;
for(curr = first; curr; curr = curr->link)
os << curr->data << " ";
}
//
template<class Type>
std::ostream& operator<<(std::ostream& os, const Chain<Type>& chain)
{
chain.Output(os);
return os;
}
//
template<class Type>
Chain<Type>& Chain<Type>::Delete(int k, Type &x)
{
if(k < 1 || !first)
{
cerr << "删除的元素不存在" << endl;
exit(EXIT_FAILURE);
}
ChainNode<Type> *p = first;
if(k == 1) // 删除第一个元素
first = first->link;
else
{// 先找到第k-1个元素
ChainNode<Type> *q = first;
for(int index = 1; index < k-1&&q; index++,q = q->link)
;
if(!q || !q->link)
{
cerr << "删除的元素不存在" << endl;
exit(EXIT_FAILURE);
}
p = q->link;// q指向第k-1个元素,p指向第k个元素
q->link = p->link; //删除第k个元素
}
x = p->data;
delete p;
return *this;
}
//
//
template<class Type>
Chain<Type>& Chain<Type>::Insert(int k, const Type& x)
{
if(k < 0)
{
cerr << "插入位置错误" << endl;
exit(EXIT_FAILURE);
}
// 找到第k个元素
ChainNode<Type> *p = first;
for(int index = 1; index < k&&p; index++)
p = p->link;
if(k > 0 && !p)
{
cerr << "插入位置错误" << endl;
exit(EXIT_FAILURE);
}
ChainNode<Type> *y = new ChainNode<Type>;
y->data = x;
if(k)
{
y->link = p->link;
p->link = y;
}
else // 作为第一个元素插入
{
y->link = first;
first = y;
}
return *this;
}
#endif // _CHAIN_CPP
标签:
原文地址:http://www.cnblogs.com/hzwackerman/p/4659472.html