双向循环链表-----C++

简介:

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

#include<assert.h>

using namespace std;

typedef int DataType;

// define double-circular-linked-list-node

struct ListNode

{

DataType _data;

ListNode* _prev;

ListNode* _next;

};

class DCListNode//double-circular-linked-list-node的简称

{

public:

ListNode* NewNode(const DataType& x)

{

ListNode* cur = new ListNode;

cur->_data = x;

cur->_next = NULL;

cur->_prev = NULL;

return cur;

}

void Print()

{

if (_phead == NULL)

{

;//因为函数最后有cout<<"null"<<endl;

}

else if (_phead->_next == _phead)

{

cout << _phead->_data << "-";

}

else

{

ListNode* cur = _phead;

while (cur->_next != _phead)

{

cout << cur->_data << "-";

cur = cur->_next;

}

cout << cur->_data << "-";//因为此时cur指向最后一个节点

}

cout << "null" << endl;

}

void PushBack(DataType x)

{

ListNode* cur = NewNode(x);

if (_phead == NULL)

{

_phead = cur;

cur->_next = _phead;

cur->_prev = _phead;

}

else if (_phead->_next == _phead)

{

_phead->_next = cur;

cur->_prev = _phead;

cur->_next = _phead;

_phead->_prev = cur;

}

else

{

ListNode* tem = _phead->_prev;

tem->_next = cur;

cur->_prev = tem;

cur->_next = _phead;

_phead->_prev = cur;

}

}

void PopBack()

{

if (_phead == NULL)

{

return;

}

else if (_phead->_next == _phead)

{

_phead = NULL;

}

else if (_phead->_next->_next == _phead)

{

_phead->_next = _phead;

_phead->_prev = _phead;

}

else

{

ListNode* tem = _phead->_prev->_prev;

tem->_next = _phead;

_phead->_prev = tem;

}

}

ListNode* Find(DataType x)

{

ListNode* cur = _phead;

if (_phead == NULL)

{

return NULL;

}

else if (_phead->_next == _phead)

{

if (_phead->_data == x)

{

return _phead;

}

else

{

return NULL;

}

}

else

{

if (cur->_prev->_data == x)

{

return cur->_prev;//因为后面的循环不能遍历到最后一个节点

}

while (cur->_next != _phead)

{

if (cur->_data == x)

{

return cur;

}

else

{

cur = cur->_next;

}

}

return NULL;

}

}

void Insert(ListNode* pos,const DataType& x)

{

assert(pos);

ListNode* cur = NewNode(x);

if (pos->_next != _phead)

{

ListNode* tem = pos->_next;

pos->_next = cur;

cur->_prev = pos;

cur->_next = tem;

tem->_prev = cur;

}

else

{

pos->_next = cur;

cur->_prev = pos;

cur->_next = _phead;

_phead->_prev = cur;

}

}

/*void Erase(ListNode* pos)

{

assert(pos);

if (_phead->_next == _phead)

{

delete pos;

_phead = NULL;

}

else

{

ListNode* prev = pos->_prev;

ListNode* next = pos->_next;

prev->_next = next;

next->_prev = prev;

next->_next = prev;

prev->_prev = next;

_phead = next;

delete pos;

}

}*/

void Erase(ListNode* pos)

{

assert(pos);

if (_phead->_next == _phead)//只有一个节点

{

delete pos;

_phead = NULL;

}

ListNode* next = pos->_next;

ListNode* pre = pos->_prev;

if (pos == _phead)// 删除的节点为头节点

{

next->_prev = pre;

pre->_next = next;

_phead = next;

delete pos;

}

else if (pos->_next == _phead)//删除的节点为尾节点

{

pre->_next = next;

next->_prev = pre;

delete pos;

}

else//删除的节点为中间节点

{

pre->_next = next;

next->_prev = pre;

delete pos;

}

}

public:

DCListNode(ListNode* phead = NULL)

:_phead(phead)

{}

~DCListNode()

{

clear();

}

void clear()

{

if (_phead == NULL)

{

;

}

else if (_phead->_next == _phead)

{

delete _phead;

_phead = NULL;

}

else

{

ListNode* cur = _phead;

while (cur->_next != _phead)

{

ListNode* del = cur;

cur = cur->_next;

delete del;

del = NULL;

}

delete cur;//因为此时cur指向最后一个节点

cur = NULL;

}

}

private:

ListNode* _phead;

};

