使用模板类实现List容器&迭代器


#include <iostream>
#include<stdexcept>
using namespace std;


template<typename T>
class List{
public:
	//构造函数 和 析构函数
	List():m_head(NULL), m_tail(NULL){}
	~List(){
		clear();
	}

	//拷贝构造 和 赋值构造
	List(const List& that):m_head(NULL), m_tail(NULL){
		for(Node* node = that.m_head; node; node = node->m_next){
			this->push_back(node->m_data);
		}
	}
	List& operator=(const List& that){
		if(this != &that){
			List temp = that;
			swap(m_head, temp.m_head);
			swap(m_tail, temp.m_tail);
		}
		return *this;
	}

	//清空链表
	void clear(void){
		for(Node* node = m_head, *next; node; node = next){
			next = node->m_next;
			delete node;
		}
		m_head = NULL;
		m_tail = NULL;
	}

	//是否为空
	bool empty(void) const {
		return !m_head && !m_tail;
	}

	//元素个数
	size_t size(void)const{
		size_t count = 0;
		for(Node* node = m_head; node; node = node->m_next){
			count++;
		}
		return count;
	}

	//得到首元素
	T& front(void){
		if(empty()){
			throw underflow_error("list under flow");
		}
		return m_head->m_data;
	}

	const T& front(void) const{
		return const_cast<List*>(this)->front();
	}

	//插入首元素
	void push_front(const T& data){
		m_head = new Node(data, NULL, m_head);
		if(m_head->m_next){
			m_head->m_next->m_prev = m_head;
		}else{
			m_tail = m_head;
		}
	}

	//删除首元素
	void pop_front(void){
		if(empty()){
			throw underflow_error("list under flow");
		}
		Node* temp =  m_head; 
		m_head = m_head->m_next;
		delete temp;
		temp = NULL;
		if(m_head){
			m_head->m_prev = NULL;
		}else{
			m_tail = NULL;
		}
	}

	//得到尾元素
	T& back(void){
		if(empty()){
			throw underflow_error("list under flow");
		}
		return m_tail->m_data;
	}
	const T& back(void) const {
		return const_cast<List*>(this)->back();
	}

	//插入尾元素
	void push_back(const T& data){
		m_tail = new Node(data, m_tail, NULL);
		if(m_tail->m_prev){
			m_tail->m_prev->m_next = m_tail;
		}else{
			m_head = m_tail;
		}
	}

	//删除尾元素
	void pop_back(void){
		if(empty()){
			throw underflow_error("list under flow");
		}
		Node* temp = m_tail;
		m_tail = m_tail->m_prev;
		delete temp;
		temp = NULL;
		if(m_tail){
			m_tail->m_next = NULL;
		}else{
			m_head = NULL;
		}

		
	}

	//删除某个特定的元素
	void remove(const T& data){
		for(Node* node = m_head, *next; node; node = next){
			next = node->m_next;
			if(data == node->m_data){
				if(node->m_prev){
					node->m_prev->m_next = node->m_next;
				}else{
					m_head = node->m_next;
				}
				if(node->m_next){
					node->m_next->m_prev = node->m_prev;
				}else{
					m_tail = node->m_prev;
				}
				delete node;
			}
		}
	}

	//打印整个链表
	friend ostream& operator<<(ostream& os, const List& list){
		for(Node* node = list.m_head; node; node = node->m_next){
			os << *node;
		}
		return os;
	}


// 节点的定义
private:
	class Node{
	public:
		Node(const T& data, Node* prev = NULL, Node* next = NULL):m_data(data),m_prev(prev),m_next(next){}
		friend ostream& operator<<(ostream& os, const Node& node){
			return os << "["<< node.m_data << "]";
		}
		T m_data;
		Node* m_prev;
		Node* m_next;
	};
	
	Node* m_head;
	Node* m_tail;


// 迭代器定义
public:
	class Iterator{
	private:
		Node* m_head;
		Node* m_tail;
		Node* m_node;

	public:
		Iterator(Node* head = NULL, Node* tail = NULL, Node* node = NULL):m_head(head),m_tail(tail),m_node(node){}
		bool operator==(const Iterator& that){
			return m_node == that.m_node;
		}
		bool operator!=(const Iterator& that){
			return !(*this==that);
		}

		Iterator& operator++(){
			if(m_node)
				m_node = m_node->m_next;
			else
				m_node = m_head;
			return *this;
		}
		const Iterator& operator++(int){
			Iterator temp = *this;
			++(*this);
			return temp;
		}

		Iterator& operator--(){
			if(m_node)
				m_node = m_node->m_prev;
			else
				m_node = m_tail;
			return *this;
		}
		const Iterator& operator--(int){
			Iterator temp = *this;
			--(*this);
			return temp;
		}

		T& operator*()const{
			return m_node->m_data;
		}
		T* operator->()const{
			//return &**this;
			return &((*this).m_node->m_data);
		}

	};
	const Iterator begin(){
		return Iterator(m_head, m_tail, m_head);
	}
	const Iterator end(){
		return Iterator(m_head, m_tail, NULL);
	}	

};



void test1(){
	List<int> list;
	for(int i = 0; i< 10; i++)
		list.push_back(i);
	cout << list << endl;

	for(List<int>::Iterator it = list.begin(); it != list.end(); ++it){
		cout << *it << endl;
	}
}

int main(){
	test1();
	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、付费专栏及课程。

余额充值