单向链表
package hash_map.link; public class TestOne { /** * 链表是一种数据结构 LinkedList的实现原理就是链表 * * 链表循环遍历查找是比较慢,插入和删除时很快 * * 1、单向链表 * 最左边的节点时头结点,每个节点都两部分组成 当前对象的数据域和下一个节点的引用 */ } package hash_map.link; /** * * @author lxh 单向链表 * */ public class OneWay { Node head = null;//头结点 class Node{ Node next = null; //节点的引用,指向下一个节点 int data;//节点的对象,即内容 public Node(int data){ this.data = data; } } //向节点插入数据 public void addNode(int data){ Node newNode = new Node(data);//实例化节点 if (head == null) { head = newNode; return; } Node tem = head; //尾插法,while循环,直到next为null while (tem.next != null) { tem = tem.next; } tem.next = newNode; } //删除节点 public boolean deleteNode(int index){ if (index<1) { return false; } if (index == 1) { head = head.next; return true; } int i = 2; Node preNode = head; Node curNode = head.next; while(curNode != null){ if (index == i) { preNode.next = curNode.next; } preNode = curNode; curNode = curNode.next; i++; } return false; } //获取节点长度 public int length(){ int length = 0; Node node = head; while(node != null){ length++; node = node.next; } return length; } }
https://blog.csdn.net/jianyuerensheng/article/details/51200274