链接点以及链表实现
2016-11-02 14:12
267 查看
链接点
链接点中包含一个数据域和一个指针域,其中数据域用来包装数据,而指针域用来指向下一个链接点public class Link{ //数据域 private int data; //指针域 private Link next; public Link(int data){ this.data=data; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Link getNext() { return next; } public void setNext(Link next) { this.next = next; } }
实现链表
在插入节点到制定位置的部分,为什么只循环到pos-1假如在下标2的位置插入数据,我们只需要找到1的下标在其后面插入数据即可。
public class LinkList{ //开始节点 private Link first; //添加 public void insert(int value){ Link lnk = new Link(value); if(first == null){ first = lnk; }else{ lnk.setNext(first); first = lnk; } } //显示全部 public void display(){ Link current = first; while(current != null){ System.out.println(current.getData()); current = current.getNext(); } } //查找节点 public Link find(int key){ Link current = first; while(current.getData() != key){ if(current.getNext() == null){ return null; } current = current.getNext(); } } //插入节点到指定位置 public void insert(int value,int pos){ if(pos == 0){ insert(value); }else{ Link current = first; for(int i=0;i<pos-1;i++){ current = current.getNext(); } Link lnk = new Link(value); lnk.setNext(current.getNext()); current.setNext(lnk); } } //删除指定节点 public void delete(int key){ Link current = first; Link ago = first; while(current.getData() != key){ if(current.getNext() == null){ return; }else{ ago = current; current = current.getNext(); } } if(current == first){ first = first.getNext(); }else{ ago.setNext(current.getNext()); } } }
相关文章推荐
- 链接点以及链表实现
- 栈的链表实现,以及编译原理中的括号匹配
- 实现二叉树以及链表发现的问题
- 单链表、带头结点的单链表、循环单链表 以及其操作实现
- 浅谈双向链表的逆转以及用双向链表实现malloc/free/realloc
- 常见内排序实现汇总(含部分优化实现,基于链表的实现),以及性能比较
- java实现单项链表以及如何检测回环
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
- 二分查找(Binary Search)需要注意的问题,以及在数据库内核中的实现[谁有源码麻烦贴过来个链接学习学习]
- 单链表常见功能的实现、以及逆转等
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
- 转—gcc指定库路径,头文件路径以及实现静态动态链接
- 数据结构(c语言版)链表的实现以及合并
- 链表的c语言实现以及根据linux内核中链表的实现过程
- 软基作业——使用链表实现多项式的存储以及加法
- 实现二叉树以及链表发现的问题
- 单向循环链表的实现以及约瑟夫环的实现
- 1 用存储过程实现分页,除了上一页,下一页,第一页,和末页外还要有go按钮,以及go到那里的文本框。另外还要在Lable显示“当前x页,一共y页”。注意验证控件的使用和 链接存储过程的内容。
- 将企业协作与微信结合,明道打通微信实现从微信到明道的消息分享,目前已支持文字、图片以及链接
- 【转链接】Handlebars模板引擎以及浅谈模板引擎的实现原理