java数据结构单向链表的操作有哪些
本文小编为大家详细介绍“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;}
一次性添加n个x数据节点:
利用循坏,直接调用头添加或者尾添加:
#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数据结构单向链表的操作有哪些”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341