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

相关文章

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

java中BigDecimal里面的subtract函数介绍及实现方法

《java中BigDecimal里面的subtract函数介绍及实现方法》在Java中实现减法操作需要根据数据类型选择不同方法,主要分为数值型减法和字符串减法两种场景,本文给大家介绍java中BigD... 目录Java中BigDecimal里面的subtract函数的意思?一、数值型减法(高精度计算)1.

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示