JAVA实现单链表
来源:互联网 发布:js undefend 判断 编辑:程序博客网 时间:2024/06/07 10:27
节点
package com.ghg.data_structure.sit;/** * 节点 * @author Administrator * */public class Node { /** * 数据 */ public int data; /** * 下一个元素 */ public Node next; public Node(int data, Node next) { super(); this.data = data; this.next = next; }}
链表
package com.ghg.data_structure.sit;import com.ghg.data_structure.singlinktab.Node;import sun.reflect.generics.tree.VoidDescriptor;public class LinkList { public Node first; public LinkList() { this. first = null; } /** * 添加元素到第个位置 * @param node */ public void addFirst(Node node){ node.next=first; first=node; } /** * 删除第一个元素 */ public void deleteFirst(){ first=first.next; } public void addNode(Node node){ //一个指针 Node current =first; /** * 找到最后个元素 */ while(current.next!=null){ current=current.next; } //添加到最后 current.next=node; } /** * 插入指定位置 * @param index * @param node */ public void addNodeByIndex(int index,Node node){ if(index<1 || index >length()+1){ System.out.println(index+ " 位置不正确 length "+length()); return; } /* * 记录我们遍历到第几个结点了,也就是记录位置 要把index添加到length后面 */ int length=1; /** * 指针 */ Node current=first; while(current.next!=null){ /** * 判断是否到达指定位置。 */ if(index==length++){ //插入操作。 把原来的下一个赋值给新的节点 node.next=current.next; current.next=node; return; } current=current.next; } } /** * 删除节点 */ public void delNodeByIndex(int index){ if(index<1 || index >length()+1){ System.out.println(index+ " 位置不正确 length "+length()); return; } /* * 记录我们遍历到第几个结点了,也就是记录位置 要把index添加到length后面 */ int length=1; /** * 指针 */ Node current=first; while(current.next!=null){ if(index==length++){ /** * 当前=下一个一下一个 */ current.next=current.next.next; return; } current=current.next; } } /** * 获取链表的长度 */ public int length(){ int length=1; Node current =first; /** * 找到最后个元素 */ while(current.next!=null){ length++; current=current.next; } return length; } /** * 显示 */ public void display(){ Node current = first; while (current != null) { current.display(); current = current. next; } System. out.println(); }}
阅读全文
0 0
- 【算法数据结构Java实现】Java实现单链表
- java实现单链表
- Java实现单链表
- java--实现单链表
- Java单链表的实现
- java实现单链表
- java实现单链表
- 单链表java语言实现
- 单链表之Java实现
- java单链表实现
- 单链表操作java实现
- 单链表---java实现
- Java实现单链表
- Java实现单链表
- java语言实现单链表
- 单链表 java实现
- 用java实现单链表
- Java 实现单链表反序
- luogu #3391 【模板】文艺平衡树(splay)
- 3n+1 数字处理
- StatsModels 安装过程中报出error: INCLUDE environment variable is empty
- 添加环境变量的三种方法
- 6步学会朴素贝叶斯算法(包含python语言和R语言源码)
- JAVA实现单链表
- gluseprogram
- 过滤器模式
- 实现数据库功能(GreenDao+butterknife)
- 洛谷P2286 [HNOI2004]宠物收养场(BZOJ1208)
- static extern const 静态全局变量与全局变量的区别
- OpenJ_Bailian 4147 汉诺塔问题(Hanoi)
- 实现简单的花括号配对问题
- 针对mybatis的理解