日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++实现 简单 单链表

發布時間:2023/11/30 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);?//刪除表頭為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;??
  • }??
  • ???????? 最后,我們用一段 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++实现 简单 单链表的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。