#include<iostream>
using namespace std;
typedef int DataType;
struct Node //结点
{
Node* _pNext;
Node* _pPre;
DataType _data;
Node(const DataType& data = DataType()) //创造新结点
: _pNext(NULL)
, _pPre(NULL)
, _data(data)
{}
};
class List
{
public:
List() //构造
: _pHead(new Node)
{
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
}
List(const List& l) //拷贝
:_pHead(new Node)
{
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
Node *pcur = l._pHead->_pNext;
while (pcur != l._pHead)
{
PushBack(pcur->_data);
pcur = pcur->_pNext;
}
}
List& operator=(List& l)//赋值运算符重载
{
swap(_pHead, l._pHead);
return *this;
}
~List() //析构
{
Node *pcur = _pHead->_pNext;
Node *pre = NULL;
while (pcur != _pHead)
{
pre = pcur;
pcur = pcur->_pNext;
delete[]pre;
}
delete[]pcur;
_pHead = NULL;
}
void PushBack(DataType data) //尾插
{
Node *newnode = new Node(data);//*********必须把空间创造出来,不能直接调用Node中的Node
Node *ptail = NULL;
ptail = _pHead->_pPre;
ptail->_pNext = newnode;
newnode->_pPre = ptail;
_pHead->_pPre = newnode;
newnode->_pNext = _pHead;
}
void PopBack() //尾删
{
if (Empty())
{
Node *pretail = _pHead->_pPre->_pPre;
free(_pHead->_pPre);
_pHead->_pPre = pretail;
pretail->_pNext = _pHead;
}
return;
}
void PushFront(DataType data) //头插
{
Node *newnode = new Node(data);
Node *ptail = NULL;
ptail = _pHead->_pNext;
ptail->_pPre = newnode;
_pHead->_pNext = newnode;
newnode->_pPre = _pHead;
newnode->_pNext = ptail;
}
void PopFront() //头删
{
if (Empty())
{
Node *pdel = _pHead->_pNext;
_pHead->_pNext = pdel->_pNext;
pdel->_pNext->_pPre = _pHead;
free(pdel);
}
return;
}
void Insert(Node* pos, DataType data) //任意位置插入
{
Node *newnode = new Node(data);
pos->_pPre->_pNext = newnode;
newnode->_pPre = pos->_pPre;
pos->_pPre = newnode;
newnode->_pNext = pos;
}
void Erase(Node* pos) //任意位置删除
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
free(pos);
}
size_t Size() //链表大小
{
size_t count = 0;
if (Empty())
{
Node *pcur = _pHead->_pNext;
while (pcur != _pHead)
{
count++;
pcur = pcur->_pNext;
}
return count;
}
return 0;
}
int Empty()const //链表是否为空
{
if (_pHead->_pNext == _pHead)
{
return 0;
}
return 1;
}
Node *Find(DataType data)
{
Node *pcur = NULL;
pcur = _pHead->_pNext;
while (pcur != _pHead)
{
if (pcur->_data == data)
{
return pcur;
}
pcur = pcur->_pNext;
}
return NULL;
}
void PrintList()
{
Node *pcur = NULL;
pcur = _pHead->_pNext;
while (pcur != _pHead)
{
cout << pcur->_data << "--->";
pcur = pcur->_pNext;
}
cout << "NULL" << endl;
}
private:
Node* _pHead;
};
int main()
{
List l1;
Node *pos;
size_t size;
l1.PushBack(3);
l1.PushBack(4);
l1.PushBack(5);
l1.PushBack(6);
l1.PushBack(7);
l1.PopBack();
l1.PrintList();
l1.PushFront(2);
l1.PushFront(1);
l1.PrintList();
l1.PopFront();
l1.PopFront();
l1.PrintList();
pos = l1.Find(4);
l1.Insert(pos, 8);
l1.PrintList();
l1.Erase(pos);
l1.PrintList();
size=l1.Size();
List l2(l1);
l2.PrintList();
List l3;
l3 = l1;
l3.PrintList();
return 0;
using namespace std;
typedef int DataType;
struct Node //结点
{
Node* _pNext;
Node* _pPre;
DataType _data;
Node(const DataType& data = DataType()) //创造新结点
: _pNext(NULL)
, _pPre(NULL)
, _data(data)
{}
};
class List
{
public:
List() //构造
: _pHead(new Node)
{
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
}
List(const List& l) //拷贝
:_pHead(new Node)
{
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
Node *pcur = l._pHead->_pNext;
while (pcur != l._pHead)
{
PushBack(pcur->_data);
pcur = pcur->_pNext;
}
}
List& operator=(List& l)//赋值运算符重载
{
swap(_pHead, l._pHead);
return *this;
}
~List() //析构
{
Node *pcur = _pHead->_pNext;
Node *pre = NULL;
while (pcur != _pHead)
{
pre = pcur;
pcur = pcur->_pNext;
delete[]pre;
}
delete[]pcur;
_pHead = NULL;
}
void PushBack(DataType data) //尾插
{
Node *newnode = new Node(data);//*********必须把空间创造出来,不能直接调用Node中的Node
Node *ptail = NULL;
ptail = _pHead->_pPre;
ptail->_pNext = newnode;
newnode->_pPre = ptail;
_pHead->_pPre = newnode;
newnode->_pNext = _pHead;
}
void PopBack() //尾删
{
if (Empty())
{
Node *pretail = _pHead->_pPre->_pPre;
free(_pHead->_pPre);
_pHead->_pPre = pretail;
pretail->_pNext = _pHead;
}
return;
}
void PushFront(DataType data) //头插
{
Node *newnode = new Node(data);
Node *ptail = NULL;
ptail = _pHead->_pNext;
ptail->_pPre = newnode;
_pHead->_pNext = newnode;
newnode->_pPre = _pHead;
newnode->_pNext = ptail;
}
void PopFront() //头删
{
if (Empty())
{
Node *pdel = _pHead->_pNext;
_pHead->_pNext = pdel->_pNext;
pdel->_pNext->_pPre = _pHead;
free(pdel);
}
return;
}
void Insert(Node* pos, DataType data) //任意位置插入
{
Node *newnode = new Node(data);
pos->_pPre->_pNext = newnode;
newnode->_pPre = pos->_pPre;
pos->_pPre = newnode;
newnode->_pNext = pos;
}
void Erase(Node* pos) //任意位置删除
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
free(pos);
}
size_t Size() //链表大小
{
size_t count = 0;
if (Empty())
{
Node *pcur = _pHead->_pNext;
while (pcur != _pHead)
{
count++;
pcur = pcur->_pNext;
}
return count;
}
return 0;
}
int Empty()const //链表是否为空
{
if (_pHead->_pNext == _pHead)
{
return 0;
}
return 1;
}
Node *Find(DataType data)
{
Node *pcur = NULL;
pcur = _pHead->_pNext;
while (pcur != _pHead)
{
if (pcur->_data == data)
{
return pcur;
}
pcur = pcur->_pNext;
}
return NULL;
}
void PrintList()
{
Node *pcur = NULL;
pcur = _pHead->_pNext;
while (pcur != _pHead)
{
cout << pcur->_data << "--->";
pcur = pcur->_pNext;
}
cout << "NULL" << endl;
}
private:
Node* _pHead;
};
int main()
{
List l1;
Node *pos;
size_t size;
l1.PushBack(3);
l1.PushBack(4);
l1.PushBack(5);
l1.PushBack(6);
l1.PushBack(7);
l1.PrintList();
l1.PopBack();l1.PopBack();
l1.PrintList();
l1.PushFront(2);
l1.PushFront(1);
l1.PrintList();
l1.PopFront();
l1.PopFront();
l1.PrintList();
pos = l1.Find(4);
l1.Insert(pos, 8);
l1.PrintList();
l1.Erase(pos);
l1.PrintList();
size=l1.Size();
List l2(l1);
l2.PrintList();
List l3;
l3 = l1;
l3.PrintList();
return 0;
}
若是对于双向链表的尾插、尾删等上述操作不明白,可以参照我以前写的博客https://mp.csdn.net/postedit/80098311
若是对深拷贝不理解的,可以参照我以前写的博客https://mp.csdn.net/postedit/80075175
希望对大家有所帮助!!!!!!