您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“java數據結構單向鏈表的操作有哪些”,內容詳細,步驟清晰,細節處理妥當,希望這篇“java數據結構單向鏈表的操作有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
最核心的是定義一個頭指針和一個尾指針(尾指針可以不定義但是會增加代碼的重復性,增加程序運行時間);
關于尾添加:(注意區分有節點和無節點的情況)
#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; }
關于代碼思路與尾添加基本一致,注意區分節點的鏈接:
#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; }
利用循壞,直接調用頭添加或者尾添加:
#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); } }
核心就是通過頭指針一個一個往下走找到指定節點的數據與所找數據是否匹配,最重要的是要使用中間變量記錄頭指針,否則就無法找到頭指針了(因為是單項鏈表):
#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; }
思路基本不變;區別傳入指定下標;內部定義一個計數器,當下標和計數器數值相等;表示鏈表存在這個節點;可以選擇傳出或者提醒;大家思考一下,動手實踐一下。
#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; } } } }
這里思路改變一下:根據數據或者下標找到前一個節點,改變前一個節點的pnext指針的指向,直接指向下一個節點,也就是這個節點的pnext;簡單示范一下刪除中間指定數據:
#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數據結構單向鏈表的操作有哪些”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。