劍指offer之C++語(yǔ)言實(shí)現(xiàn)鏈表(兩種刪除節(jié)點(diǎn)方式)
1 問(wèn)題
用C++語(yǔ)言實(shí)現(xiàn)鏈表
2 代碼實(shí)現(xiàn)
#include <iostream> #include <stdlib.h> using namespace std; class List { public: List(); ~List(); List* createNode(int value);//創(chuàng)建節(jié)點(diǎn) bool insertNode(List *node);//插入節(jié)點(diǎn) void printList();//打印節(jié)點(diǎn) bool deleteNode(List *node);//刪除節(jié)點(diǎn)不移動(dòng)頭節(jié)點(diǎn) bool deleteNode1(List *node);//刪除節(jié)點(diǎn)移動(dòng)頭節(jié)點(diǎn) int listSize();//長(zhǎng)度 void printNode();//打印但前的value void freeList();//釋放鏈表 private: int value; List *head; List *next; }; bool List::deleteNode(List *node) { if (node == NULL) { std:cout << "node is NULL" << std::endl; return false; } if (head == NULL) { std::cout << "head is NULL" << std::endl; return false; } //如果node等于head if (head == node) { head = head->next; } List *p = head; while (p->next != NULL) { if (p->next == node) { p->next = p->next->next; return true; } p = p->next; } return false; } bool List::deleteNode1(List *node) { if (node == NULL) { std:cout << "node is NULL" << std::endl; return false; } if (head == NULL) { std::cout << "head is NULL" << std::endl; return false; } //如果node等于head if (head == node) { head = head->next; } List *p = head; while (head->next != NULL) { if (head->next == node) { head->next = head->next->next; std::cout << "delete node success head->value" << head->value << std::endl; //這里要記得把頭節(jié)點(diǎn)的指針移動(dòng)最后還原,這里的頭節(jié)點(diǎn)是保存在這個(gè)類里面,改變了就是改變了 //如果這里是把head作為參數(shù)傳遞,最后head會(huì)被銷毀那么不需要移動(dòng)頭指針 head = p; return true; } //注意,這里由于head是成員變量,改變了就是改變了,所以需要最后重新指定 head = head->next; } std::cout << "delete node fail head->value" << head->value << std::endl; //這里要記得把頭節(jié)點(diǎn)的指針移動(dòng)最后還原,這里的頭節(jié)點(diǎn)是保存在這個(gè)類里面,改變了就是改變了 //如果這里是把head作為參數(shù)傳遞,最后head會(huì)被銷毀那么不需要移動(dòng)頭指針 head = p; return false; } List::List() { value = 0; head = NULL; next = NULL; } List::~List() { delete head; delete next; } List* List::createNode(int value) { List *list = NULL; list = new List(); if (list) { list->value = value; return list; } return NULL; } bool List::insertNode(List *node) { node->next = head; head = node; return true; } void List::printList() { if (head == NULL) { std::cout << "head is NULL" << std::endl; return; } List *p = head; while (p != NULL) { std::cout << p->value << std::endl; p = p->next; } return; } void List::printNode() { std::cout << value << std::endl; } int List::listSize() { if (head == NULL) { std::cout << "head is NULL" << std::endl; return 0; } int len = 0; List *p = head; while (p != NULL) { p = p->next; ++len; } return len; } void List::freeList() { if (head == NULL) { std::cout << "head is NULL" << std::endl; return; } List *p; while (head != NULL) { p = head; head = head->next; free(p); } } int main() { List list; List *list1 = list.createNode(5); list.insertNode(list1); List *list2 = list.createNode(6); list.insertNode(list2); List *list3 = list.createNode(1); list.insertNode(list3); List *list4 = list.createNode(3); list.insertNode(list4); List *list5 = list.createNode(2); list.insertNode(list5); list.printList(); std::cout << "list size is " << list.listSize() << std::endl; std::cout << "-----------開(kāi)始刪除節(jié)點(diǎn)值為3的節(jié)點(diǎn)" << std::endl; list.deleteNode1(list4); list.printList(); std::cout << "list size is " << list.listSize() << std::endl; list.freeList(); list.printList(); return 0; }
3 運(yùn)行結(jié)果
2
3
1
6
5
list size is 5
-----------開(kāi)始刪除節(jié)點(diǎn)值為3的節(jié)點(diǎn)
delete node success head->value2
2
1
6
5
list size is 4
head is NULL
4 小結(jié)
很明顯用C語(yǔ)言實(shí)現(xiàn),我們習(xí)慣在外面搞個(gè)頭結(jié)點(diǎn),然后用C++實(shí)現(xiàn),我們直接在類的里面放一個(gè)head指針,然后我們?cè)谠黾庸?jié)點(diǎn)的時(shí)候我們會(huì)把head進(jìn)行移動(dòng),放在最前面,所以后面的 便利和刪除操作等最好是不要?jiǎng)觝ead的位置了,因?yàn)閔ead動(dòng)了,下次便利就有問(wèn)題,如果刪除函數(shù)移動(dòng)了head,我們最后需要復(fù)原h(huán)ead
比如我們的頭指針盡量不要移動(dòng),我們可以用一個(gè)指針變量來(lái)保存這個(gè)head指針,然后我們移動(dòng)保存的指針變量,同時(shí)把保存的指針變量在一些情況下改變下一個(gè)指向的指針,那么我們下次便利head也是生效的,這樣保證了頭指針不被污染
比如下面的例子
#include <stdio.h> void change(char *a) { *(a + 1) = 's'; } int main() { char value[10] = "chenyu"; change(value); printf("value is %s\n", value); return 0; } #include <stdio.h> void change(char *a) { char *p = a; *(p + 1) = 's'; } int main() { char value[10] = "chenyu"; change(value); printf("value is %s\n", value); return 0; }
其實(shí)最后的結(jié)果都是一樣,csenyu
頭指針是指向這塊內(nèi)存的地址,如果我們用指針變量保存了,然后這個(gè)指針變量也指向了這里,用指針變量去操作后,然后頭指針也是指向這里,后面數(shù)據(jù)的指向也會(huì)改變
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C++ 項(xiàng)目引入lib和dll的區(qū)別與使用實(shí)戰(zhàn)
靜態(tài)鏈接庫(kù)與動(dòng)態(tài)鏈接庫(kù)都是共享代碼的方式,本文主要介紹了C++項(xiàng)目引入lib和dll的區(qū)別與使用實(shí)戰(zhàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02C語(yǔ)言超詳細(xì)講解隊(duì)列的實(shí)現(xiàn)及代碼
隊(duì)列(Queue)與棧一樣,是一種線性存儲(chǔ)結(jié)構(gòu),它具有如下特點(diǎn):隊(duì)列中的數(shù)據(jù)元素遵循“先進(jìn)先出”(First?In?First?Out)的原則,簡(jiǎn)稱FIFO結(jié)構(gòu)。在隊(duì)尾添加元素,在隊(duì)頭刪除元素2022-04-04C/C++表格組件Qt?TableWidget應(yīng)用詳解
本文詳細(xì)講解了C/C++中使用列表框組件Qt?TableWidget的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12詳細(xì)分析c++ const 指針與指向const的指針
這篇文章主要介紹了c++ const 指針與指向const的指針的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07C++ 中"emplace_back" 與 "push_back" 的區(qū)別
這篇文章主要介紹了C++ 中"emplace_back" 與 "push_back" 的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-04-04