在双向链表中删除指定元素
2013-04-01 14:03
363 查看
#include <iostream> using namespace std; class List { public: List(int num) { this->num = num; } int num; List* next; List* pre; }; //创建链表 List* createList(int count) { if(count == 0 ) return NULL; List* list = new List(0); List* cur = list; cur->pre = NULL; List* pre = list; for(int i=1; i < count ; i++) { cur->next = new List(i); cur = cur->next; cur->pre = pre; pre = cur; } cur->next = NULL; return list; } //双向遍历 void travelList(List* list) { List* cur = list; List* end; while(cur != NULL) { cout<<cur->num<<endl; if(!cur->next) end = cur; cur = cur->next; } cur = end; while(cur != NULL) { cout<<cur->num<<endl; cur = cur->pre; } } List* remove(List* head,List* nodeToDelete){ List* cur = head; while(cur && cur != nodeToDelete) { cur = cur->next; } if(!cur) return head; List* pre = cur->pre; List* post = cur->next; if(pre) pre -> next = post; if(post) post -> pre = pre; List* ret = head; if(cur == head) ret = post; delete cur; return ret; } int main(){ List* l = createList(10); travelList(l); l=remove(l,l->next->next); cout<<endl; travelList(l); return 0; }
相关文章推荐
- C++单链表中删除指定范围内的元素
- 递归删除链表中的指定元素x
- sdut oj1464 链表-删除指定元素
- SDUT_OJ_1464-链表-删除指定元素
- C++单链表中删除指定范围内的元素
- 删除两个双向链表中相同元素的所有节点
- 删除链表中指定位置的元素
- C++单链表中删除指定范围内的元素
- 单链表的基本操作(尾插,尾删,头插,头删,查找,指定位置插入、删除,指定元素删除、全部删除等)
- C++单链表中删除指定范围内的元素
- 链表-删除指定元素 SDUT 1464
- 删除链表中指定的元素
- 链表——删除指定元素
- C++单链表中删除指定范围内的元素
- Remove Linked List Elements 删除链表中指定元素
- C++单链表中删除指定范围内的元素
- C 语言 实现双向链表 模拟ArrayList功能 可追加,插入,移除,得到指定index 的元素值
- 用C语言实现在一个链表删除指定的一个或多个元素
- 链表-删除指定元素
- C++单链表中删除指定范围内的元素