C++实现双向链表
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
typedef int DataType;
typedef struct ListNode
{
DataType data;
ListNode* prev;
ListNode* next;
ListNode(const DataType d)
:data(d)
, prev(NULL)
, next(NULL)
{}
}Node;
class List
{
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=(const List& l)
{
if (this != &l)
{
Clear();
Node* cur = l._head;
while (cur)
{
PushBack(cur->data);
cur = cur->next;
}
}
return *this;
}
List& operator=(const List& l)
{
if (this != &l)
{
List tmp(l);
swap(_head, tmp._head);
swap(_tail, tmp._tail);
}
return *this;
}*/
List& operator=(List l)
{
if (this != &l)
{
Swap(l);
}
return *this;
}
~List()
{
Clear();
}
void PushBack(DataType d)
{
Node* tmp = new Node(d);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
_tail->next = tmp;
tmp->prev = _tail;
_tail = tmp;
_tail->next = NULL;
}
}
void PopBack()
{
if (_head == NULL)
{
cout << "链表为空,无法尾删" << endl;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _tail;
_tail = _tail->prev;
_tail->next = NULL;
delete del;
}
}
void PushFront(DataType d)
{
Node* tmp = new Node(d);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
_head->prev = tmp;
tmp->next = _head;
_head = tmp;
_head->prev = NULL;
}
}
void PopFront()
{
if (_head == NULL)
{
cout << "链表为空,无法头删" << endl;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _head;
_head = _head->next;
_head->prev = NULL;
delete del;
}
}
void Insert(Node* pos, DataType d) //在pos前插入一个数据
{
assert(pos);
Node* tmp = new Node(d);
if (pos == _head)
{
PushFront(d);
}
else
{
pos->prev->next = tmp;
tmp->prev = pos->prev;
tmp->next = pos;
pos->prev = tmp;
}
}
void Erase(Node* pos) //删除pos位置的数据
{
assert(pos);
if (pos == _head)
{
PopFront();
}
else if (pos == _tail)
{
PopBack();
}
else
{
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
delete pos;
}
}
Node* Find(DataType d)
{
Node* cur = _head;
while (cur)
{
if (cur->data == d)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
private:
void Clear()
{
Node* cur = _head;
while (cur)
{
Node* del = cur;
cur = cur->next;
delete[] del;
}
_head = _tail = NULL;
}
void Swap(List& l)
{
swap(_head, l._head);
swap(_tail, l._tail);
}
private:
Node* _head;
Node* _tail;
};
测试函数如下
#include "List.h"
void Test1()
{
List l1;
/*l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();*/
/*List l2(l1);
l2.Print();
List l3;
l3 = l1;
l3.Print();*/
/*l1.PopBack();
l1.Print();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Print();
l1.PopBack();*/
l1.PushFront(4);
l1.PushFront(3);
l1.PushFront(2);
l1.PushFront(1);
l1.Print();
l1.PopFront();
l1.Print();
l1.PopFront();
l1.PopFront();
l1.PopFront();
l1.Print();
l1.PopFront();
}
void Test2()
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
Node* res = l1.Find(2);
/*l1.Insert(res, 0);
l1.Print();*/
l1.Erase(res);
l1.Print();
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}