C++实现双向链表
来源:互联网 发布:什么是淘宝订购参谋 编辑:程序博客网 时间:2024/06/05 22:48
#pragma once
typedef int DataType;
struct ListNode
{
ListNode* _next;
ListNode* _prev;
DataType _data;
struct ListNode
{
ListNode* _next;
ListNode* _prev;
DataType _data;
ListNode(const DataType& x)
:_data(x)
,_next(NULL)
,_prev(NULL)
{}
};
:_data(x)
,_next(NULL)
,_prev(NULL)
{}
};
class List
{
typedef ListNode Node;
public:
List()
:_head(NULL)
,_tail(NULL)
{}
List(const List& l)
:_head(NULL)
,_tail(NULL)
{
Node* cur = l._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
{
typedef ListNode Node;
public:
List()
:_head(NULL)
,_tail(NULL)
{}
List(const List& l)
:_head(NULL)
,_tail(NULL)
{
Node* cur = l._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
List& operator=(List l)
{
swap(_head, l._head);
swap(_tail, l._tail);
}
{
swap(_head, l._head);
swap(_tail, l._tail);
}
void Clear()
{
Node* cur = _head;
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
{
Node* cur = _head;
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = NULL;
}
}
~List()
{
Clear();
}
{
Clear();
}
void PushBack(const DataType& x)
{
if (_tail == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
{
if (_tail == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
}
}
void PopBack()
{
Erase(_tail);
}
{
Erase(_tail);
}
void PushFront(const DataType& x)
{
Insert(_head, x);
}
{
Insert(_head, x);
}
void PopFront()
{
Erase(_head);
}
void Insert(Node* pos, const DataType& x)
{
Node* tmp = new Node(x);
if (_head == pos)
{
if(_head == NULL)
{
_head = _tail = tmp;
}
else
{
tmp->_next = _head;
_head->_prev = tmp;
_head = tmp;
}
}
else
{
Node* prev = pos->_prev;
prev->_next = tmp;
tmp->_prev = prev;
{
Erase(_head);
}
void Insert(Node* pos, const DataType& x)
{
Node* tmp = new Node(x);
if (_head == pos)
{
if(_head == NULL)
{
_head = _tail = tmp;
}
else
{
tmp->_next = _head;
_head->_prev = tmp;
_head = tmp;
}
}
else
{
Node* prev = pos->_prev;
prev->_next = tmp;
tmp->_prev = prev;
tmp->_next = pos;
pos->_prev = tmp;
}
}
pos->_prev = tmp;
}
}
void Erase(Node* pos)
{
if (_head == _tail)
{
assert(_head == pos);
_head = _tail = NULL;
}
else if (pos == _head)
{
_head = _head->_next;
_head->_prev = NULL;
}
else if (pos == _tail)
{
_tail = _tail->_prev;
_tail->_next = NULL;
}
else
{
Node* prev = pos->_prev;
Node* next = pos->_next;
prev->_next = next;
next->_prev = prev;
}
{
if (_head == _tail)
{
assert(_head == pos);
_head = _tail = NULL;
}
else if (pos == _head)
{
_head = _head->_next;
_head->_prev = NULL;
}
else if (pos == _tail)
{
_tail = _tail->_prev;
_tail->_next = NULL;
}
else
{
Node* prev = pos->_prev;
Node* next = pos->_next;
prev->_next = next;
next->_prev = prev;
}
delete pos;
}
}
Node* Find(const DataType& x)
{
Node* cur = _head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
{
Node* cur = _head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
}
return NULL;
}
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout<<cur->_data<<" ";
cur = cur->_next;
}
cout<<endl;
}
{
Node* cur = _head;
while (cur)
{
cout<<cur->_data<<" ";
cur = cur->_next;
}
cout<<endl;
}
void Reverse()
{
Node* cur = _head;
while(cur)
{
swap(cur->_next, cur->_prev);
cur = cur->_prev;
}
{
Node* cur = _head;
while(cur)
{
swap(cur->_next, cur->_prev);
cur = cur->_prev;
}
swap(_head, _tail);
}
}
private:
Node* _head;
Node* _tail;
};
Node* _head;
Node* _tail;
};
void TestList()
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
ListNode* pos = l1.Find(3);
l1.Insert(pos, 30);
pos = l1.Find(3);
l1.Erase(pos);
l1.Print();
l1.Insert(pos, 30);
pos = l1.Find(3);
l1.Erase(pos);
l1.Print();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Print();
}
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Print();
}
void TestList1()
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
l1.Reverse();
l1.Print();
List l2(l1);
l2.Print();
}
l2.Print();
}
阅读全文
0 0
- C/C++/JAVA 双向链表实现
- C语言实现双向链表删除、插入、双向输出
- 双向链表的实现 c
- 双向链表代码实现-C语言
- 双向链表C语言实现
- C语言实现双向链表[上]
- C语言实现双向链表
- c语言双向链表的实现
- 双向链表的C实现
- 双向链表的C实现
- C语言实现双向循环链表
- c双向链表的实现
- C语言实现双向链表
- 双向链表的C语言实现
- c双向循环链表实现
- 双向链表的C实现
- 双向链表的C实现
- 数据结构(C实现)------- 双向链表
- 51Nod 1085 背包问题
- 数据库_数据库设计1
- CodeForces
- populating-next-right-pointers-in-each-node
- wkhtmltopdf 设置 cookie
- C++实现双向链表
- skb
- Java高并发,如何解决,什么方式解决
- 闲谈团队的代码质量:没有code review的团队没有未来
- 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- STM32F4 FPU和DSP库使用
- ubuntu配置ssh免密码登陆
- 036day(枚举(生理周期)的练习和const关键字的学习)
- Java:通讯录类Address创建,使用