java单链表的基本操作
来源:互联网 发布:常用推荐算法 编辑:程序博客网 时间:2024/06/05 19:08
package linklist;public class LinkList {private Node head = null;// 链表头节点private Node tail = null;// 链表尾节点private Node pointer = null;// 遍历链表的节点public LinkList(Node node) {head=new Node();head.LinkNext(node);tail = node;}// 在链表末尾添加节点public void AddNode(Node node) {tail.LinkNext(node);tail = tail.next();}// 返回链表长度public int getLength() {pointer=head.next();int current=0;while(pointer!=tail){ current++; pointer=pointer.next();}return ++current; //注意++一定要写前面,被还惨了QAQ}// 打印链表所有的元素public void printLink() {pointer=head.next();while(pointer!=tail){System.out.print(pointer.getValue()+" ");pointer=pointer.next();}System.out.println(tail.getValue());}// 在链表指定位置插入节点public void insertAt(int index, Node node) { if(index>this.getLength()) { System.out.print("wrong"); } else {pointer=head;for(int i=0;i<index-1;i++)pointer=pointer.next(); /*int current=0; while(current!=index) { pointer=pointer.next(); current++; }*/node.LinkNext(pointer.next());pointer.LinkNext(node);}}// 在链表指定位置删除节点public void removeAt(int index) {if(index>this.getLength()){System.out.print("wrong");}else{pointer=head; Node q=pointer; for(int i=0;i<index;i++) { q=pointer; pointer=pointer.next(); } q.LinkNext(pointer.next()); pointer.LinkNext(null);} /* int current=0; while(current!=index) { pointer=pointer.next(); current++; } pointer.LinkNext(pointer.next()); */}}class Node {private Object value;// 值private Node next;// 下一个节点 public Node(){}public Node(Object value) {this.value = value;} // 获取下一个节点public Node next() {return this.next;} // 链接下一个节点public void LinkNext(Node next) {this.next = next;} // 设置当前节点的值public void setValue(Object value) {this.value = value;} // 获取当前节点的值public Object getValue() {return this.value;}}
package linklist;public class testlink {public static void main(String[] args) {Node n1=new Node("a");Node n2=new Node("b"); Node n3=new Node("c"); Node n4=new Node("d"); Node n5=new Node("e"); LinkList list=new LinkList(n1); list.AddNode(n2); list.AddNode(n3); list.AddNode(n4); list.printLink();//此时输出:abcd System.out.println(list.getLength());//此时输出:4 list.insertAt(4, n5); //System.out.println(list.getLength()); list.printLink();//此时输出:abced list.removeAt(2); list.printLink();//此时输出:aced System.out.println(list.getLength());} }
补充内容:
单链表节点Node比如说有A,B,C,D四个Node对象,它们按顺序一个接一个next指的是下一个A.next=B;B.next=C;C.next=D;root指的是上一个B.root=A;C.root=B;D.root=C;来源:http://zhidao.baidu.com/link?url=SugMe6R5e-4KV15Pkmo7tzjqLHn8q2zJGUqpU5QSnvGzCnsz2_gb5TC0FXf74SWJMk6yA6tP84ib33H8vhFn0K
0 0
- java单链表的基本操作
- java的基本操作
- java实现单链表的基本操作
- java实现单链表的基本操作
- java自己实现单链表的基本操作
- JAVA单链表基本操作
- java基本入门-----String的基本操作
- Java File的基本操作
- java 基本的io操作
- Java 基本的IO操作
- Java文件夹的基本操作
- Java文件的基本操作
- Java变量的基本操作
- mongoDB----Java的基本操作
- Java反射的基本操作
- Java线程的基本操作
- Java---->String的基本操作
- java mongo的基本操作
- C语言笔记(《C与指针》)
- Android使用post方式上传图片到服务器的方法
- Android基础之内存溢出
- 迷宫问题
- windows7下的PHP+apache+mysql+eclipse for php 开发环境搭建
- java单链表的基本操作
- 高中OJ3046. 【NOIP2012模拟10.23】游戏
- 例题:除法(UVa 725)
- NDK开发的javah命令无法生成h文件
- Revit开发设置模型线颜色
- [剑指offer学习心得]之:两个链表的第一个公共结点
- 欢迎使用CSDN-markdown编辑器
- hive count(*)问题处理
- 记一次unhappy考试(10.5)