C++类模板实现单链表

-----------------------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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象程序设计课程作业 1. 请创建一个数据型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List型的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List型的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List型的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List型的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值