1.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node *prev,*next;
Node(): prev(NULL),next(NULL){}
Node(int _val): val(_val),prev(NULL),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node *head=new Node(),*tail=new Node();//哨兵
head->next=tail,tail->prev=head;
print(head);
auto a=new Node(1);
a->next=head->next;
a->prev=head;
head->next->prev=a;
head->next=a;
print(head);
return 0;
}
2.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node *prev,*next;
Node(): prev(NULL),next(NULL){}
Node(int _val): val(_val),prev(NULL),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node *head=new Node(),*tail=new Node();//哨兵
head->next=tail,tail->prev=head;
print(head);
auto a=new Node(1);
a->next=head->next;
a->prev=head;
head->next->prev=a;
head->next=a;
auto b=new Node(2);
b->next=head->next;
b->prev=head;
head->next->prev=b;
head->next=b;
print(head);
return 0;
}
3.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node *prev,*next;
Node(): prev(NULL),next(NULL){}
Node(int _val): val(_val),prev(NULL),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node *head=new Node(),*tail=new Node();//哨兵
head->next=tail,tail->prev=head;
print(head);
auto a=new Node(1);
a->next=head->next;
a->prev=head;
head->next->prev=a;
head->next=a;
auto b=new Node(2);
b->next=a->next;
b->prev=a;
a->next->prev=b;
a->next=b;
print(head);
return 0;
}
4.delete
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node *prev,*next;
Node(): prev(NULL),next(NULL){}
Node(int _val): val(_val),prev(NULL),next(NULL){}
};
void print(Node *head)
{
for(auto p=head;p;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node *head=new Node(),*tail=new Node();//哨兵
head->next=tail,tail->prev=head;
print(head);
auto a=new Node(1);
a->next=head->next;
a->prev=head;
head->next->prev=a;
head->next=a;
auto b=new Node(2);
b->next=a->next;
b->prev=a;
a->next->prev=b;
a->next=b;
print(head);
//shanchu
b->prev->next=b->next;
b->next->prev=b->prev;
print(head);
return 0;
}
5.循环链表
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node *prev,*next;
Node(): prev(NULL),next(NULL){}
Node(int _val): val(_val),prev(NULL),next(NULL){}
};
void print(Node *head)
{
for(auto p=head->next;p!=head;p=p->next)
cout<<p->val<<' ';
cout<<endl;
}
int main()
{
Node *head=new Node(),*tail=head;//哨兵
head->next=tail,tail->prev=head;
print(head);
auto a=new Node(1);
a->next=head->next;
a->prev=head;
head->next->prev=a;
head->next=a;
auto b=new Node(2);
b->next=a->next;
b->prev=a;
a->next->prev=b;
a->next=b;
print(head);
//shanchu
b->prev->next=b->next;
b->next->prev=b->prev;
print(head);
return 0;
}