单向链表
package one.linkedlist;
import java.util.Stack;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode node1 = new HeroNode(1, "宋亚楠", "songyanan");
HeroNode node2 = new HeroNode(2, "刘吉昂", "liujiang");
HeroNode node3 = new HeroNode(3, "侯强", "houqiang");
HeroNode node4 = new HeroNode(4, "李四", "lisi");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.addByOrder(node2);
singleLinkedList.addByOrder(node1);
singleLinkedList.addByOrder(node4);
singleLinkedList.addByOrder(node3);
singleLinkedList.delNode(4);
singleLinkedList.list();
System.out.println("有效节点个数:" + singleLinkedList.length());
System.out.println("倒数第n个节点:" + singleLinkedList.findLastNode(1));
System.out.println("节点反转begin:");
singleLinkedList.list();
System.out.println("节点反转end:");
singleLinkedList.reverseList();
singleLinkedList.list();
System.out.println("逆序打印");
singleLinkedList.reverseOrderPrint();
}
}
class SingleLinkedList {
private HeroNode head = new HeroNode(0, "", "");
public void add(HeroNode node) {
HeroNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
public void list() {
if (head.next == null) {
System.err.println("链表为空 。。。");
return;
}
HeroNode heroNode = head.next;
while (heroNode != null) {
System.out.println(heroNode);
heroNode = heroNode.next;
}
}
public void addByOrder(HeroNode node) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > node.no) {
break;
} else if (temp.next.no == node.no) {
System.err.printf("add err 英雄【%d】存在\n", node.no);
flag = true;
break;
}
temp = temp.next;
}
if (!flag) {
node.next = temp.next;
temp.next = node;
}
}
public void delNode(int no) {
HeroNode temp = head;
while (true) {
if (temp.next.no == no) {
temp.next = temp.next.next;
break;
}
temp = temp.next;
if (temp.next == null) {
System.err.println("要删除的节点未找到..." + no);
return;
}
}
}
public int length() {
HeroNode temp = this.head.next;
int count = 0;
while (temp != null) {
temp = temp.next;
count++;
}
return count;
}
public HeroNode findLastNode(int n) {
if (head.next == null) {
return null;
}
int length = length();
if (n > length || n <= 0) {
return null;
}
HeroNode curr = head.next;
for (int i = 0; i < length - n; i++) {
curr = curr.next;
}
return curr;
}
public void reverseList() {
if (head.next == null || head.next.next == null) {
return;
}
HeroNode cur = head.next;
HeroNode next = null;
HeroNode reverseHead = new HeroNode(0, "", "");
while (cur != null) {
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
head.next = reverseHead.next;
}
public void reverseOrderPrint() {
HeroNode curr = head.next;
Stack<HeroNode> heroNodes = new Stack<>();
while (curr != null) {
heroNodes.push(curr);
curr = curr.next;
}
while (!heroNodes.empty()) {
System.out.println(heroNodes.pop());
}
}
}
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 + '\'' +
'}';
}
}
双向两表
package one.linkedlist;
import java.util.Stack;
public class DoubleLinkedListDemo {
public static void main(String[] args) {
HeroNode2 node1 = new HeroNode2(1, "宋亚楠", "songyanan");
HeroNode2 node2 = new HeroNode2(2, "权宇航", "quanyuhang");
HeroNode2 node3 = new HeroNode2(3, "侯强", "houqiang");
HeroNode2 node4 = new HeroNode2(4, "李四", "lisi");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(node2);
doubleLinkedList.add(node1);
doubleLinkedList.add(node4);
doubleLinkedList.add(node3);
doubleLinkedList.delNode(2);
doubleLinkedList.list();
}
}
class DoubleLinkedList{
private HeroNode2 head = new HeroNode2(0, "", "");
public void add(HeroNode2 node) {
HeroNode2 temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
node.pre = temp;
}
public void list() {
if (head.next == null) {
System.err.println("链表为空 。。。");
return;
}
HeroNode2 HeroNode2 = head.next;
while (HeroNode2 != null) {
System.out.println(HeroNode2);
HeroNode2 = HeroNode2.next;
}
}
public void addByOrder(HeroNode2 node) {
HeroNode2 temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > node.no) {
break;
} else if (temp.next.no == node.no) {
System.err.printf("add err 英雄【%d】存在\n", node.no);
flag = true;
break;
}
temp = temp.next;
}
if (!flag) {
node.next = temp.next;
temp.next = node;
}
}
public void delNode(int no) {
HeroNode2 temp = head.next;
while (true) {
if (temp.no == no) {
temp.pre.next = temp.next;
temp.next.pre = temp.pre;
break;
}
temp = temp.next;
if (temp.next == null) {
System.err.println("要删除的节点未找到..." + no);
return;
}
}
}
public int length() {
HeroNode2 temp = this.head.next;
int count = 0;
while (temp != null) {
temp = temp.next;
count++;
}
return count;
}
public HeroNode2 findLastNode(int n) {
if (head.next == null) {
return null;
}
int length = length();
if (n > length || n <= 0) {
return null;
}
HeroNode2 curr = head.next;
for (int i = 0; i < length - n; i++) {
curr = curr.next;
}
return curr;
}
public void reverseList() {
if (head.next == null || head.next.next == null) {
return;
}
HeroNode2 cur = head.next;
HeroNode2 next = null;
HeroNode2 reverseHead = new HeroNode2(0, "", "");
while (cur != null) {
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
head.next = reverseHead.next;
}
public void reverseOrderPrint() {
HeroNode2 curr = head.next;
Stack<HeroNode2> HeroNode2s = new Stack<>();
while (curr != null) {
HeroNode2s.push(curr);
curr = curr.next;
}
while (!HeroNode2s.empty()) {
System.out.println(HeroNode2s.pop());
}
}
}
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) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode2{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
约瑟夫环问题
package one.linkedlist;
public class Josephus {
public static void main(String[] args) {
CircleSingleLinkedList linkedList = new CircleSingleLinkedList();
linkedList.addBoy(4);
linkedList.showBoy();
}
}
class CircleSingleLinkedList {
private Boy first = null;
public void addBoy(int nums) {
if (nums < 1) {
System.out.println("nums的值不正确");
return;
}
Boy curBoy = null;
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
if (i == 1) {
first = boy;
first.setNext(first);
curBoy = first;
} else {
curBoy.setNext(boy);
boy.setNext(first);
curBoy = boy;
}
}
}
public void showBoy() {
if (first == null) {
System.out.println("没有任何小孩~~");
return;
}
Boy curBoy = first;
while (true) {
System.out.printf("小孩的编号 %d \n", curBoy.getNo());
if (curBoy.getNext() == first) {
break;
}
curBoy = curBoy.getNext();
}
}
}
class Boy {
private int no;
private Boy next;
public Boy(int no) {
this.no = no;
this.next = null;
}
public void setNext(Boy next) {
this.next = next;
}
public Boy getNext() {
return next;
}
@Override
public String toString() {
return "Boy{" +
"no=" + no +
'}';
}
public int getNo() {
return no;
}
}