首先定义一个类,用来当做 节点类型,跟c的结构体一样:
public class Node {
int date;
Node next;
public Node(int value) {
this.date = value;
}
}
在定义一个类,用它开实现链表:
public class IList {
Node head = null;
/**
* 链表添加节点
* @param value
*/
public void addNode(int value) {
Node newNode = new Node(value);
if(head == null) {
head = newNode;
return;
}
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
/**
* 遍历链表
* @param node头节点,没有存放date
*/
public void traverse() {
if(head == null)
return;
//临时节点
Node temp = head;
while(temp != null) {
System.out.print(temp.date+ " ");
temp = temp.next;
}
System.out.println();
}
/**
* 删除一个节点
* @param index
* @return
*/
public boolean deleteNode(int index) {
if(index < 1 || index > length())
return false;
if(index == 1) { //删除第一个节点
head = head.next;
return true;
}
Node preNode = head;
Node curNode = preNode.next;
int i = 1;
while(curNode != null) {
if(i == index) {
preNode = curNode;
}
preNode = preNode.next;
curNode = curNode.next;
}
return true;
}
/**
* 求链表的长度
* @return
*/
public int length() {
int length = 0;
Node temp = head;
while(temp != null) {
length++;
temp = temp.next;
}
return length;
}
/**
* 在链表中寻找指定值
* @param value
* @return
*/
public boolean findNode(int value) {
if(head == null)
return false;
Node temp = head;
while(temp != null){
if(temp.date == value)
return true;
temp = temp.next;
}
return false;
}
/**
* 指定位置插入指定值
* @param value
* @param index
* @return
*/
public boolean insertNode(int value, int index) {
if(index < 1 || index > length() + 1) {
System.out.println("插入位置不合法!");
return false;
}
Node newNode = new Node(value);
if(index == 1) {
newNode.next = head;
head = newNode;
return true;
}
Node preNode = head;
Node nextNode = preNode.next;
int i = 2;
while (preNode != null) {
if( i == index) {
preNode.next = newNode;
newNode.next = nextNode;
return true;
}
i++;
preNode = preNode.next;
nextNode = nextNode.next;
}
return false;
}
/**
* 寻找链表中倒数第k个节点
* 设置两个指针p1、p2,让p2比p1快k个节点,
* 同时向后遍历,当p2为空,则p1为倒数第k个节点
* @param k
* @return 如果找到,返回节点,否则返回null
*/
public Node findPreNode(int k) {
if(k < 1 || k >length()) {
System.out.println("输入不合法!");
return null;
}
int i = 1;
Node preNode = head;
Node nextNode = head;
while(i <= k) {
nextNode = nextNode.next;
i++;
}
while(nextNode != null) {
preNode = preNode.next;
nextNode = nextNode.next;
}
return preNode;
}
/**
* 寻找链表中的中间节点
* 设置两个指针,一个每次走1步,一个每次走两步,
* 走两步的遍历完,然后走一步的指针,那就是中间节点
* @return中间节点
*/
public Node findMidNode() {
Node preNode = head;
Node nextNode = head;
while(nextNode.next != null) {
preNode = preNode.next;
nextNode = nextNode.next;
nextNode = nextNode.next;
}
return preNode;
}
public static void main(String[] args) {
IList mylist = new IList();
mylist.addNode(1);
mylist.addNode(2);
mylist.addNode(3);
mylist.addNode(4);
mylist.traverse();
System.out.println("find value is 1 : "+mylist.findNode(1));
mylist.deleteNode(1);
mylist.traverse();
System.out.println("链表的中间值是:"+mylist.findMidNode().date);
System.out.println("find value is 1 : "+mylist.findNode(1));
System.out.println(mylist.length());
mylist.insertNode(1, 4);
mylist.traverse();
int k = 4;
Node node = mylist.findPreNode(k);
System.out.println("倒数第"+ k +"个节点是:"+node.date);
}
}