参考网上教程学习数据结构:记录学习过程,积累点滴知识。
链表是由节点组成,一个节点又分为数据域和指针域。数据域(data)存储该节点的数据,指针域(next)存储下一个节点。
java 代码的链表实现:
一、创建节点类:
/**
* Created by greg on 17-4-3.
* 链表是由数据域和指针域组成
* 创建链表中的节点
*/
public class Node {
protected Node next; //指针域,下一个节点
protected int data; //数据域,存储本节点的数
/**
* 带参数的构造函数
*/
public Node(int value){
this.data = value;
}
/**
* 显示方法
*/
public void disPlay(){
System.out.print(this.data+"\t");
}
}
二、创建链表类:
1、创建类以及链表的结构,以及构造函数
public class LinkList {
//头结点
protected Node first;
//链表的长度
protected int length = 0;
//构造函数
public LinkList(){
first = null;
}
/**
* 插入一个节点,从头节点开始插入
* 当头结点为空的时候,插入的节点直接为头结点
* 否则将插入的节点的指针域指向原来的头结点,并将插入的节点赋值给头结点。
*/
public void insertFirst(int value){
Node node = new Node(value);
if (first == null){
first = node;
}else{
node.next = first;
first = node;
}
length++;
}
/**
* 删除一个节点,删除头结点,并返回该节点
* 将头节点的下一个节点赋值给头结点
*/
public Node deleteFirst(){
Node tmp = first;
first = tmp.next;
length--;
return tmp;
}
/**
* 在任意位置插入数据,在index的后面插入数据
*
* 如果index的值超过链表的长度,就在最后添加节点
* 如果不超过链表的长度,就在index之后插入
*/
public void add(int index, int value){
Node tmp = new Node(value);
Node current = first; //current表示第index个节点
if (index>=length-1){
for (int i = 0; i < length-1; i++) {
current = current.next;
}
current.next = tmp;
}else{
for(int i=0;i<index;i++){
current = current.next;
}
tmp.next = current.next;
current.next = tmp;
}
length++;
}
/**
* 显示方法
* 当前节点不为空,就显示该节点的值
* 当前节点为空的时候就退出循环
*/
public void disPlay(){
Node current = first;
// while(current.next != null){
// current.disPlay();
// current = current.next;
// if (current.next == null){
// current.disPlay();
// }
// }
while (current != null){
current.disPlay();
current = current.next;
}
System.out.println();
}
/**
* 查找方法,根据数据来进行查找
* 当前数据的值与查找的值不相等,判断当前节点的下一个节点是否为空
* 如果为空,就返回空 如果不为空就将下一个节点赋值给当前节点
*/
public Node find(int value){
Node current = first;
while (current.data != value){
if (current.next == null){
return null;
}
current = current.next;
}
return current;
}
/**
* 根据数据来进行删除 删除数据就是将被删除节点的前一个节点的指针域指向被删除节点的下一个节点
* 所以要定义两个节点,一个是当前节点,一个时前一个节点。推理方法同上
*/
public Node delete(int value){
Node current = first;
Node previous = first;
while(current.data != value){
if(current.next == null){
return null;
}
previous = current;
current = current.next;
//这种写法容易漏掉数据
// current = current.next;
// previous.next = current;
}
if (current == first){
first = first.next;
}else{
previous.next = current.next;
}
length--;
return current;
}
/**
* 根据索引删除任意位置的节点
* 索引有三种情况:
* 1、大于链表的长度
* 2、等于链表最大的长度
* 3、在链表的长度范围之内
*/
public Node deleteByIndex(int index){
Node current = first;
Node previous = first;
if(index>length-1){
throw new IndexOutOfBoundsException();
}else if(index == length - 1){
for (int i = 0; i < index ; i++) {
previous = current;
current = current.next;
}
previous.next = null;
}
else{
for (int i = 0; i < index ; i++) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
length--;
return current;
}
}
三、测试类:
LinkList linkList = new LinkList();
linkList.insertFirst(12);
linkList.insertFirst(13);
linkList.insertFirst(14);
linkList.insertFirst(15);
linkList.insertFirst(16);
linkList.disPlay();
// Node node1 = linkList.find(13);
// node1.disPlay();
// System.out.println();
// linkList.disPlay();
Node node2 = linkList.delete(15);
node2.disPlay();
System.out.println();
linkList.disPlay();
System.out.println(linkList.length);
linkList.add(5 , 17);
linkList.disPlay();
System.out.println(linkList.length);
Node node3 = linkList.deleteByIndex(2);
node3.disPlay();
linkList.disPlay();
System.out.println(linkList.length);
四、结果