数据结构之——单链表的实现
2016-03-15 02:22
393 查看
/* 使用嵌套类实现【单链表】 学习要点:、 (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; }
相关文章推荐
- 学习笔记:第8章 数据结构与算法
- 数据结构与算法学习(二)
- 数据结构与算法之链表
- 第14章 数据结构扩张 区间树部分代码
- 数据结构/算法 - 桶排序
- 第十四章 数据结构扩张 动态顺序统计部分代码
- 《数据结构》2.3求两个递增链表的交集
- 数据结构(RMQ):POJ 3624 Balanced Lineup
- uva 11997——K Smallest Sums
- uva 1203—— Argus
- 数据结构和二叉树操作的封装
- 【数据结构与算法】(五) c 语言递归与汉诺塔实现
- 数据结构术语和概念的明确
- 数据结构入门
- Java数据结构笔记2——数组线性表类(ArrayList)
- JAVA数据结构---循环队列
- JAVA数据结构---循环队列
- JAVA数据结构---循环队列
- JAVA数据结构---循环队列
- JAVA数据结构---循环队列