#include<iostream>
using namespace std;
struct node{
int data;
node* prev;
node* next;
};
class doubleList{
private:
node* head;
public:
doubleList();
~doubleList();
void CreatList(int n);
int Insert(const int e,int pos);
int DeletePts(const int e);
void print();
};
doubleList::doubleList(){
head=new node;
head->prev=NULL;
head->next=NULL;
}
void doubleList::CreatList(int n){
int i;
node* tmp;
node* p=head;
for(i=0;i<n;i++){
cin>>tmp->data;
p->next=tmp;
tmp->prev=p;
tmp->next=NULL;
p=tmp;
}
}
int doubleList::Insert(const doubleList &L,int e,int pos){
int i=1;
node* tmp;
node* p=head;
if(pos<0||pos>sizeof(L))
return -1;
else{
while(pos!=i){
p=p->next;
i++;
}
if(p->data==e){
tmp->data=e;
tmp->next=p->next;
tmp->prev=p;
p->next=tmp;
p->next->prev=tmp;
}
else return -1;
}
}
int doubleList::DeletePts(const int e){
node* p=head->next;
while(p->data!=e){
p=p->next;
}
if(p->data==e){
p->prev->next=p->next;
p->next->prev=p->prev;
delete p;
}
else return -1;
}
void doubleList::Print(){
node* p=head->next;
while(p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int main(){
doubleList l;
l.CreatList(5);
l.Print();
}
转载请注明原文地址:https://blackberry.8miu.com/read-9717.html