单链表的实现
或许最简单的事情细节最多。单链表中也是有很多值得思考的东西。有些变量为什么赋这个值,而不能赋那个值? 本可以200行搞定的单链表。写了许多注释,就到400行左右了。 当然其中也有些暂时没有解决的问题。各位大佬,不吝赐教!!
import java.util.Stack;
public class SingleLinkedListTest {
public static void main(String[] args) {
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.addByOrder(hero1);
singleLinkedList.addByOrder(hero4);
singleLinkedList.addByOrder(hero2);
singleLinkedList.addByOrder(hero3);
System.out.println("原来链表的情况~~");
singleLinkedList.list();
HeroNode newHeroNode = new HeroNode(2, "卢", "麒麟++++");;
singleLinkedList.update(newHeroNode);
System.out.println("更新后链表的情况~~");
singleLinkedList.list();
System.out.println("原来链表的情况~~");
singleLinkedList.list();
System.out.println("反转单链表~~");
reverseList(singleLinkedList.getHead());
singleLinkedList.list();
System.out.println("测试逆序打印单链表, 没有改变链表的结构~~");
reversePrint(singleLinkedList.getHead());
System.out.println("测试合并~~");
testMerge();
}
public static void testMerge() {
SingleLinkedList s1 = new SingleLinkedList();
s1.addNode(new HeroNode(1,"1","1"));
s1.addNode(new HeroNode(3,"20","20"));
s1.addNode(new HeroNode(4,"4","4"));
s1.addNode(new HeroNode(10,"10","10"));
SingleLinkedList s2 = new SingleLinkedList();
s2.addNode(new HeroNode(3,"1","1"));
s2.addNode(new HeroNode(5,"20","20"));
s2.addNode(new HeroNode(20,"4","4"));
s2.addNode(new HeroNode(50,"10","10"));
s1.head = merge(s1.getHead(),s2.getHead());
s1.list();
}
public static HeroNode merge(HeroNode head1, HeroNode head2) {
HeroNode newHead = new HeroNode(0, "", "");
HeroNode temp = newHead;
HeroNode temp1 = head1.next;
HeroNode temp2 = head2.next;
boolean loop = true;
boolean key1 = true;
boolean key2 = true;
while (loop) {
if (temp1 == null) {
key1 = false;
break;
}
if (temp2 == null) {
key2 = false;
break;
}
if (temp1.no < temp2.no) {
temp.next = temp1;
temp1 = temp1.next;
} else if (temp1.no > temp2.no) {
temp.next = temp2;
temp2 = temp2.next;
} else {
temp.next = temp1;
temp1 = temp1.next;
temp2 = temp2.next;
}
temp = temp.next;
}
if (key1)
temp.next = temp1;
if (key2)
temp.next = temp2;
return newHead;
}
public static void reversePrint(HeroNode head) {
Stack<HeroNode> stack = new Stack();
HeroNode temp = head.next;
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
while (!stack.empty()) {
System.out.println(stack.pop());
}
}
public static int getLength(HeroNode head) {
int length = 0;
HeroNode temp = head.next;
while (temp != null) {
length++;
temp = temp.next;
}
return length;
}
public static HeroNode getLastIndexNode(HeroNode head, int index) {
int length = getLength(head);
if (index > length) {
System.out.println("输入数字超出范围");
return null;
}
HeroNode temp = head;
for (int i = 0; i < length - index + 1; i++) {
temp = temp.next;
}
return temp;
}
public static void reverseList(HeroNode head) {
if (head.next == null || head.next.next == null)
return;
HeroNode reverseHead = new HeroNode(0,"","");
HeroNode cur = head.next;
HeroNode curNext = null;
while (cur != null) {
curNext = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = curNext;
}
head.next = reverseHead.next;
}
}
class SingleLinkedList {
public HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return head;
}
public void addNode(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.next == null)
break;
temp = temp.next;
}
temp.next = heroNode;
}
public void addByOrder(HeroNode heroNode) {
if (heroNode == null) {
System.out.println("输入节点不能为null");
return;
}
HeroNode temp = head;
while (true) {
if (temp.next == null)
break;
if (heroNode.no == temp.next.no) {
System.out.println("节点已经存在,无法添加");
return;
} else
if (heroNode.no < temp.next.no)
break;
temp = temp.next;
}
heroNode.next = temp.next;
temp.next = heroNode;
}
public void update(HeroNode heroNode) {
if (heroNode == null) {
System.out.println("所输入要修改的节点不能为 null");
return;
}
HeroNode temp = head.next;
boolean flag = false;
while (true) {
if (temp == null)
break;
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 del(int no) {
HeroNode temp = head;
boolean loop = true;
while (loop) {
if (temp.next == null) {
System.out.println("遍历到尾部,未找到要修改的节点");
return;
}
if (temp.next.no == no) {
loop = false;
break;
}
temp = temp.next;
}
temp.next = temp.next.next;
}
public void list() {
if (head == null) {
System.out.println("链表为空");
return;
}
HeroNode temp = head.next;
while (temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
}
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;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name +", nickname=" + nickname + "]";
}
}
|