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

相关文章

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

C++读写word文档(.docx)DuckX库的使用详解

《C++读写word文档(.docx)DuckX库的使用详解》DuckX是C++库,用于创建/编辑.docx文件,支持读取文档、添加段落/片段、编辑表格,解决中文乱码需更改编码方案,进阶功能含文本替换... 目录一、基本用法1. 读取文档3. 添加段落4. 添加片段3. 编辑表格二、进阶用法1. 文本替换2

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Python Excel 通用筛选函数的实现

《PythonExcel通用筛选函数的实现》本文主要介绍了PythonExcel通用筛选函数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录案例目的示例数据假定数据来源是字典优化:通用CSV数据处理函数使用说明使用示例注意事项案例目的第一

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法