typedef int DataType; //实现双向链表
struct ListNode
{
ListNode* _next; //设置结点,指向下一节点
ListNode* _prev; //设置结点,指向上一节点
DataType _data; //设置数据结点
ListNode(const DataType& x) //将const DataType转换为ListNode类型
:_next(NULL)
, _prev(NULL)
{
_data = x;
}
};
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;
}
}
List& operator = (const List& l)
{
if (this != &l)
{
Node* cur = l._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
}
~List() //释放内存
{
Node* cur = _head;
while (cur)
{
Node* del = cur;
cur = cur->_next;
delete del;
}
_head = _tail = NULL;
}
public:
void PushBack(const DataType& x)//尾结点插入
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
void PopBack()//尾结点删除
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* prev = _tail->_prev;
delete _tail;
prev->_next = NULL;
_tail = prev;
}
}
void PushFront(const DataType& x)//头结点插入
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
tmp->_next = _head;
_head->_prev = tmp;
_head = tmp;
}
}
void PopFront()//头结点删除
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* next = _head->_next;
delete _head;
next->_prev = NULL;
_head = next;
}
}
void Insert(Node* pos, const DataType& x) //指定位置的插入
{
assert(pos);
//两种情况:
//1.pos == _head
//2.pos != _head
if (pos == _head)
{
PushFront(x);
}
else
{
Node* tmp = new Node(x);
Node* net = pos;
Node* prev = net->_prev;
prev->_next = tmp;
tmp->_next = net;
net->_prev = tmp;
tmp->_prev = prev;
}
}
void Erase(Node* pos) //删除指定位置
{
assert(pos);
if (pos == _head)
{
PopFront();
}
else if (pos == _tail)
{
PopBack();
}
else
{
Node* cur = pos;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
cur = NULL;
}
}
Node* Find(const DataType& x) //寻找x的位置
{
Node* cur = _head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return (Node*)-1;
}
void Reverse() //逆置
{
//Node* cur = _head; // 交换next和prev
//while (cur)
//{
// swap(cur->_prev, cur->_next);
// cur = cur->_prev;
//}
/*swap(_head, _tail);*/
Node* head = _head; //交换头尾结点的数据
Node* tail = _tail;
while (!(head == tail || tail->_next == head))
{
swap(head->_data, tail->_data);
head = head->_next;
tail = tail->_prev;
}
}
void Print() //链表的打印
{
Node* cur = _head;
while (cur)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
private:
Node* _head; //头结点
Node* _tail; //尾结点
};
void TextList()
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
List l2(l1);
l2.Print();
l2.PopBack();
l2.Print();
//l1.PopBack();
//l1.PopBack();
//l1.PopBack();
//l1.PopBack();
//l1.Print();
/*l1.PushFront(4);
l1.PushFront(3);
l1.PushFront(2);
l1.PushFront(1);
l1.Print();*/
/*l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Print();*/
//l1.PopFront();
//l1.PopFront();
//l1.PopFront();
//l1.PopFront();
//l1.PopFront();
//l1.Print();
/*l1.Insert(l1.Find(2), 6);
l1.Print();
l1.Erase(l1.Find(2));
l1.Print();*/
l1.Reverse();
l1.Print();
}
#include<iostream>
using namespace std;
#include<windows.h>
#include<assert.h>
#include"List.h"
int main()
{
TextList();
system("pause");
return 0;
}