直接上代码,有不合理之处希望大伙们提点修改意见:
包含了单链表的删除,增加,顺序增加,修改,查找倒数第k个元素,递归思想创建链表,递归思想反转链表,非递归思想反转链表,逆序打印链表;
package com.housy.linklist;
import java.awt.HeadlessException;
import java.lang.Thread.State;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "李四", "哈哈1");
HeroNode heroNode2 = new HeroNode(2, "张三", "哈哈2");
HeroNode heroNode3 = new HeroNode(3, "王五", "哈哈3");
HeroNode heroNode4 = new HeroNode(4, "赵六", "哈哈4");
SingleLinkedList singleLinkedList = new SingleLinkedList();
//-------------------------testing the list to add----------------------------------
singleLinkedList.orderAdd(heroNode1);
singleLinkedList.orderAdd(heroNode3);
singleLinkedList.orderAdd(heroNode4);
singleLinkedList.orderAdd(heroNode2);
// singleLinkedList.list();
//-------------------------testing the list to modify----------------------------------
HeroNode heroNode = new HeroNode(3, "王五", "修改哈哈哈3");
singleLinkedList.modify(heroNode);
singleLinkedList.list();
//-------------------------testing the list to delete----------------------------------
// singleLinkedList.delete(heroNode1);
// singleLinkedList.list();
// System.out.println(SingleLinkedList.size(singleLinkedList.getHead()));
//-------------------------used for testing list the reciprocal of the first k elements----------------------------------
// System.out.println(singleLinkedList.countBackwardsByK(3));
-------------------------recursive thought to create linked list----------------------------------
// singleLinkedList.createLinkedList(Arrays.asList());
// singleLinkedList.list();
// System.out.println("------------------------");
// singleLinkedList.createLinkedList(Arrays.asList(1));
// singleLinkedList.list();
// System.out.println("------------------------");
// singleLinkedList.createLinkedList(Arrays.asList(1,2,3,4));
// singleLinkedList.list();
//-------------------------recursive thought to reverse linked list----------------------------------
// System.out.println(singleLinkedList.createLinkedList(Arrays.asList()));
// singleLinkedList.reverseLinkedList(singleLinkedList.createLinkedList(Arrays.asList()));
// System.out.println("-------------------null-----");
// singleLinkedList.list();
// System.out.println("------------------------");
// singleLinkedList.reverseLinkedList(singleLinkedList.createLinkedList(Arrays.asList(1)));
// singleLinkedList.list();
// System.out.println("------------------------");
// singleLinkedList.reverseLinkedList(singleLinkedList.createLinkedList(Arrays.asList(1,2,3,4)));
// singleLinkedList.list();
//-------------------------non-recursive thought to reverse linked list----------------------------------
// System.out.println(singleLinkedList.createLinkedList(Arrays.asList()));
// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLinkedList(Arrays.asList()));
// System.out.println("-------------------null-----");
// singleLinkedList.list();
// System.out.println("------------------------");
// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLinkedList(Arrays.asList(1)));
// singleLinkedList.list();
// System.out.println("------------------------");
// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLinkedList(Arrays.asList(1,2,3,4)));
// singleLinkedList.list();
// System.out.println("------------------------");
// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLargeLinkedList(100000));
// singleLinkedList.list();
// singleLinkedList.createLargeLinkedList(10);
// singleLinkedList.list();
//-----------------------------reverse print test--------------------------------
singleLinkedList.reversePrint();
}
}
class SingleLinkedList {
/**
* 判空
* 遍历
*
* 先一般情况后特殊
*/
private HeroNode head = new HeroNode(0, null, null);
// private int i = 0;
public HeroNode getHead() {
return head;
}
//------------------------reverse print-------------------------
public void reversePrint() {
if (head.next == null) {
return;
}
Stack stack = new Stack<>();
HeroNode temp = head.next;
while(temp != null) {
stack.push(temp);
temp = temp.next;
}
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
//--------------------recursive thought-------------------------
public HeroNode createLinkedList(List no) {
if (no.isEmpty()) {
return null;
}
HeroNode firstNode = new HeroNode(no.get(0), null, null);
if (no.get(0) == 1) {
head.next = firstNode;
}
//System.out.println(no.get(0) + "运行第" + i ++ +"次 " + firstNode);
firstNode.next = createLinkedList(no.subList(1, no.size()));
return firstNode;
}
public HeroNode reverseLinkedList(HeroNode heroNode) {
System.out.println(heroNode);
if (heroNode == null || heroNode.next == null ) {
head.next = heroNode;
System.out.println(heroNode);
// System.out.println("head.next = " + head.next);
return heroNode;
}
HeroNode newHead = reverseLinkedList(heroNode.next);
heroNode.next.next = heroNode;
heroNode.next = null;
return newHead;
}
//--------------------non-recursive thought-------------------------
public HeroNode reverseLinkedListByNonrecursive(HeroNode heroNode) {
HeroNode newHead = null;
HeroNode curHead = heroNode;
while(curHead != null) {
//curHead == null
HeroNode next = curHead.next;
curHead.next = newHead;
newHead = curHead;
curHead = next;
}
head.next = newHead;
return newHead;
}
public HeroNode createLargeLinkedList(int size) {
HeroNode prev = null;
HeroNode first = null;
for(int i = 1; i <= size; i++) {
HeroNode heroNode = new HeroNode(i, null, null);
if (prev != null) {
prev.next = heroNode;
} else {
first = heroNode;
head.next = first;
}
prev = heroNode;
// System.out.println(prev);
}
return first;
}
public void add(HeroNode heroNode) {
HeroNode temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = heroNode;
}
public void list() {
HeroNode temp = head.next;
// System.out.println("head.next = " + head.next);
if (head.next == null) {
return;
}
while(temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
public void orderAdd(HeroNode heroNode) {
HeroNode temp = head;
boolean flag = false;
while(temp.next != null) {
if (temp.next.no > heroNode.no) {
break;
} else if (temp.next.no == heroNode.no) {
flag = true;
}
temp = temp.next;
}
if (flag) {
System.out.println("编号存在");
} else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
public void modify(HeroNode heroNode) {
if (head.next == null) {
return;
}
HeroNode temp = head.next;
boolean flag = false;
while(temp != null) {
if (temp.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = heroNode.name;
temp.nickname = heroNode.nickname;
} else {
System.out.println("未找到!");
}
}
public void delete(HeroNode heroNode) {
if (head.next == null) {
return;
}
HeroNode temp = head.next;
boolean flag = false;
while(temp != null) {
if (temp.no == heroNode.no) {
// System.out.println(heroNode.next );
head.next = heroNode.next;
break;
}
if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
// System.out.println(flag + " " + temp);
if (flag) {
temp.next = heroNode.next;
heroNode.next = null;
}
}
public static int size(HeroNode head) {
int length = 0;
if (head.next == null) {
return 0;
}
HeroNode temp = head.next;
while(temp != null) {
length++;
temp = temp.next;
}
return length;
}
public HeroNode countBackwardsByK(int k) {
int count = size(getHead()) - k;
HeroNode temp = head.next;
while(count != 0) {
temp = temp.next;
count--;
}
return temp;
}
}
class HeroNode {
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
this.next = null;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}
在利用栈逆向打印链表的时候有这么一句代码:Stack
有关泛型:后面尖括号里面是可以为null也可以为HeroNode,这是由于在声明stack的时候,就已经知道了这是一个存放HeroNode类型的栈,所以加不加都是可以的,在早期java(jdk1.5之前)中,是没有尖括号的,没有泛型这个概念,意思是Stack
package com.housy.linklist;
public class DoubleLinkedListDemo {
public static void main(String[] args) {
HeroNode2 heroNode1 = new HeroNode2(1, "Bob", "foolish");
HeroNode2 heroNode2 = new HeroNode2(2, "Alice", "beautiful");
HeroNode2 heroNode3 = new HeroNode2(3, "Housy", "cutey");
HeroNode2 heroNode4 = new HeroNode2(4, "Jack", "intelligent");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(heroNode1);
doubleLinkedList.add(heroNode2);
doubleLinkedList.add(heroNode3);
doubleLinkedList.add(heroNode4);
doubleLinkedList.list();
System.out.println("---------------------delete 4-----------------");
doubleLinkedList.delete(4);
doubleLinkedList.list();
System.out.println("---------------------delete 2-----------------");
doubleLinkedList.delete(2);
doubleLinkedList.list();
}
}
class DoubleLinkedList {
private HeroNode2 head = new HeroNode2(0, null, null);
public HeroNode2 getHead() {
return head;
}
public void add(HeroNode2 heroNode) {
HeroNode2 temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = heroNode;
heroNode.pre = temp;
}
public void delete(int no) {
if (head.next == null) {
System.out.println("链表为null");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;
while(temp != null) {
if (temp.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.pre.next = temp.next;
if (temp.next != null) {
temp.next.pre = temp.pre;
}
}
}
public void list() {
HeroNode2 temp = head.next;
// System.out.println("head.next = " + head.next);
if (head.next == null) {
System.out.println("---------------null------------");
return;
}
while(temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
}
class HeroNode2 {
public int no;
public String name;
public String nickname;
public HeroNode2 next;
public HeroNode2 pre;
public HeroNode2(int no, String name, String nickname) {
super();
this.no = no;
this.name = name;
this.nickname = nickname;
this.next = null;
this.pre = null;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}