#include<iostream> using namespace std; class DblList; class DblListNode { friend class DblList; public: int data; DblListNode *llink,*rlink; }; class DblList { public: DblList() { first=new DblListNode(); first->llink=first->rlink=first; } void Insert(DblListNode*,DblListNode*); void Delete(DblListNode*); //private: public: DblListNode *first; }; void DblList::Insert(DblListNode *p,DblListNode *x)//p表示新插入的节点插入x节点的右边 { p->llink=x; p->rlink=x->rlink; x->rlink->llink=p; x->rlink=p; } void DblList::Delete(DblListNode *x) { if(x==first) cout<<"cant"<<endl; else{ x->llink->rlink=x->rlink; x->rlink->llink=x->llink; delete x; } } int main() { cout<<"ok"<<endl; DblList intList; DblListNode *node1,*node2,*node3,*node4,*node5; node1=new DblListNode();//创建的时候用new,那么就可以用deldete删除,否则报错 node2=new DblListNode(); node3=new DblListNode(); node4=new DblListNode(); node5=new DblListNode(); node1->data=10; node2->data=20; node3->data=30; node4->data=40; node5->data=50; intList.Insert(node1,intList.first);//指针所以加& intList.Insert(node2,intList.first); intList.Insert(node3,intList.first); intList.Insert(node4,intList.first); intList.Insert(node5,intList.first); cout<<intList.first->rlink->data<<endl; cout<<intList.first->rlink->rlink->data<<endl; cout<<intList.first->rlink->rlink->rlink->data<<endl; intList.Delete(node4); cout<<intList.first->rlink->data<<endl; cout<<intList.first->rlink->rlink->data<<endl; cout<<intList.first->rlink->rlink->rlink->data<<endl; return 0; }