/*
使用嵌套类实现【双链表】
学习要点:、
(1)实现双链表数据结构的定义以及各种操作
(1)区别深拷贝(另外动态分配内存进行拷贝) 和 浅拷贝(直接拷贝赋值)
(2)实现深拷贝构造和深拷贝赋值函数
(3)实现操作运算符的重载
= (深拷贝赋值)
<< 输出运算符重载:建立全局函数,在类中定义时可声明为friend函数;
friend函数可访问类中private成员
*/
#include<iostream>
using namespace std;
class List{
public:
//构造函数 和 析构函数
List(void) : 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;
}
//得到首元素
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, 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;
}
}
//得到尾元素
int& back(void){
if(empty()){
throw underflow_error("list under flow");
}
return m_tail->m_data;
}
const int& back(void) const {
return const_cast<List*>(this)->back();
}
//插入尾元素
void push_back(const int& 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 int& 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:
int m_data;
Node* m_prev;
Node* m_next;
Node(int data, Node* prev = NULL, Node* next = NULL):m_data(data), m_prev(prev), m_next(next){}
/* 注意:重载输出/入运算符, 要定义全局函数,在类内定义时可定义为friend函数 */
//打印节点
friend ostream& operator<<(ostream& os, const Node& node){
return os << "["<< node.m_data << "]";
}
};
Node* m_head;
Node* m_tail;
};
//主函数测试
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;
}
数据结构之——双链表的实现
最新推荐文章于 2024-08-20 17:16:26 发布