您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“java數(shù)據(jù)結(jié)構(gòu)單向鏈表的操作有哪些”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“java數(shù)據(jù)結(jié)構(gòu)單向鏈表的操作有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
最核心的是定義一個(gè)頭指針和一個(gè)尾指針(尾指針可以不定義但是會(huì)增加代碼的重復(fù)性,增加程序運(yùn)行時(shí)間);
關(guān)于尾添加:(注意區(qū)分有節(jié)點(diǎn)和無節(jié)點(diǎn)的情況)
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; endadd(&phead,&pend,4); ...... return 0; } void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata) { struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct)); if(NULL == pt) return; pt->data = adddata; pt->pnext = NULL; if(NULL == *phead) { *phead = pt; } else { (*pend)->pnext = pt; } *pend= pt; }
關(guān)于代碼思路與尾添加基本一致,注意區(qū)分節(jié)點(diǎn)的鏈接:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; head_add(&phead,&pend,4); ...... return 0; } void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata) { struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct)); if(NULL == pt) return; pt->data = adddata; pt->pnext = NULL; if(NULL == *phead) { *pend = pt; } else { pt->pnext = (*phead); } *phead= pt; }
利用循壞,直接調(diào)用頭添加或者尾添加:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int adddata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; circulate_add(&phead,&pend,4,5); ...... return 0; } void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int count, int adddata); { for(int i = 0;i<count;i++) { endadd(phead, pend, adddata); } }
核心就是通過頭指針一個(gè)一個(gè)往下走找到指定節(jié)點(diǎn)的數(shù)據(jù)與所找數(shù)據(jù)是否匹配,最重要的是要使用中間變量記錄頭指針,否則就無法找到頭指針了(因?yàn)槭菃雾?xiàng)鏈表):
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void data_find(struct Mystruct *phead, int designated_data); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; middle_data_find(phead,4); ...... return 0; } void data_find(struct Mystruct* phead, int designated_data) { if (NULL == phead) return; struct Mystruct* ptemp = phead; while (ptemp != NULL) { if (ptemp->data == designated_data) { printf("找到了"); break; } ptemp = ptemp->pnext; } return; }
思路基本不變;區(qū)別傳入指定下標(biāo);內(nèi)部定義一個(gè)計(jì)數(shù)器,當(dāng)下標(biāo)和計(jì)數(shù)器數(shù)值相等;表示鏈表存在這個(gè)節(jié)點(diǎn);可以選擇傳出或者提醒;大家思考一下,動(dòng)手實(shí)踐一下。
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; struct Mystruct *index_find(struct Mystruct *phead, int index); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; middle_data_find(phead,4); ...... return 0; } struct Mystruct* index_find(struct Mystruct* phead, int index) { if (NULL == phead||index<0) return NULL; struct Mystruct* ptemp = phead; int i = 0; for (i = 0; i < index; i++) { ptemp = ptemp->pnext; } return ptemp; }
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_head(struct Mystruct **phead,struct Mystruct **pend); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_head(&phead) ...... return 0; } void deleat_head(struct Mystruct** phead, struct Mystruct** pend) { if (NULL == *phead) return; struct Mystruct* pt = *phead; if ((*phead)->pnext == NULL) { free(pt); *phead = NULL; *pend = NULL; } else { *phead = (*phead)->pnext; free(pt); } } void deleat_end(struct My
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_end(struct Mystruct**phead,struct Mystruct**pend); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_head(&phead) ...... return 0; } void deleat_end(struct Mystruct** phead, struct Mystruct** pend) { if (NULL == *phead) return; struct Mystruct* pt = *phead; if (pt->pnext == NULL) { free(pt); *phead = NULL; *pend = NULL; } else { while (pt->pnext != (*pend)) { if (pt->pnext == (*pend)) { free(*pend); *pend = pt; pt->pnext = NULL; pt = pt->pnext; } } } }
這里思路改變一下:根據(jù)數(shù)據(jù)或者下標(biāo)找到前一個(gè)節(jié)點(diǎn),改變前一個(gè)節(jié)點(diǎn)的pnext指針的指向,直接指向下一個(gè)節(jié)點(diǎn),也就是這個(gè)節(jié)點(diǎn)的pnext;簡(jiǎn)單示范一下刪除中間指定數(shù)據(jù):
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_head(&phead) ...... return 0; } void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata) { if (NULL == *phead) return; struct Mystruct* pt = *phead; if (pt->pnext == NULL) { free(pt); *phead = NULL; *pend = NULL; } }
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_all(struct Mystruct** phead, struct Mystruct** pend) int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_all(&phead,&pend) ...... return 0; } void deleat_all(struct Mystruct** phead, struct Mystruct** pend) { while (*phead!= NULL) { struct Mystruct* pt = *phead; *phead = (*phead)->pnext; free(pt); } *phead = NULL; *pend = NULL; }
讀到這里,這篇“java數(shù)據(jù)結(jié)構(gòu)單向鏈表的操作有哪些”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。