<span style="font-size:18px;">#include<iostream>
using namespace std;
typedef int DataType;
class LinkNode
{
friend class Slist;
public:
LinkNode(const DataType& x)
:_date(x)
, _prev(NULL)
, _next(NULL)
{
}
public:
DataType _date;
LinkNode* _prev;
LinkNode* _next;
};
class Slist
{
public:
Slist()
:_head(NULL)
, _tail(NULL)
{}
~Slist()
{}
Slist(const Slist& s)
{}
Slist operator=(const Slist& s)
{}
public:
void Print()//打印
{
LinkNode* begin = _head;
while (begin)
{
cout << begin->_date << "->";
begin = begin->_next;
}
cout << "NULL" << endl;
}
void PushBack(const DataType& x)//尾插
{
//1.链表为空
if (_head == NULL)
{
_head = new LinkNode(x);
_tail = _head;
}
//2.有两个以上节点
else
{
LinkNode* tmp = new LinkNode(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
void PopBack()//尾删
{
//1.链表为空
if (_head == NULL)
{
cout << "链表为空!" << endl;
return;
}
//2.只有一个节点
else if (_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
}
//3.有两个以上节点
else
{
LinkNode* del = _tail;
_tail = _tail->_prev;
delete del;
_tail->_next = NULL;
}
}
void PushFront(const DataType& x)//头插
{
//1.链表为空
if (_head == NULL)
{
_head = new LinkNode(x);
_tail=_head;
}
//2.有两个以上节点
else
{
LinkNode *tmp = new LinkNode(x);
tmp->_next = _head;
_head->_prev = tmp;
_head = tmp;
}
}
void PopFront()//头删
{
//1.链表为空
if (_head == NULL)
{
cout << "链表为空!" << endl;
return;
}
//2.只有一个节点
else if (_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
}
//3.有两个以上节点
else
{
LinkNode* del = _head;
_head = _head->_next;
delete del;
_head->_prev = NULL;
}
}
LinkNode* Find(const DataType& x)//查找
{
//1.链表为空
if (_head == NULL)
{
return NULL;
}
//2.链表不为空
else
{
LinkNode* begin = _head;
while (begin!= NULL)
{
if (begin->_date == x)
{
return begin;
}
begin = begin->_next;
}
return NULL;
}
}
void Insert(LinkNode* pos, const DataType& x)//pos后面插入节点
{
//1.链表为空
if (_head == NULL)
{
_head = new LinkNode(x);
_tail = _head;
}
//2.有两个以上节点
else if (pos==_tail)
{
PushBack(x);
}
else
{
LinkNode* tmp = new LinkNode(x);
tmp->_next = pos->_next;
pos->_next->_prev = tmp;
pos->_next = tmp;
tmp->_prev = pos;
}
}
void Erase(LinkNode* del)//删除节点
{
//1.链表为空
if (_head == NULL)
{
cout << "链表为空!" << endl;
return;
}
//2.只有一个节点
else if (_head == _tail)
{
if (_head == del)
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
return;
}
}
//两个以上节点
else
{
if (_head == del)
{
_head = _head->_next;
_head->_prev = NULL;
}
else if (del == _tail)
{
_tail = _tail->_prev;
_tail->_next = NULL;
}
else
{
LinkNode* prev = del->_prev;
LinkNode* next = del->_next;
prev->_next = next;
next->_prev = prev;
}
delete del;
}
}
void Reverse()//逆置
{
LinkNode* begin = _head;
LinkNode* last = _tail;
if (_head == _tail)
{
return;
}
else
{
//while(begin!=last && last->next!=begin)
while (begin!=last)
{
if (last->_next == begin)
{
return;
}
swap(begin->_date, last->_date);
begin = begin->_next;
last = last->_prev;
}
}
}
private:
LinkNode* _head;
LinkNode* _tail;
};
void Test1()
{
Slist s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.Print();
s1.PopBack();
s1.Print();
s1.PopBack();
s1.Print();
s1.PopBack();
s1.Print();
s1.PopBack();
s1.Print();
}
void Test2()
{
Slist s2;
s2.PushFront(1);
s2.PushFront(2);
s2.PushFront(3);
s2.PushFront(4);
s2.Print();
s2.PopFront();
s2.Print();
s2.PopFront();
s2.Print();
s2.PopFront();
s2.Print();
s2.PopFront();
s2.Print();
s2.PopFront();
}
void Test3()
{
Slist s3;
s3.PushBack(1);
s3.PushBack(2);
s3.PushBack(3);
s3.PushBack(4);
s3.Print();
LinkNode* ret1 = s3.Find(1);
cout << ret1->_date << endl;
LinkNode* ret2 = s3.Find(3);
cout << ret2->_date << endl;
LinkNode* ret3 = s3.Find(4);
cout << ret3->_date << endl;
LinkNode* ret4 = s3.Find(5);
cout << ret4 << endl;
}
void Test4()
{
Slist s3;
s3.PushBack(1);
s3.PushBack(2);
s3.PushBack(3);
s3.PushBack(4);
s3.Print();
LinkNode* ret1 = s3.Find(1);
s3.Insert(ret1, 5);
s3.Print();
LinkNode* ret2 = s3.Find(2);
s3.Insert(ret2, 7);
s3.Print();
LinkNode* ret3 = s3.Find(4);
s3.Insert(ret3, 8);
s3.Print();
}
void Test5()
{
Slist s5;
s5.PushBack(1);
s5.PushBack(2);
s5.PushBack(3);
s5.PushBack(4);
//s5.PushBack(5);
s5.Print();
s5.Reverse();
s5.Print();
}
int main()
{
//Test1();
//Test2();
//Test3();
//Test4();
Test5();
return 0;
}</span>循环链表
最新推荐文章于 2020-10-12 11:44:50 发布
726

被折叠的 条评论
为什么被折叠?



