c++双链表【构造函数、运算符重载、析构函数、增删查改及逆置等】

本文主要是介绍c++双链表【构造函数、运算符重载、析构函数、增删查改及逆置等】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

c++中的双向链表写法,主要实现(增删查改,链表逆置,构造函数,运算符重载,等)

建立头文件SList.h

#pragma oncetypedef int DataType;
class ListNode
{friend class List;//友元函数
public:ListNode(const DataType x):_data(x), _prev(NULL), _next(NULL){}
private:DataType _data;ListNode* _prev;ListNode* _next;
};
class List
{
public:List():_head(NULL), _tail(NULL){}//深拷贝List(const List& s):_head(NULL), _tail(NULL){ListNode* tmp = s._head;while (tmp){this->PushBack(tmp->_data);tmp = tmp->_next;}}//现代写法List& operator=(List& s){swap(_head, s._head);swap(_tail, s._tail);return *this;}
public:void Clear();void PrintList();void PushBack(DataType x);void PopBack();void PushFront(DataType x);void PopFront();void Insert(ListNode* pos, DataType x);void Erase(ListNode* pos);ListNode* Find(DataType x);//void Reverse();List Reverse();
private:ListNode* _head;ListNode* _tail;
};

各函数的实现

#include<iostream>
using namespace std;#include"List.h"
#include<assert.h>void List::Clear()//清除双链表
{ListNode* cur = _head;while (cur){ListNode* del = cur;cur = cur->_next;delete del;del = NULL;}
}void List::PrintList()//打印双链表
{ListNode* cur=_head;while (cur){cout << cur->_data << "->";cur = cur->_next;}cout << "NULL" << endl;
}void List::PushBack(DataType x)//尾插
{if (NULL == _head){_head = new ListNode(x);_tail = _head;}else{ListNode* tmp = new ListNode(x); _tail->_next = tmp;tmp->_prev = _tail;tmp->_next = NULL;_tail = tmp;}
}void List::PopBack()//尾删
{if (NULL == _head){cout << "List is empty!" << endl;}else if (_head == _tail){delete _head;_head = _tail = NULL;}else{//相比单链表方便找到尾节点的前一个节点ListNode* cur = _tail;_tail = cur->_prev;_tail->_next = NULL;delete cur;cur = NULL;}
}void List::PushFront(DataType x)//头插
{ListNode* tmp = _head;_head = new ListNode(x);_head->_prev = NULL;_head->_next = tmp;
}void List::PopFront()//头删
{if (NULL == _head){cout << "SList is empty!" << endl;}else if (NULL == _head->_next){delete _head;_head = NULL;}else{ListNode* tmp = _head->_next;delete _head;_head = tmp;tmp->_prev = NULL;}
}ListNode* List::Find(DataType x)//查找x
{ListNode* cur = _head;while (cur){if (x == cur->_data)return cur;cur = cur->_next;}return NULL;
}void List::Insert(ListNode* pos, DataType x)//指定位置处插入x
{assert(pos);if (NULL == pos->_next)List::PushBack(x);else if (_head == pos)List::PushFront(x);else{ListNode* cur = new ListNode(x);ListNode* prev = pos->_prev;prev->_next = cur;cur->_prev = prev;cur->_next = pos;pos->_prev = cur;}
}void List::Erase(ListNode* pos)//删除结点pos
{assert(pos);if (NULL == pos->_next)List::PopBack();else if (_head == pos)List::PopFront();else{ListNode* prev = pos->_prev;ListNode* next = pos->_next;next->_prev = prev;prev->_next = next;delete pos;pos = NULL;}
}
//逆置双链表
//通过两个指针,从两边向中间移动,交换所储蓄内容
//void List::Reverse()
//{
//	ListNode* begin = _head;
//	ListNode* end = _tail;
//	//奇数个节点时两个指针相等时结束循环;偶数个节点时两个指针发生交叉时结束循环
//	while (begin != end && begin->_prev != end)
//	{
//		swap(begin->_data, end->_data);
//		begin = begin->_next;
//		end = end->_prev;
//	}
//}//交换头尾指针,交换每个结点的前驱和后继
//void List::Reverse()
//{
//	swap(_head, _tail);
//	ListNode* cur = _head;
//	while (cur)
//	{
//		swap(cur->_prev,cur->_next);
//		cur = cur->_next;
//	}
//}//建立新链表,通过头插法实现
List List::Reverse()
{if (NULL == _head){cout << "SList is empty!" << endl;}else if(NULL != _head->_next){List NewList;ListNode* cur = _head->_next;ListNode* tmp = _head;//保存头指针,头插完后使其_next指针指向空while (cur){this->PushFront(cur->_data);cur = cur->_next;}tmp->_next = NULL;return NewList;}return *this;
}

本文出自 “Scen” 博客,请务必保留此出处http://10741357.blog.51cto.com/10731357/1748592

这篇关于c++双链表【构造函数、运算符重载、析构函数、增删查改及逆置等】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/332190

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

Java方法重载与重写之同名方法的双面魔法(最新整理)

《Java方法重载与重写之同名方法的双面魔法(最新整理)》文章介绍了Java中的方法重载Overloading和方法重写Overriding的区别联系,方法重载是指在同一个类中,允许存在多个方法名相同... 目录Java方法重载与重写:同名方法的双面魔法方法重载(Overloading):同门师兄弟的不同绝

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C# List.Sort四种重载总结

《C#List.Sort四种重载总结》本文详细分析了C#中List.Sort()方法的四种重载形式及其实现原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录1. Sort方法的四种重载2. 具体使用- List.Sort();- IComparable

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

Python海象运算符:=的具体实现

《Python海象运算符:=的具体实现》海象运算符又称​​赋值表达式,Python3.8后可用,其核心设计是在表达式内部完成变量赋值并返回该值,从而简化代码逻辑,下面就来详细的介绍一下如何使用,感兴趣... 目录简介​​条件判断优化循环控制简化​推导式高效计算​正则匹配与数据提取​性能对比简介海象运算符

C语言逗号运算符和逗号表达式的使用小结

《C语言逗号运算符和逗号表达式的使用小结》本文详细介绍了C语言中的逗号运算符和逗号表达式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 在C语言中逗号“,”也是一种运算符,称为逗号运算符。 其功能是把两个表达式连接其一般形式为:表达

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u