哇、、、、C++ 实现单向链表
之前相關的文章
C語言,鏈表
Linux內核鏈表
#什么是鏈表
鏈表是一種基本的數據結構,之前我在C語言里面寫過鏈表的知識,現在延申到C++,不管是什么語言,鏈表表示的是一種數據結構,跟語言沒有強相關性的。
如果我們需要實現一個鏈表,首先最關鍵的就是節點,一個節點表示鏈表的一個數據存儲點,鏈表是由很多個節點組成的。
鏈表還需要很多線把很多節點串聯在一起,可以用數組的特性串聯,也可以用指針串聯。
/*節點類*/? class?Node { public:DataType?data;Node?*next; };#鏈表頭部
鏈表是一種數據結構,跟數組一樣,和整型的數據一樣,都需要一個起始位置,鏈表頭就是這個起始位置,鏈表頭存在,就表示鏈表存在,鏈表頭沒有了,那這個鏈表也就沒有了。
鏈表頭也是一個節點,只是這個節點保存的是這個鏈表的起始位置
頭節點有點意思,它其實是不需要存儲數據的,所以data的值可有可無。
代碼上我們可以這樣寫,創建一個鏈表也就是創建一個鏈表頭
LinkList::LinkList() {head?=?new?Node;head->data?=?0;head->next?=?NULL;size?=?0; }#插入一個數據到鏈表中
如果是一個已經成型的鏈表,我們要把一個數據插入到鏈表中,我們需要有幾個判斷,是從鏈表頭插入,還是鏈表尾部插入,還是從鏈表的中間插入
— — 如果從鏈表頭插入,如下圖
— — 如果從鏈表中間插入,如下圖
— — 如果從鏈表尾部插入,如下圖
— —代碼實現
int?LinkList::InsertLinklList(Node?*data,?int?n) {Node?*ptemp;if?(this->head?==?NULL)?{cout?<<?"鏈表為空"?<<?endl;return?-1;}if?(data?==?NULL)?{cout?<<?"插入節點為空"?<<?endl;return?-1;}/*頭部插入*/?if?(n<2)?{Node?*pnew?=?new?Node;pnew->data?=?data->data;pnew->next?=?this->head->next;this->head->next?=?pnew;this->size++;return?0;}/*尾部插入*/?if?(n?>?this->size)?{ptemp?=?this->head;while?(ptemp->next?!=?NULL)?{ptemp?=?ptemp->next;}Node?*pnew?=?new?Node;pnew->data?=?data->data;pnew->next?=?NULL;ptemp->next?=?pnew;this->size++;return?0;}else?{/*中間插入*/?ptemp?=?this->head;for?(int?i?=?1;?i?<?n;?i++)?{ptemp?=?ptemp->next;}Node?*pnew?=?new?Node;pnew->data=?data->data;pnew->next?=?ptemp->next;ptemp->next?=?pnew;this->size++;return?0;} }#完整的代碼
#include?<string> #include?<iostream> using?namespace?std;/*節點類*/ class?Node { public:int?data;Node?*next; };class?LinkList { public:LinkList();/*構造函數*/~LinkList();/*析構函數*/int?CreateLinkList(int?size);/*新建一個鏈表*/int?DestroyLinkList();/*銷毀一個鏈表*/int?TravalLinkList();/*遍歷一個鏈表*/int?InsertLinklList(Node?*data,?int?n);/*向鏈表插入一個數據*/int?DeleteLinklist(int?n);/*刪除鏈表中的某個值*/int?GetLength();/*獲取鏈表的長度*/bool?IsEmpty();/*判斷鏈表是否為空*/Node?*head;/*鏈表頭*/int?size;/*鏈表長度*/ };LinkList::LinkList() {head?=?new?Node;head->data?=?0;head->next?=?NULL;size?=?0; }LinkList::~LinkList() {delete?head; }int?LinkList::CreateLinkList(int?n) {if?(n<0)?{cout<<"error"<<endl;return?-1;}Node?*ptemp?=?NULL;Node?*pnew?=?NULL;this->size?=?n;ptemp?=?this->head;for(int?i?=0?;?i<n?;?i++){pnew?=?new?Node;pnew->next?=?NULL;cout?<<?"輸入第"?<<?i+1?<<?"個節點值"?<<?endl;cin?>>?pnew->data;ptemp->next?=?pnew;ptemp?=?pnew;}cout?<<?"創建完成"?<<?endl;return?0; }int?LinkList::DestroyLinkList() {Node?*ptemp;if?(this->head?==?NULL)?{cout?<<?"鏈表原本就為空"?<<?endl;return?-1;}while?(this->head){ptemp?=?head->next;free(head);head?=?ptemp;}cout?<<?"銷毀鏈表完成"?<<?endl;return?0; }int?LinkList::TravalLinkList() {Node?*ptemp?=?this->head->next;if?(this->head?==?NULL)?{cout?<<?"鏈表為空"?<<?endl;return?-1;}while(ptemp){cout?<<?ptemp->data?<<?"->";ptemp?=?ptemp->next;}cout?<<"NULL"<<?endl;return?0; }int?LinkList::InsertLinklList(Node?*data,?int?n) {Node?*ptemp;if?(this->head?==?NULL)?{cout?<<?"鏈表為空"?<<?endl;return?-1;}if?(data?==?NULL)?{cout?<<?"插入節點為空"?<<?endl;return?-1;}/*頭部插入*/if?(n<2)?{Node?*pnew?=?new?Node;pnew->data?=?data->data;pnew->next?=?this->head->next;this->head->next?=?pnew;this->size++;return?0;}/*尾部插入*/if?(n?>?this->size)?{ptemp?=?this->head;while?(ptemp->next?!=?NULL)?{ptemp?=?ptemp->next;}Node?*pnew?=?new?Node;pnew->data?=?data->data;pnew->next?=?NULL;ptemp->next?=?pnew;this->size++;return?0;}/*中間插入*/else?{ptemp?=?this->head;for?(int?i?=?1;?i?<?n;?i++)?{ptemp?=?ptemp->next;}Node?*pnew?=?new?Node;pnew->data=?data->data;pnew->next?=?ptemp->next;ptemp->next?=?pnew;this->size++;return?0;} }int?LinkList::DeleteLinklist(int?n) {Node?*ptemp;Node?*ptemp2;if?(n?>?this->size)?{cout?<<?"n太大"?<<?endl;return?-1;}/*刪頭節點*/if?(n?<?2)?{ptemp?=?this->head->next;this->head->next?=?ptemp->next;free(ptemp);this->size--;return?0;}/*尾部刪除*/if?(n?==?this->size)?{ptemp?=?this->head;for?(int?i?=?1;?i?<?this->size;i++)?{ptemp?=?ptemp->next;}ptemp2?=?ptemp->next;ptemp->next?=?NULL;free(ptemp2);this->size--;return?0;}/*中間刪除*/else{ptemp?=?this->head;for?(int?i?=?1;?i?<?n;?i++)?{ptemp?=?ptemp->next;}ptemp2?=?ptemp->next;ptemp->next?=?ptemp2->next;free(ptemp2);this->size--;return?0;} }int?LinkList::GetLength() {return?this->size; }bool?LinkList::IsEmpty() {if?(this->head?==?NULL)?{return?true;}else{return?false;} }int?main(void) {LinkList?list;LinkList?*plist?=?&list;/*創建6個節點的鏈表*/plist->CreateLinkList(5);plist->TravalLinkList();/*向鏈表中插入一個數據*/Node?temp;temp.data?=?77;temp.next?=?NULL;/*向0號位置插入一個節點*/plist->InsertLinklList(&temp,?0);/*遍歷整個鏈表*/plist->TravalLinkList();/*向尾部插入一個鏈表*/plist->InsertLinklList(&temp,?plist->GetLength()+1);/*遍歷整個鏈表*/plist->TravalLinkList();/*向5號位置插入一個鏈表*/plist->InsertLinklList(&temp,?5);/*遍歷整個鏈表*/plist->TravalLinkList();plist->DeleteLinklist(0);/*遍歷整個鏈表*/plist->TravalLinkList();/*刪除第二個節點*/plist->DeleteLinklist(4);/*遍歷整個鏈表*/plist->TravalLinkList();/*刪除整個鏈表*/plist->DestroyLinkList();system("pause");return?(0); }#代碼輸出
輸入第1個節點值 2 輸入第2個節點值 22 輸入第3個節點值 3 輸入第4個節點值 44 輸入第5個節點值 5 創建完成 2->22->3->44->5->NULL 77->2->22->3->44->5->NULL 77->2->22->3->44->5->77->NULL 77->2->22->3->77->44->5->77->NULL 2->22->3->77->44->5->77->NULL 2->22->3->44->5->77->NULL 銷毀鏈表完成 請按任意鍵繼續.?.?.推薦閱讀:
專輯|Linux文章匯總
專輯|程序人生
專輯|C語言
我的知識小密圈
嵌入式Linux
微信掃描二維碼,關注我的公眾號
下面是我的僚機號,歡迎大家關注
?
寫代碼的籃球球癡
微信掃描二維碼,關注我的公眾號
總結
以上是生活随笔為你收集整理的哇、、、、C++ 实现单向链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫下载付费音乐包有什么用_
- 下一篇: xsd文件生成class