【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)
来源:互联网 发布:哈布斯堡 知乎 编辑:程序博客网 时间:2024/05/19 16:38
建立源文件List.cpp
include "List.h"int main(){ Test(); system("pause"); return 0;}
建立头文件List.h
#ifndef __LISH_H__#define __LISH_H__#include<iostream>using namespace std;typedef int DataType;struct ListNode{ ListNode(DataType x) :_next(NULL) , _prev(NULL) , _data(x) {} ListNode* _next; ListNode* _prev; DataType _data;};class List{public: List() :_head(NULL) ,_tail(NULL) {} List(const List& s) :_head(NULL) , _tail(NULL) { ListNode* cur = s._head; while (cur) { this->PushBack(cur->_data); cur = cur->_next; } } List& operator= (const List& s) { //先删除节点,再插入节点 if (&s != this) { ListNode* pcur = _head; while (pcur) { ListNode* del = pcur; pcur = pcur->_next; delete del; del = NULL; } ListNode* cur = s._head; while (cur) { this->PushBack(cur->_data); cur = cur->_next; } } return *this; } ~List() { ListNode* cur = _head; while (cur) { ListNode* del = cur; cur = cur->_next; delete del; del = NULL; } } //尾插 void PushBack(DataType x) { //分:0节点 1、多节点两种情况 if (_head == NULL) { _head = new ListNode(x); _tail = _head; _tail->_next = NULL; } else { ListNode* cur = new ListNode(x); _tail->_next = cur; cur->_prev = _tail; _tail = cur; _tail->_next = NULL; } } //尾删 void PopBack() { if (_head == _tail) { if (_head != NULL) { delete _head; _head = NULL; _tail = NULL; } else { return; } } else { ListNode* prev = _tail->_prev; delete _tail; _tail = NULL; _tail = prev; _tail->_next = NULL; } } //头插 void PushFront(DataType x) { if (_head == NULL) { PushBack(x); } else { ListNode* index = new ListNode(x); index->_next = _head; _head->_prev = index; _head = index; _head->_prev = NULL; } } //头删 void PopFront() { if (_head == _tail) { PopBack(); } else { ListNode* del = _head; ListNode* next = _head->_next; _head = next; _head->_prev = NULL; delete del; del = NULL; } } //插入元素 void Insert(size_t pos, DataType x) { //分:是尾插 不是尾插 两种情况 ListNode* cur = _head; while (--pos) { cur = cur->_next; } if (cur == _tail) { PushBack(x); } else { ListNode* index = new ListNode(x); ListNode* prev = cur->_prev; index->_next = cur; cur->_prev = index; prev->_next = index; index->_prev = prev; } } //查找元素 ListNode* Find(DataType x) { ListNode* cur = _head; while (cur) { if (cur->_data == x) { return cur; } cur = cur->_next; } return NULL; } //删除元素 void Erase(ListNode* pos) { if (pos == _head) { PopFront(); } else if (pos == _tail) { PopBack(); } else { ListNode* prev = pos->_prev; ListNode* next = pos->_next; prev->_next = next; next->_prev = prev; delete pos; pos = NULL; } } //逆置方法一:从两头走,交换数据 /*void Reverse() { ListNode* begin = _head; ListNode* end = _tail; while (!((begin == end) || (end->_next == begin))) { swap(begin->_data, end->_data); begin = begin->_next; end = end->_prev; } }*/ //逆置方法二:交换节点的前驱和后继 /*void Reverse() { ListNode* cur = _head; while (cur) { swap(cur->_prev, cur->_next); cur = cur->_prev; } swap(_head, _tail); }*/ //逆置方法三:摘节点,头插该节点 void Reverse() { ListNode* cur = _head; ListNode* newhead = NULL; while (cur) { ListNode* tmp = cur; cur = cur->_next; if (newhead == NULL) { newhead = tmp; newhead->_next = NULL; newhead->_prev = NULL; _head = _tail = newhead; } else { newhead->_prev = tmp; tmp->_next = newhead; newhead = tmp; _head = newhead; _head->_prev = NULL; } } } //打印 void PrintList() { ListNode* cur = _head; while (cur) { cout << cur->_data << "->"; cur = cur->_next; } cout << "NULL" << endl; }private: ListNode* _head; ListNode* _tail;};void Test(){ List s; s.PushBack(1); s.PushBack(2); s.PushBack(3); s.PushBack(4); s.PushBack(5); s.PrintList(); s.PopBack(); s.PrintList(); s.PushFront(0); s.PrintList(); s.PopFront(); s.PrintList(); s.Insert(2, 10); s.PrintList(); s.Reverse(); s.PrintList();}#endif //__LIST_H__
本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1747880
0 0
- 【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)
- 双向链表的实现与操作(C语言实现)
- C语言实现双向链表的基本操作
- C语言实现双向链表的基本操作
- 双向链表的操作实现(c语言)
- 双向链表的相关操作--C语言实现
- c语言实现双向链表的基本操作
- 数据结构 - C语言版 - 双向链表 所有基本操作
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 【总结】逆置双向链表的三种方法
- java实现双向链表的操作
- 双向循环链表操作的实现
- 双向循环链表操作的实现
- 双向链表的基本操作实现
- [C++]双向链表操作
- 双向链表的实现 c
- 【String类浅拷贝的实现】C++:String类引用计数浅拷贝的两种实现
- 【String类】C++:String类的传统写法与现代写法
- 《剑指offer》字符串中的字符替换
- 【C++】实现动态顺序表的PushBack(),PopBack(),PushFront(),PopFront(),Find(),Insert
- 【C++】用类实现单向单链表的尾插PushBack(),尾删PopBack(),打印PrintSlist()。
- 【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 【继承与多态】C++:继承中的赋值兼容规则,子类的成员函数,虚函数(重写),多态
- 【C++】模版类实现普通类静态顺序表
- 【C++】认识模版函数
- 【C++】模版实现双向链表的各种操作(如:逆置、去重Unique、分类(冒泡)、合并)
- 【C++】容器适配器实现栈Stack的各种功能(入栈、出栈、判空、大小、访问所有元素等)
- 【C++】容器适配器实现队列Queue的各种功能(入队、出队、判空、大小、访问所有元素等)
- 论IT就业发展趋势