数据结构与算法设计--List C++实现

数据结构与算法设计--List  C++实现

List类的实现,用单链进行实现,运行环境VS2013,还没写注释,因为类不是很难,主要注意形参的类型即可,测试函数随便乱打,如测试出哪里的代码有问题,请评论,我会及时更正。

#include<iostream>
#include<vector>
using namespace std;
enum Error_code {
	success, range__error,overflow
};

template<class Node_entry>
struct Node
{
	Node_entry entry;
	Node<Node_entry> *next;
	Node();
	Node(Node_entry data, Node<Node_entry> *link = NULL);
};

template<class List_entry>
class List {
public:
	List();
	int size() const;
	bool empty() const;
	void clear();
	void traverse(void(*visit)(List_entry&));
	Error_code retrieve(int position, List_entry&x) const;
	Error_code replace(int position,const List_entry&x);
	Error_code remove(int position, List_entry&x);
	Error_code insert(int position, const List_entry&x);

	~List();
	List(const List<List_entry> &copy);
	void operator = (const List<List_entry> &copy);

protected:
	int count;
	Node<List_entry> *head;
	Node<List_entry> *set_position(int position) const;
};

template<class Node_entry>
Node<Node_entry>::Node() {
	next = NULL:
}

template<class Node_entry>
Node<Node_entry>::Node(Node_entry data, Node<Node_entry> *link = NULL) {
	entry = data;
	next = link;
}

template<class List_entry>
List<List_entry>::List() {
	count = 0;
	head = NULL;
}

template<class List_entry>
int List<List_entry>::size() const {
	return count;
}

template<class List_entry>
bool List<List_entry>::empty() const {
	return count == 0;
}

template<class List_entry>
void List<List_entry>::clear() {
	int a = this->size();
	List_entry b;
	while (a)
	{
		remove(a - 1, b);
		a--;
	}
}

template<class List_entry>
Node<List_entry> *List<List_entry>::set_position(int position) const {
	Node<List_entry> *p = head;
	while (position--) p = p->next;
	return p;
}

template<class List_entry>
void List<List_entry>::traverse(void(*visit)(List_entry&)) {
	for (int i = 0; i < count; i++) {
		(*visit)(set_position(i)->entry);
	}
}

template<class List_entry>
Error_code List<List_entry>::retrieve(int position, List_entry&x) const {
	if (position < 0 || position > count) return range__error;
	x = set_position(position)->entry;
	return success;
}

template<class List_entry>
Error_code List<List_entry>::replace(int position, const List_entry&x) {
	if (position < 0 || position > count) return range__error;
	set_position(position)->entry = x;
	return success;
}

template<class List_entry>
Error_code List<List_entry>::remove(int position, List_entry&x) {
	if (position < 0 || position > count) return range__error;
	if (position == 0) {
		Node<List_entry>*p = head;
		head = head->next;
		delete p;
	}
	else {
		Node<List_entry>*p = set_position(position - 1);
		Node<List_entry>*q = set_position(position);
		p->next = q->next;
		delete q;
	}
	count--;
	return success;
}

template<class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry&x) {
	if (position < 0 || position > count) return range__error;
	Node<List_entry> *new_node, *previous, *following;
	if (position > 0) {
		previous = set_position(position - 1);
		following = previous->next;
		new_node = new Node<List_entry>(x, following);
		previous->next = new_node;
	}
	else {
		following = head;
		new_node = new Node<List_entry>(x, following);
		head = new_node;
	}
	new_node = new Node<List_entry>(x, following);
	//if (new_node == NULL) return overflow;
	//if (position == 0) head = new_node;
	//else previous->next = new_node;
	count++;
	return overflow;
}

template<class List_entry>
List<List_entry>::~List() {
	clear();
}

template<class List_entry>
List<List_entry>::List(const List<List_entry> &copy) {
	*this = copy;
}

template<class List_entry>
void List<List_entry>::operator = (const List<List_entry> &copy) {
	int sum = copy.size();
	List_entry x;
	for (int i = 0; i < sum; i++) {
		copy.retrieve(i, x);
		this->insert(i, x);
	}
}

void print(int &a) {
	cout << a << " ";
}

int main() {
	List<int> list;
	int ins;
	for (int i = 0; i < 10; i++) {
		cin >> ins;
		list.insert(i, ins);
	}
	cin >> ins;
	int b = ins;
	list.insert(ins, 45);
	cout << b;
	list.replace(3, 458);
	list.traverse(print);
	List<int> second = list;
	list.clear();
	second.traverse(print);
	cout << endl<< list.empty();
	cin >> ins;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值