链表
双向循环链表
typedef int item;
class list{
private:
struct node{
item data;
node* from;
node* to;
};
node* head=new node;
public:
list(){
head->to=head;
head->from=head;
}
~list(){
for(node* i=head->to;i!=head;i=i->to)
delete i;
delete head;
}
void prepend(item);
void append(item);
void del(item);
void edit(item,item);
void print();
node* search(item);
};
void list::prepend(item x){
node* temp=new node;
temp->data=x;
temp->to=head->to;
head->to->from=temp;
head->to=temp;
temp->from=head;
return;
}
void list::append(item x){
node* temp=new node;
temp->data=x;
temp->from=head->from;
temp->to=head;
head->from->to=temp;
head->from=temp;
}
void list::del(item val){
for(node* i=head->to;i!=head;i=i->to){
if(i->data==val){
i->from->to=i->to;
i->to->from=i->from;
}
}
return;
}
void list::edit(item val,item a){
for(node* i=head->to;i!=head;i=i->to){
if(i->data==val){
i->data=a;
}
}
return;
}
void list::print(){
for(node* i=head->to;i!=head;i=i->to){
std::cout<<i->data<<' ';
}
return;
}
list::node* list::search(item val){
for(node* i=head->to;i!=head;i=i->to){
if(i->data==val) return i;
}
return nullptr;
}