双链表c++

    科技2022-07-16  117

    #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(); }
    Processed: 0.008, SQL: 8