#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
struct ListNode
{
ListNode* prev;
ListNode* next;
DataType data;
ListNode(DataType x)
:data(x)
,next(NULL)
,prev(NULL)
{
;
}
};
typedef ListNode Node;
class List
{
public:
List()
:_head(NULL)
, _tail(NULL)
{}
void PushBack(const DataType x)
{
//1.链表为空
if (_head == NULL)
{
_head = Buynode(x);
_tail = _head;
}
//2.链表不为空
else
{
Node* tmp = _tail;
_tail->next = Buynode(x);
_tail = _tail->next;
_tail->prev = tmp;
}
}
void PushFront(const DataType&x)
{
//1.链表为空
if (_head == NULL)
{
_head = Buynode(x);
_tail = _head;
}
//2.链表不为空
else
{
Node* tmp = _head;
_head->prev = Buynode(x);
_head = _head->prev;
_head->next = tmp;
}
}
void PopBack()
{
//1.链表为空
if (_tail == NULL)
{
return;
}
//2.只有一个节点
else if (_tail == NULL)
{
delete _tail;
_head = _tail = NULL;
}
//3.有多个节点
else
{
Node* tmp = _tail;
_tail = _tail->prev;
_tail->next = NULL;
delete tmp;
}
}
void PopFront()
{
//1.链表为空
if (_head == NULL)
{
return;
}
//2.只有一个节点
else if (_head->next == NULL)
{
delete _head;
_head = _tail = NULL;
}
//3.有多个节点
else
{
Node* tmp = _head;
_head = _head->next;
_head->prev = NULL;
delete tmp;
}
}
Node* Find(const DataType& x)
{
Node* cur = _head;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return cur;
}
void Insert(Node* pos, const DataType&x)//目标节点前插
{
assert(pos);
//目标节点是头结点直接前插
if (pos == _head)
{
PushFront(x);
}
else
{
Node*tmp = Buynode(x);
tmp->prev = pos->prev;
pos->prev->next = tmp;
tmp->next = pos;
pos->prev = tmp;
}
}
void Erase(Node* pos)
{
assert(pos);
//1.要删除节点是头结点直接调用头删
if (pos == _head)
{
PopFront();
}
//2.目标节点是尾节点直接调用尾删
else if (pos == _tail)
{
PopBack();
}
else
{
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
delete pos;
pos = NULL;
}
}
size_t Size() //求双链表节点个数
{
if (_head == NULL)
{
return 0;
}
size_t count = 0;
Node* cur = _head;
while (cur)
{
cur = cur->next;
count++;
}
return count;
}
bool Empty()const //判断链表是否为空
{
if (_head == _tail == NULL)
{
return true;
}
return false;
}
List(const List& l) //链表的拷贝构造
:_head(NULL)
,_tail(NULL)
{
Node* tmp = l._head;
while (tmp)
{
PushBack(tmp->data);
tmp = tmp->next;
}
}
void Swap(List &l)
{
swap(_head, l._head);
swap(_tail, l._tail);
}
List&operator = (const List& l) //赋值运算符重载
{
if (this != &l)
{
List tmp(l);
Swap(tmp);
}
return *this;
}
void Display()const
{
if (_head == NULL)
{
cout << "NULL" << endl;
}
Node* cur = _head;
while (cur)
{
cout << cur->data << "->";
cur = cur->next;
}
cout << endl;
Node* tmp = _tail;
while (tmp)
{
cout << tmp->data << "->";
tmp = tmp->prev;
}
cout << endl;
}
private:
Node* Buynode(const DataType& data)
{
return new Node(data);
}
private:
Node* _head;
Node* _tail;
};
void test1()
{
List lel;
lel.PushBack(1);
lel.PushBack(2);
lel.PushBack(3);
lel.PushBack(4);
lel.Display();
lel.PushFront(0);
lel.Display();
lel.PopFront();
lel.PopBack();
lel.Display();
Node* aim = lel.Find(3);
lel.Insert(aim, 8);
lel.Display();
lel.Erase(aim);
lel.Display();
List tet;
tet = lel;
tet.Display();
int i = lel.Empty();
cout << i << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
C++ 实现双向链表
最新推荐文章于 2024-04-04 08:50:11 发布