1️⃣4️⃣ 双向链表
#include <iostream>
using namespace std;
class DoubleList{
private:
struct DoubleNode{
DoubleNode* previous;
int data;
DoubleNode* next;
DoubleNode(){}
DoubleNode(int d){
data = d;
previous = next = 0;
}
};
DoubleNode* head;
public:
DoubleList();
void Insert(int i);
void Delete(int i);
void Show()const;
};
DoubleList::DoubleList(){
head = new DoubleNode();
head->previous = head;
head->next = head;
}
void DoubleList::Insert(int i){
DoubleNode* p = new DoubleNode(i);
p->next = head->next;
p->previous = head;
head->next = p;
}
void DoubleList::Delete(int i){
DoubleNode* p = head->next;
DoubleNode* q = head;
while(p!=head&&p->data!=i){
q = p;
p = p->next;
}
if(p!=head){
p->next->previous = q;
q->next = p->next;
delete p;
}
}
void DoubleList::Show()const{
DoubleNode* p = head->next;
while(p!=head){
cout << p->data << " ";
p = p->next;
}
}
int main() {
DoubleList l1;
l1.Insert(1);
l1.Insert(2);
l1.Insert(3);
l1.Insert(4);
l1.Show(); cout << endl;
l1.Delete(2);
l1.Show();
return 0;
}