节点结构:
- struct both_node
- {
- int key;
- struct both_node *prev;
- struct both_node *next;
- both_node(int k)
- :key(k),prev(NULL),next(NULL)
- {}
- };
不带头节点的双链表实现插入 删除,
- 双链表
- void insert_both(both_node *&head,int key)
- {
- if(head==NULL)
- {
- head= new both_node(key);
- } else
- {
- both_node *tmp= new both_node(key);
- if(head->next==NULL)
- {
- head->next=tmp;
- tmp->prev=head;
- } else
- {
- tmp->next=head->next;
- head->next->prev=tmp;
-
- head->next=tmp;
- tmp->prev=head;
- }
- }
- }
- bool remove_both(both_node *&head,int key)
- {
- if(head==NULL)
- return false ;
- if(head->key==key)
- {
- if(head->next==NULL)
- {
- delete head;
- head=NULL;
- } else
- {
- both_node *tmp=head;
- head=head->next;
- head->prev=NULL;
- delete tmp;
- tmp=NULL;
- }
- return true ;
- }
- both_node *cur=head->next;
- while(cur !=NULL && cur->key !=key)
- cur=cur->next;
- if(cur==NULL)
- return false ;
- if(cur->next !=NULL)
- {
- cur->prev->next=cur->next;
- cur->next->prev=cur->prev;
- } else
- {
- cur->prev->next=NULL;
- }
-
- delete cur;
- cur=NULL;
- return true ;
- }
双链表插入 删除详解
原文:http://www.cnblogs.com/Ph-one/p/6367522.html