您的位置:首页 > 编程语言 > C语言/C++

C++类模板实现单链表

2013-10-28 09:52 330 查看
-----------------------Chain.h---------------------------

#ifndef CHAIN_H_
#define CHAIN_H_
#include <iostream>
using namespace std;
enum Err_type{success, underflow, rangeerror, overflow};
template<class T>
struct ChainNode
{
T data;
ChainNode<T>* next;
ChainNode(const T& data, ChainNode<T>* next);
//	ChainNode();
};

template<class T>
class Chain
{
public:
Chain();
~Chain();
bool empty() const;
int getsize() const;
Err_type insert(int pos, const T& data);
Err_type remove(int k, T& data);
Err_type retrieve(int k, T& data) const;
int find(const T& data);
void output(ostream& out) const;
private:
int count;
ChainNode<T>* head;
};
template<class T>
ostream& operator<<(ostream& out, const Chain<T>&x);

#include "chain.cpp"
#endif

--------------------Chain.cpp----------------------

#ifndef CHAIN_CPP_
#define CHAIN_CPP_
#include "chain.h"
template<class T>
ChainNode<T>::ChainNode(const T& data, ChainNode<T>* next)
{
this->data = data;
this->next = next;
}
template<class T>
Chain<T>::Chain()
{
count = 0;
head = NULL;
}
template<class T>
Chain<T>::~Chain()
{
ChainNode<T>* next;
while(head)
{
next = head->next;
delete head;
head = next;
}
}
template<class T>
int Chain<T>::getsize() const
{
return count;
}
template<class T>
bool Chain<T>::empty() const
{
return (count==0)
}
template<class T>
Err_type Chain<T>::insert(int pos, const T& data)
{//在位置pos出插入数据data,0<=pos<=count
if(pos>count || pos<0)
return rangeerror;
ChainNode<T> *first = head, *ins, *following;
ins = new ChainNode<T>(data, NULL);
if(ins == NULL) return underflow;
int i = 0;
if(pos == 0)
{
if(count == 0)
head = ins;
else
{
ins->next = head;
head = ins;
}
}
else
{
while(i<(pos-1) && first)
{
first = first->next;
i++;
}
following = first->next;
first->next = ins;
ins->next = following;
}
count++;
return success;
}
template<class T>
Err_type Chain<T>::remove(int k, T& data)
{//把第k个元素取到data中,然后删除该元素, 0<=k<count
int i=0;
if(k<0 || k>=count)
return rangeerror;
ChainNode<T>*first = head, *del;
if(k==0)
{
del = first;
head = del->next;
}
else
{
while(i<(k-1))
{
i++;
first = first->next;
}
del = first->next;
first->next = del->next;
}
delete del;
count--;
return success;
}
template<class T>
Err_type Chain<T>::retrieve(int k, T& data) const
{//获取位置k的值保存在data中, 0<=k<count
if(k<0 || k>=count)
return rangeerror;
ChainNode<T>* first = head;
int i = 0;
while(i<k)
{
first = first->next;
i++;
}
data = first->data;
return success;
}
template<class T>
int Chain<T>::find(const T& data)
{//查找是否有等于data的元素,成功返回对应位置,否则返回-1
int i = 0;
ChainNode<T>* first = head;
while(data!=first->data)
{
first = first->next;
if(!first)
return -1;
i++;
}
return i;
}
template<class T>
void Chain<T>::output(ostream& out) const
{
ChainNode<T>* first = head;
while(first)
{
out<<first->data<<" ";
first = first->next;
}
out<<endl;
}
template<class T>
ostream& operator<<(ostream& out, const Chain<T>&x)
{
x.output(out);
return out;
}
#endif


----------------------测试代码--------------------------

#include <iostream>
#include "tools.h"
#include "chain.h"
using namespace std;
int main()
{
Chain<int> dd;
int rr;
dd.insert(0, 2);
dd.insert(1, 3);
dd.insert(0,4);
dd.insert(3,7);
dd.insert(3, 10);
cout<<dd;
dd.remove(0, rr);
cout<<dd;
dd.remove(2, rr);
cout<<dd;
dd.remove(1, rr);
cout<<dd;
dd.retrieve(0, rr);
cout<<rr<<endl;
dd.retrieve(1, rr);
cout<<rr<<endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: