数据结构之——单链表的实现

/*
	使用嵌套类实现【单链表】
	学习要点:、
	(1)实现单链表数据结构的定义以及各种操作
	(1)区别深拷贝(另外动态分配内存进行拷贝) 和 浅拷贝(直接拷贝赋值)
	(2)实现深拷贝构造和深拷贝赋值函数
	(3)实现操作运算符的重载
			=    (深拷贝赋值)   
			<<    输出运算符重载:建立全局函数,在类中定义时可声明为friend函数;
				friend函数可访问类中private成员

*/



#include<iostream>
using namespace std;

class List{
public:
	//构造函数 和  析构函数
	List(void) : m_head(NULL){}
	~List(){
		clear();
	}

	// 深度拷贝构造  和  深度拷贝赋值
	List(const List& that):m_head(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;
			this->m_head  = temp.m_head;
			temp.m_head = NULL;
		}
		return *this;
	}

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

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

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

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

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

	//插入首元素
	void push_front(const int& data){
		m_head = new Node(data, 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;
	}

	//得到尾元素
	int& back(void){
		if(empty()){
			throw underflow_error("list under flow");
		}
		Node* node = m_head;
		while(node){
			if(!node->m_next){
				return node->m_data;
			}
			node = node->m_next;
		}
	}
	const int& back(void) const {
		return const_cast<List*>(this)->back();
		
	}

	//插入尾元素
	void push_back(const int& data){
		Node* node = m_head;
		while(node){
			if(!node->m_next){
				node->m_next = new Node(data, NULL);
				return;
			}
			node = node->m_next;
		}
	}

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

	//删除某个特定的元素
	void remove(const int& data){
		while(data == m_head->m_data){
			this->pop_front();
		}
		for(Node* node = m_head; node; node = node->m_next){
			if(node->m_next && data == node->m_next->m_data){
				Node* temp = node->m_next;
				node->m_next = node->m_next->m_next;
				delete temp;
				temp = NULL;
			}	
		}
	}

	//打印整个链表
	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:
		int   m_data;
		Node* m_next;
		Node(int data, Node* next = NULL):m_data(data), m_next(next){}
		/* 注意:重载输出/入运算符, 要定义全局函数,在类内定义时可定义为friend函数  */
		//打印节点
		friend ostream& operator<<(ostream& os, const Node& node){
			return os << "["<< node.m_data << "]";
		}
	};
	Node* m_head;

};
//  主函数测试
int main(){
	List list;
	cout << list.size() << endl;
	list.push_front(5);
	list.push_front(6);
	list.push_front(5);
	list.push_front(5);
	cout << list.empty() << endl;
	list.push_back(21);
	list.push_back(22);
	cout << list.size() << endl;
	cout << list.empty() << endl;
	cout << "================" << endl;
	cout << list.front() << endl;
	cout << list.back() <<endl;
	list.pop_front();
	list.pop_back();
	cout << list.size() << endl;
	cout << list.empty() << endl;
	cout << "================" << endl;
	list.remove(5);
	cout << list.size() << endl;
	cout << list.empty() << endl;
	list.clear();
	cout << list.size() << endl;
	cout << list.empty() << endl;
	return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值