#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
struct ListNode
{
ListNode *_next;
ListNode *_prev;
ListNode _data;
ListNode(DataType x)
:_data(x)
, _next(NULL)
, _prev(NULL)
{}
};
class List
{
typedef ListNode Node;
public:
List()
:_head(new Node(DataType()))
{
_head->_next = _head;
_head-> _prev = _head;
}
List(const List &l)
:_head(new Node(DataType()))
{
_head-> _next = _head;
_head -> _prev = _head;
Node *cur = l._head-> _next;
while(cur != l._head)
{
PushBack(cur-> _data);
cur = cur->_next;
}
}
List& operator=(List l)
{
swap(_head, l._head);
return *this;
}
~List()
{
Node *cur = _head->_next;
while(cur != _head)
{
Node *next = cur->_next;
delete cur;
cur=next;
}
delete _head;
_head = NULL;
}
void PushBack(DataType x)
{
Node *tail = _head->_prev;
Node *newnode = new Node(x);
tail->_next = newnode;
newnode->prev = tail;
newnode->_next = _head;
_head->_prev=newnode;
//Insert(_head,x);
}
void PushFront(DataType x)
{
Insert(_head->_next, x);
}
void PopBack()
{
Erase(_head->_prev);
}
void PopFront()
{
Erase(_head->_next);
}
void Insert(Node *pos, DataType x)
{
assert(pos);
Node *newnode = new Node(x);
Node *prev = pos->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = pos;
pos->_prev = newnode;
}
void Erase(Node *pos)
{
assert(pos != _head);
Node *prev = pos->_prev;
Node *next = pos->_next;
prev->_next=next;
next->_prerv=prev;
delete pos;
}
void Print()
{
Node *cur = _head->_next;
while(cur != _head)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
private:
Node *_head;
};
如果有什么不对的地方,可以评论告诉我,望指导!