這篇文章主要為大家展示了“C++如何創(chuàng)建鏈表”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C++如何創(chuàng)建鏈表”這篇文章吧。
創(chuàng)新互聯(lián)建站從2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元鹿城做網(wǎng)站,已為上家服務(wù),為鹿城各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18980820575
前言
1.鏈表在C/C++里使用非常頻繁, 因?yàn)樗浅J褂? 可作為天然的可變數(shù)組. push到末尾時(shí)對(duì)前面的鏈表項(xiàng)不影響. 反觀C數(shù)組和std::vector, 一個(gè)是靜態(tài)大小, 一個(gè)是增加多了會(huì)對(duì)之前的元素進(jìn)行復(fù)制改寫(xiě)(線(xiàn)程非常不安全).
2.通常創(chuàng)建鏈表都是有next這樣的成員變量指向下一個(gè)項(xiàng), 通過(guò)定義一個(gè)head,last來(lái)進(jìn)行鏈表創(chuàng)建. 參考函數(shù) TestLinkCreateStupid().
說(shuō)明
1.其實(shí)很早就知道另一種創(chuàng)建方式, 但是一直沒(méi)總結(jié). 沒(méi)見(jiàn)過(guò)的童鞋看看以下創(chuàng)建鏈表的方式你用了哪一種. linus說(shuō)了不會(huì)第一種的TestLinkCreateClever()根本不會(huì)用指針(看來(lái)我真不會(huì)用指針). 這種方式在循環(huán)里根本不用判斷, 可見(jiàn)效率有多高.
// test_shared.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #include "stdafx.h" #include <memory> #include <string> #include <iostream> typedef struct stage_tag { int data_ready; /* Data present */ long data; /* Data to process */ struct stage_tag *next; /* Next stage */ } stage_t; // 高效率的鏈表創(chuàng)建方式 stage_t* TestLinkCreateClever(int stages) { stage_t *head = NULL,*new_stage = NULL,*tail = NULL; stage_t **link = &head; // 區(qū)別在這個(gè)指針地址變量上,它起到綁定新的stage的作用. for(int i =0; i<stages;++i) { new_stage = (stage_t*)malloc(sizeof(stage_t)); new_stage->data_ready = 0; new_stage->data = i; *link = new_stage; // 把新的stage賦值給link指向的指針地址 link = &new_stage->next; // 綁定下一個(gè)的指針地址 } tail = new_stage; *link = NULL; return head; } // 低效率的鏈表創(chuàng)建方式 stage_t* TestLinkCreateStupid(int stages) { stage_t *head = NULL,*new_stage = NULL,*tail = NULL; for(int i =0; i<stages;++i) { new_stage = (stage_t*)malloc(sizeof(stage_t)); new_stage->data_ready = 0; new_stage->data = i; new_stage->next = NULL; if(tail) tail->next = new_stage; else head = new_stage; tail = new_stage; } return head; } int _tmain(int argc, _TCHAR* argv[]) { std::cout << "=== TestLinkCreateClever ===" << std::endl; auto first = TestLinkCreateClever(10); while(first) { std::cout << "data: " << first->data << std::endl; first = first->next; } std::cout << "=== TestLinkCreateStupid ===" << std::endl; auto second = TestLinkCreateStupid(10); while(second) { std::cout << "data: " << second->data << std::endl; second = second->next; } return 0; }
以上是“C++如何創(chuàng)建鏈表”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站標(biāo)題:C++如何創(chuàng)建鏈表
文章起源:http://chinadenli.net/article8/ggidip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、用戶(hù)體驗(yàn)、微信小程序、品牌網(wǎng)站設(shè)計(jì)、小程序開(kāi)發(fā)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)