单循环链表:(典型应用)约瑟夫环 自行了解
#include<iostream>
using namespace std;
class CircleLink
{
public:
CircleLink()
{
_head = new Node();
_tail = _head;
_head->_next = _head;
}
~CircleLink()
{
Node* p = _head->_next;
while (p != _head)
{
_head->_next = p->_next;
delete p;
p = _head->_next;
}
delete _head;
}
//尾插法
void InsertTail(int val)
{
Node* node = new Node(val);
node->_next = _tail->_next;
_tail->_next = node;
_tail = node;
}
void InsertHead(int val)
{
Node* node = new Node(val);
node->_next = _head->_next;
_head->_next = node;
if (node->_next == _head)
{
_tail = node;
}
}
//删除节点
void Remove(int val)
{
Node* head = _head;
Node* q = head;
Node* p = head->_next;
while (p != head)
{
if (p->_data == val)
{
q->_next = p->_next;
delete p;
if (q->_next == head)
{
_tail = q;
}
return;
}
else
{
q = p;
p = p->_next;
}
}
}
bool find(int val)
{
Node* p = _head->_next;
while (p != _head)
{
if (val == p->_data)return true;
else
{
p = p->_next;
}
}
return false;
}
void show()
{
Node* p = _head->_next;
while (p != _head)
{
cout << p->_data << " ";
p = p->_next;
}
cout << endl;
}
private:
struct Node
{
Node(int data = 0) :_data(data), _next(nullptr) {}
int _data;
Node* _next;
};
Node* _head;//指向头节点
Node* _tail;//指向末尾节点
};
int main()
{
CircleLink a;
a.InsertHead(10);
a.InsertHead(20);
a.InsertHead(40);
a.InsertHead(30);
a.show();
;
return 0;
}
双链表基操:
#include<iostream>
using namespace std;
struct Node
{
Node(int data = 0) :
_data(data),
_next(nullptr),
_pre(nullptr)
{}
int _data;
Node* _next;
Node* _pre;
};
class DoubleLink
{
public:
DoubleLink()
{
_head = new Node();
}
~DoubleLink()
{
Node* p = _head;
while (p != nullptr)
{
_head = _head->_next;
delete p;
p = _head;
}
}
public:
//头插法
void InsertHead(int val)
{
Node* node = new Node(val);
node->_next = _head->_next;
node->_pre = _head;
if (_head->_next != nullptr) //防止后面是空的
{
node->_next->_pre = node;
}
_head->_next = node;
}
void InsertTail(int val)
{
Node* node = new Node(val);
Node* p = _head;
while (p->_next != nullptr)
{
p = p->_next;
}
node->_pre = p;
p->_next = node;
}
void Remove(int val)
{
Node* p = _head;
while (p != nullptr)
{
if (p->_data == val)
{
p->_pre->_next = p->_next;
if (p->_next != nullptr)p->_next->_pre = p->_pre;
delete p;
return;
}
else
{
p = p->_next;
}
}
}
bool find(int val)
{
Node* p = _head;
while (p != nullptr)
{
if (p->_data == val)
{
return true;
}
else
{
p = p->_next;
}
}
return false;
}
void show()
{
Node* p = _head->_next;
while (p != nullptr)
{
cout << p->_data << " ";
p = p->_next;
}
cout << endl;
}
private:
Node* _head;
};
int main()
{
DoubleLink a;
a.InsertHead(10);
a.InsertHead(20);
a.InsertHead(30);
a.InsertHead(40);
a.show();
a.Remove(20);
a.show();
return 0;
}
双循环链表基操:
#include<iostream>
using namespace std;
struct Node
{
Node(int data = 0)
:_data(data),
_next(nullptr),
_pre(nullptr)
{}
int _data;
Node* _next;
Node* _pre;
};
class DoubleCircleLink
{
public:
DoubleCircleLink()
{
_head = new Node();
_head->_next = _head;
_head->_pre = _head;
}
~DoubleCircleLink()
{
Node* p = _head->_next;
while (p != _head)
{
_head->_next = p->_next;
p->_next->_pre = _head;
delete p;
p = _head->_next;
}
delete _head;
_head = nullptr;
}
public:
void InsertHead(int val)
{
Node* node = new Node(val);
node->_next = _head->_next;
node->_pre = _head;
_head->_next->_pre = node;
_head->_next = node;
}
//尾插
void InsertTail(int val)
{
Node* node = new Node(val);
Node* p = _head->_pre;
node->_pre = p;
p->_next = node;
node->_next = _head;
_head->_pre = node;
}
void Remove(int val)
{
Node* p = _head->_next;
while (p != _head)
{
if (p->_data == val)
{
p->_pre->_next = p->_next;
p->_next->_pre = p->_pre;
delete p;
return;
}
else
{
p = p->_next;
}
}
}
bool find(int val)
{
Node* p = _head->_next;
while (p != _head)
{
if (p->_data == val)
{
return true;
}
else
{
p = p->_next;
}
}
}
void show()
{
Node* p = _head->_next;
while (p->_next != _head)
{
cout << p->_data << " ";
p = p->_next;
}
cout << endl;
}
private:
Node* _head;
};
int main()
{
DoubleCircleLink a;
a.InsertTail(10);
a.InsertTail(20);
a.InsertTail(30);
a.InsertTail(40);
a.InsertTail(50);
cout << a.find(20) << endl;
a.show();
return 0;
}