轉自: http://blog.csdn.net/wonggonghong/article/details/21527577
? 我們首先建立一個<List.h>頭文件,聲明一個單鏈表結構:
#include "List.h"
[cpp]?view plain?copy
- //創建一個單鏈表結構,包含一些常見的操作??
- #ifndef?_List_H_??
- #define?_List_H_??
- ??
- #include?<iostream>??
- ??
- struct?Node{??
- ????int?element;??//節點存儲信息可以根據需要修改!??
- ????Node*?next;??
- };??
- ??
- Node*?CreateLists();?//創建一個空表,并返回表頭??
- void?DeleteLists(Node*?head);?//刪除表頭為head的該鏈表??
- bool?IsLast(Node*?P);??
- Node*?Find(int?X,?Node*?head);??
- Node*?FindPrevious(int?X,?Node*?head);??
- void?Delete(int?X,?Node*?head);??
- void?Insert(Node*?P,?int?X);?//在節點P后面插入X??
- void?OutputLists(Node*?head);?//輸出鏈表中所有元素的值??
- ??
- #endif??????
??? 然后在<List.cpp>文件里實現頭文件中的鏈表操作:
#include "List.cpp"
[cpp]?view plain?copy
- #include?"list.h"??
- ??
- Node*?CreateLists()??
- {??
- ????Node*?head?=?new?Node;??
- ????head->next?=?NULL;??
- ????return?head;??
- }??
- ??
- void?DeleteLists(Node*?head)??
- {??
- ????Node*?P?=?head->next,?*temp;??
- ????head->next?=?NULL;??
- ????while(P)??
- ????{??
- ????????temp?=?P->next;??
- ????????delete?P;??
- ????????P?=?temp;??
- ????}??
- }??
- ??
- bool?IsLast(Node*?P)??
- {??
- ????return?P->next?==?NULL;??
- }??
- ??
- Node*?Find(int?X,?Node*?head)??
- {??
- ????Node*?P?=?head->next;??
- ????while(P?&&?P->element!=X)??
- ????????P?=?P->next;??
- ????return?P;??
- }??
- ??
- Node*?FindPrevious(int?X,?Node*?head)??
- {??
- ????Node*?P=head;??
- ????while(P->next?&&?P->next->element!=X)??
- ????????P=P->next;??
- ????return?P;??
- }??
- ??
- void?Delete(int?X,?Node*?head)??
- {??
- ????Node*?P?=?FindPrevious(X,head),?*temp;?//如果沒找到X,則返回的是鏈表最后一項??
- ????if(P->next)??
- ????{??
- ????????temp?=?P->next;??
- ????????P->next?=?temp->next;??
- ????????delete?temp;??
- ????}??
- }??
- ??
- void?Insert(Node*?P,?int?X)??
- {??
- ????Node*?tempX?=?new?Node;??
- ????tempX->element?=?X;??
- ????tempX->next?=?P->next;??
- ????P->next?=?tempX;??
- }??
- ??
- void?OutputLists(Node*?head)??
- {??
- ????Node*?P?=?head->next;??
- ????while(P)??
- ????{??
- ????????std::cout<<P->element<<"???";??
- ????????P?=?P->next;??
- ????}??
- ????std::cout<<std::endl;??
- }??
[cpp]?view plain?copy
- #include?<iostream>??
- #include?<assert.h>??
- #include?"list.h"??
- ??
- using?namespace?std;??
- ??
- int?main()??
- {??
- ????int?Date[8]?=?{2,9,5,8,15,32,7,4};??
- ????Node?*head?=?CreateLists();??
- ????Node?*P?=?head;??
- ????for(int?i=0;i<8;i++)??
- ????{??
- ????????Insert(P,Date[i]);??
- ????????P?=?P->next;??
- ????}??
- ????cout<<"打印出鏈表中所有元素:\n";??
- ????OutputLists(head);??
- ????if(IsLast(P))??
- ????????cout<<"該Lists的最后一個節點為:"<<P->element<<endl;??
- ????else??
- ????????cout<<"P不是最后一個節點!!P等于:"<<P->element<<endl;??
- ??????
- ????if(Find(15,head))??
- ????????cout<<"Find函數在該Lists中找到了值為15的節點!!!\n";??
- ????else??
- ????????cout<<"Find函數沒找到值為15的節點!!\n";??
- ??
- ????cout<<"FindPrevious函數找到節點15的前一個節點為:"<<FindPrevious(15,head)->element<<endl;??
- ????cout<<"而節點15的前一個節點應該為“8”!\n";??
- ??
- ????Delete(8,?head);??
- ????if(Find(8,head))??
- ????????cout<<"Delete(8,?head)后,在該Lists中找到了值為8的節點!!!\n";??
- ????else??
- ????????cout<<"Delete(8,?head)后,Find函數沒找到值為8的節點!!\n";??
- ??
- ????DeleteLists(head);???
- ????if(head->next)??
- ????????cout<<"DeleteLists函數未成功刪除鏈表!!\n";??
- ????else??
- ????????cout<<"鏈表已經為空!DeleteLists函數沒有問題!!!\n";??
- ????return?0;??
- }??