//test PushBack()  PopBack()

void test1()

{

DCListNode s1;

s1.PushBack(1);

s1.PushBack(2);

s1.PushBack(3);

s1.PushBack(4);

s1.PushBack(5);

s1.Print();

s1.PopBack();

s1.Print();

s1.PopBack();

s1.Print();

s1.PopBack();

s1.Print();

s1.PopBack();

s1.Print();

s1.PopBack();

s1.Print();

s1.PopBack();

s1.Print();

}

//test Find()  Insert()  Erase()

void test2()

{

DCListNode s1;

s1.PushBack(1);

s1.PushBack(2);

s1.PushBack(3);

s1.PushBack(4);

s1.PushBack(5);

cout << (s1.Find(2))->_data << endl;

ListNode* p1 = s1.Find(2);

s1.Insert(p1, 10);

s1.Print();

cout << (s1.Find(5))->_data << endl;

ListNode* p2 = s1.Find(5);

s1.Insert(p2, 20);

s1.Print();

ListNode* p3 = s1.Find(3);

s1.Erase(p3);

s1.Print();

}

int main()

{

//test1();

test2();

system("pause");

return 0;

}











本文转自 ye小灰灰  51CTO博客,原文链接:http://blog.51cto.com/10704527/1719275,如需转载请自行联系原作者
目录
相关文章
【链表】还不会用C++实现链表?一文教会你各种链表的实现
【链表】还不会用C++实现链表?一文教会你各种链表的实现
799 0
|
存储 C++
C++的list-map链表与映射表
```markdown C++ 中的`list`和`map`提供链表和映射表功能。`list`是双向链表,支持头尾插入删除(`push_front/push_back/pop_front/pop_back`),迭代器遍历及任意位置插入删除。`map`是键值对集合,自动按键排序,支持直接通过键来添加、修改和删除元素。两者均能使用范围for循环遍历,`map`的`count`函数用于统计键值出现次数。 ```
400 1
|
存储 C++
C++的list-map链表与映射表
这篇教程介绍了C++中`list`链表和`map`映射表的基本使用。`list`链表可通过`push_front()`、`push_back()`、`pop_front()`和`pop_back()`进行元素的添加和删除,使用迭代器遍历并支持在任意位置插入或删除元素。`map`是一个键值对的集合,元素自动按键值排序,可使用下标操作符或`insert()`函数插入元素,通过迭代器遍历并修改键值对,同时提供`count()`方法统计键值出现次数。教程中包含多个示例代码以帮助理解和学习。
458 2
|
算法 C++
c++算法学习笔记 (13) 链表
c++算法学习笔记 (13) 链表
|
C++ Python
UE C++ 链表
UE C++ 链表
|
存储 算法 C++
【数据结构】链表—C/C++实现
【数据结构】链表—C/C++实现
766 1
|
C++ 容器
【C++进阶】深入STL之list:高效双向链表的使用技巧
【C++进阶】深入STL之list:高效双向链表的使用技巧
|
Python Linux Ubuntu
Linux系统部署Python语言开发运行环境
Linux系统部署Python语言开发运行环境
766 0
Linux系统部署Python语言开发运行环境
|
Go Unix 开发者
Go语言time库,时间和日期相关的操作方法
Go语言time库,时间和日期相关的操作方法
637 0
Go语言time库,时间和日期相关的操作方法
|
C++ Java Go
Java每日一练(20230425) 乘积最大子数组、插入区间、删除有序数组中的重复项II
Java每日一练(20230425) 乘积最大子数组、插入区间、删除有序数组中的重复项II
310 0
Java每日一练(20230425) 乘积最大子数组、插入区间、删除有序数组中的重复项II