在无头节点的单链表里删除元素
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客内容:在无头节点的链表里删除元素;
博客时间:2014-4-15;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008 32位编译器;
制图工具:office 2010 ppt;
硬件信息:7G-3 笔记本;
mine words
problem静净精镜. In the world, no patience and no road.
solution无头节点的单链表里删除节点
对于此指针 p ,分为两种情况
要么是尾节点,要么不是
1:p is the end of list
2: p is not the end of listso, we need travel the list again, and find its left node p2. Then free the end form p2;
time cost: O(list.length)
process follows
t
so, copy its right node(we make it called p2) key and next point to p, and delete p2;
time cost: O(1)
experiment
picture follows
code
test.cpp
#include<iostream> using namespace std; template<typename T> class Node { public: T data; Node<T>* next; Node() { data = NULL; next = NULL; } }; // function: make list // input: an array // output: a list template<typename T> Node<T>* _Make_list(T * D,size_t size); // function: delete the key from list // input: a no_head list and the key // output: a bool template<typename T> bool _Delete_node_from_Nohead_list(Node<T> * & L,Node<T>* key); // function: output list // input: a list // output: void template<typename T> void _Output_list(Node<T>* L); // function: find the key // input: a list and the key // output: the key node point or NULL template<typename T> Node<T>* _Search_from_list(Node<T> *& L,T key); int main() { size_t size = 10; int * D = new int[size]; cout<<"data follows"<<endl; for(size_t i =0;i<size;i++ ) { D[i] = rand(); cout<<D[i]<<endl; } Node<int>* L = _Make_list(D,size); int key ; Node<int> *p; cout<<"the key you want delete"<<endl; while(cin>>key) { //_Output_list(L); p = _Search_from_list(L,key); cout<<"find key = "<<p->data<<endl; _Delete_node_from_Nohead_list(L,p); _Output_list(L); cout<<"the key you want delete"<<endl; } system("pause"); return 0; } // function: delete the key from list // input: a no_head list and the key // output: a bool template<typename T> bool _Delete_node_from_Nohead_list(Node<T> * & L,Node<T> * key) { if(L != NULL) { // first case: key is not the end of list if(key != NULL && key->next != NULL) { Node<T> * p = key->next; key->next = p->next; key->data = p->data; delete p; } // second case: key is the end of list else if(key != NULL && key->next == NULL) { Node<T>* p = L; while(p->next != NULL && p->next != key) { p = p->next; } if(p->next == NULL) return false; else if(p->next == key) { p->next = NULL; delete key; } } } else return false; } // function: make list // input: an array // output: a list template<typename T> Node<T>* _Make_list(T * D,size_t size) { if(D != NULL && size >0) { Node<T> * L,*p = new Node<T> ; p->data = D[0]; L = p; for(size_t i = 1;i < size; i++) { p->next = new Node<T> ; p = p->next; p -> data = D[i]; } return L; } else return NULL; } // function: output list // input: a list // output: void template<typename T> void _Output_list(Node<T>* L) { Node<T>* p = L; cout<<"list follows"<<endl; while( p != NULL ) { cout<< p->data <<endl; p = p->next; } cout<<"list over"<<endl; } // function: find the key // input: a list and the key // output: the key node point or NULL template<typename T> Node<T>* _Search_from_list(Node<T> *& L,T key) { if(L != NULL) { Node<T> * p = L; while(p != NULL && p->data != key) { p = p->next ; } if(p == NULL) { return NULL; } else { return p; } } else return NULL; }