生活随笔
收集整理的這篇文章主要介紹了
C++实现 简单 单链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自: 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);??? 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);??? 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;??? ????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;?? }??
????????
最后,我們用一段
main
代碼驗證一下正確性:
[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;?? }??
結果如下:
總結
以上是生活随笔為你收集整理的C++实现 简单 单链表的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。