C++类模板实现双链表

----------------------------------List.h------------------------------------

#ifndef LIST_H_XX
#define  LIST_H_XX

enum Error_code{rangeerror, underflow, overflow, success};
template<class Node_entry>
struct Node
{
	//data member
	Node_entry entry;
	Node<Node_entry> *next;
	Node<Node_entry> *back;
	//constructor
	Node();
	Node(Node_entry entry, Node<Node_entry> *next=NULL, Node<Node_entry>*back=NULL);
};

template<class List_entry>
class List
{
public:
	List();
	~List();
	List<List_entry>& operator = ( List<List_entry> ©);
	bool empty() const;
	Error_code replace(int position, const List_entry&data);
	Error_code retrieve(int position, List_entry&x) const;
	Error_code insert(int position, const List_entry&x);
	Error_code remove(int position);
	int getsize() const;
	void print() const;
protected:
	//mutable 的作用是在const函数中也可以对其进行修改
	int count;
	mutable int current_position;
	mutable Node<List_entry> *current;
	void set_position(int position) const;
};
#include "List.cpp"
#endif

------------------------------------------List.cpp-----------------------------------------------

#ifndef LIST_CPP_XX
#define LIST_CPP_XX
#include "List.h"
#include <iostream>
using namespace std;
//结构体构造函数
template <class Node_entry>
Node<Node_entry>::Node()
{
	back = next = NULL;
}
template <class Node_entry>
Node<Node_entry>::Node(Node_entry entry, Node<Node_entry> *next/* =NULL */, Node<Node_entry>*back/* =NULL */)
{
	this->entry = entry;
	this->back = back;
	this->next = next;
}
//类实现函数,constructor
template <class List_entry>
List<List_entry>::List()
{
	current_position = count = 0;
	current = NULL;
}
//destructor
template <class List_entry>
List<List_entry>::~List()
{
	while(!empty())
		remove(0);
}
//insert a member
//0<=position<=count
template <class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry &x)
{
	if(position<0 || position>count)
		return rangeerror;
	Node<List_entry>*previous, *following, *new_node;
	if(position == 0)
	{
		//如果链表为空 
		if(count == 0) 
		{previous = following = NULL;}
		else
		{
			set_position(0);
			following = current;
			previous = NULL;
		}
	}
	else
	{
		set_position(position-1);
		previous = current;
		following = current->next;
	}
	//完成插入操作
	new_node = new Node<List_entry>(x, following, previous);
	if(new_node == NULL) return overflow;
	if(following!=NULL) following->back = new_node;
	if(previous!=NULL) previous->next = new_node;
	//指针指向当前操作的位置
	current = new_node;
	current_position = position;
	count++;
	return success;
}
//0<=position<coount
template <class List_entry>
Error_code List<List_entry>::remove(int position)
{
	Node<List_entry>* previous, *del_node, *following;
	if(empty())
		return underflow;
	if(position<0 || position>count)
		return rangeerror;
	set_position(position);
	if(position == 0)
	{	
		del_node = current;
		following = current->next;
		if(following!=NULL)following->back = NULL;
		delete del_node;

		count--;
		current = following;
		current_position = position;
		return success;		
	}
	else
	{
		del_node = current;
		following = current->next;
		previous = current->back;
		//断开连接
		if(previous!=NULL)previous->next = following;
		if(following!=NULL)following->back = previous;
		//释放内存
		delete del_node;
		count--;
		//如果删除的结点在最后
		if(following == NULL)
		{
			current_position = position-1;
			current = previous;
		}
		else
		{
			current = following;
			current_position = position;
		}
	}
	return success;
}
//current和current_position加了 mutable,所以这两个变量在 const函数里也能修改
template <class List_entry>
void List<List_entry>::set_position(int position) const
{
	int i ;
	if(position>current_position)
	{
		for(i=current_position; i<position; i++)
			current = current->next;
	}
	else
	{
		for(i=position; i<current_position; i++)
			current = current->back;
	}
	current_position = position;
}
//读取指定位置的数据
//0<=position<count
template <class List_entry>
Error_code List<List_entry>::retrieve(int position, List_entry&x) const
{
	if(empty())
		return underflow;
	if(position<0 || position>count)
		return rangeerror;
	set_position(position);
	x = current->entry;
	return success;
}
//打印函数,注意不要在该函数中直接操作修改current 或调用 set_position
template <class List_entry>
void List<List_entry>::print() const
{
	List_entry x;
	int num = getsize();
	for(int i=0; i<num; i++)
	{
		retrieve(i, x);
		cout<<x;
	}
	cout<<endl;
}
//replace 函数
//0<=position<count
template <class List_entry>
Error_code List<List_entry>::replace(int position, const List_entry&data)
{
	if(empty())
		return underflow;
	if(position>count||position<0)
		return rangeerror;
	set_position(position);
	current->entry = data;
	return success;
}
template <class List_entry>
int List<List_entry>::getsize() const
{
	return count;
}

template <class List_entry>
bool List<List_entry>::empty() const
{
	return (count == 0);
}
//如果返回值为void 那么只能实现单重赋值,下面的方法可以实现多重赋值如first_list=second_list=third_list
//另外要注意的是不能简单地赋值current和current_position,因为析构函数中会调用delete所以必须要在此处重新
//分配内存,即调用insert函数
template <class List_entry>
List<List_entry>&  List<List_entry>::operator=( List<List_entry> ©)
{
	int num = copy.getsize();
	List_entry data;
	for(int i=0; i<num; i++)
	{
		copy.retrieve(i, data);
		this->insert(i, data);
	}
	return *this;
}
#endif

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

#include <iostream>
#include "List.h"
using namespace std;

int main()
{
	List<char*> tt;
	tt.insert(0, "mmsa ");
	tt.insert(1, "jjkl ");
	tt.insert(1, "ooks ");
  	tt.remove(0);
  	tt.print();
 	tt.insert(0, "nmcnv ");
 	tt.print();

 	List<char*> dd, mm;
 	mm = dd = tt;
	dd.replace(0, "dddd ");
 	dd.print();
	mm.print();
	system("pause");
	return 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、付费专栏及课程。

余额充值