单链表
public class SingleLinkedListDemo {
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 list = new SingleLinkedList();
list.addByNo(hero2);
list.addByNo(hero4);
list.addByNo(hero3);
list.addByNo(hero2);
list.addByNo(hero1);
System.out.println("orderbyno 插入……");
list.list();
HeroNode hero5 = new HeroNode(4, "亚托克斯", "光明使者");
list.update(hero5);
System.out.println("修改完的链表");
list.list();
list.del(2);
list.del(4);
System.out.println("删除完的链表");
list.list();
System.out.println(list.getLength(list.getHead()));
System.out.println(list.getLastIndexNode(list.getHead(),4));
list.reverse(list.getHead());
System.out.println("反转后的链表");
list.list();
System.out.println("从尾到头……");
list.reverseList(list.getHead());
SingleLinkedList list2 = new SingleLinkedList();
list2.addByNo(hero2);
list2.addByNo(hero4);
list.reverse(list.getHead());
System.out.println("打印list1");
list.list();
System.out.println("打印list2");
list2.list();
System.out.println("打印合并后的链表");
System.out.println(list.mergeTwoList(list.getHead(), list2.getHead()));
}
}
class SingleLinkedList{
HeroNode head = new HeroNode(0, "", "");
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
public void addByNo(HeroNode heroNode) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > heroNode.no) {
break;
}else if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (!flag) {
heroNode.next = temp.next;
temp.next = heroNode;
}else {
System.out.printf("要插入的节点 %d 已存在,不能插入\n",heroNode.no);
}
}
public void update(HeroNode heroNode) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next.name = heroNode.name;
temp.next.nickname = heroNode.nickname;
}else {
System.out.printf("要添加的节点%d不存在,无法修改\n", heroNode.no);
}
}
public void del(int no) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
}else {
System.out.printf("要删除的节点%d不存在,无法删除", no);
}
}
public void list() {
if (head == null) {
System.out.println("当前链表为空,没有数据呐,兄弟");
return;
}
HeroNode temp = head.next;
while (true) {
if(temp == null) {
break;
}
System.out.println(temp.toString());
temp = temp.next;
}
}
public int getLength(HeroNode head) {
if (head == null || head.next == null) {
return 0;
}
HeroNode cur = head.next;
int count = 0;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}
public HeroNode getLastIndexNode(HeroNode head,int index) {
if (head == null || head.next == null) {
return null;
}
HeroNode cur = head.next;
int count = getLength(head);
if (index < 0 || index > count) {
return null;
}
for (int i = 0; i < count - index; i++) {
cur = cur.next;
}
return cur;
}
public void reverse(HeroNode head) {
if (head.next == null || head.next.next == null) {
return;
}
HeroNode newHead = new HeroNode(0, "", "");
HeroNode cur = head.next;
HeroNode next = null;
while(cur != null) {
next = cur.next;
cur.next = newHead.next;
newHead.next = cur;
cur = next;
}
head.next = newHead.next;
}
public void reverseList(HeroNode head) {
if (head == null || head.next == null) {
System.out.println("当前链表为空");
return;
}
Stack<HeroNode> stack = new Stack<HeroNode>();
HeroNode cur = head.next;
while(cur != null) {
stack.push(cur);
cur = cur.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
public HeroNode mergeTwoList(HeroNode node1,HeroNode node2) {
if (node1.next == null && node2.next == null) {
return null;
}
HeroNode newHead = new HeroNode(0, "", "");
HeroNode prev = newHead;
while (node1.next != null && node2.next != null) {
if (node1.next.no <= node2.next.no) {
prev.next = node1.next;
node1.next = node1.next.next;
}else {
prev.next = node2.next;
node2.next = node2.next.next;
}
prev = prev.next;
}
prev.next = node1.next == null?node2.next:node1.next;
return newHead.next;
}
public HeroNode getHead() {
return head;
}
public void setHead(HeroNode head) {
this.head = head;
}
}
class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode(int no, String nameString, String nicknameString) {
this.no = no;
this.name = nameString;
this.nickname = nicknameString;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", nameString=" + name + ", nicknameString=" + nickname + next+"]";
}
}
双链表
public class DoubleLinkedListDemo {
public static void main(String[] args) {
HeroNode2 hero1 = new HeroNode2(1, "亚索", "疾风剑豪");
HeroNode2 hero2 = new HeroNode2(2, "易", "无极剑圣");
HeroNode2 hero3 = new HeroNode2(4, "菲奥娜", "无双剑姬");
HeroNode2 hero4 = new HeroNode2(5, "阿托", "暗裔剑魔");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(hero1);
doubleLinkedList.add(hero2);
doubleLinkedList.add(hero3);
doubleLinkedList.add(hero4);
doubleLinkedList.list();
HeroNode2 hero5 = new HeroNode2(4, "亚托克斯", "光明使者");
doubleLinkedList.update(hero5);
System.out.println("update ");
doubleLinkedList.list();
doubleLinkedList.del(3);
System.out.println("del");
doubleLinkedList.list();
HeroNode2 hero6 = new HeroNode2(3, "小剑魔", "使者");
System.out.println("by order print");
doubleLinkedList.addByNo(hero6);
doubleLinkedList.list();
}
}
class DoubleLinkedList{
HeroNode2 head = new HeroNode2(0,"", "");
public HeroNode2 getHead() {
return head;
}
public void add(HeroNode2 heroNode) {
HeroNode2 temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
heroNode.pre = temp;
}
public void addByNo(HeroNode2 heroNode) {
HeroNode2 temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.no > heroNode.no) {
break;
}else if (temp.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (!flag) {
temp.pre.next = heroNode;
heroNode.next = temp;
heroNode.pre = temp.pre;
temp.pre = heroNode;
}else {
System.out.printf("要插入的节点 %d 已存在,不能插入\n",heroNode.no);
}
}
public void update(HeroNode2 heroNode) {
HeroNode2 temp = head;
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.printf("要修改的节点%d不存在,无法修改\n", heroNode.no);
}
}
public void del(int no) {
if (head.next == null) {
System.out.println("链表为空无法删除");
return;
}
HeroNode2 temp = head;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
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;
}else {
temp.pre = null;
}
}else {
System.out.printf("要删除的节点%d不存在,无法删除", no);
}
}
public void list() {
if (head == null) {
System.out.println("当前链表为空,没有数据呐,兄弟");
return;
}
HeroNode2 temp = head.next;
while (true) {
if(temp == null) {
break;
}
System.out.println(temp.toString());
temp = temp.next;
}
}
}
class HeroNode2{
public int no;
public String name;
public String nickname;
public HeroNode2 pre;
public HeroNode2 next;
public HeroNode2(int no, String nameString, String nicknameString) {
this.no = no;
this.name = nameString;
this.nickname = nicknameString;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", nameString=" + name + ", nicknameString=" + nickname +"]";
}
}
链表实际应用——约瑟夫环
public class Josephu {
public static void main(String[] args) {
CircleSingleLinkedList cList = new CircleSingleLinkedList();
cList.add(250);
cList.list();
cList.getBoy(250, 1, 2);
}
}
class CircleSingleLinkedList{
Boy first = null;
public void add(int nums) {
if (nums < 1) {
System.out.println("你输入的小孩个数太少了 不够吃的啊");
return;
}
Boy curBoy = null;
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
if (i == 1) {
first = boy;
first.setNext(boy);
curBoy = first;
}else {
curBoy.setNext(boy);
boy.setNext(first);
curBoy = boy;
}
}
}
public void list() {
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();
}
}
public void getBoy(int nums,int k,int n) {
Boy helper = first;
while (true) {
if (helper.getNext() == first) {
break;
}
helper = helper.getNext();
}
for (int i = 0; i < k - 1; i++) {
first = first.getNext();
helper = helper.getNext();
}
while (true) {
if (helper == first) {
break;
}
for (int i = 0; i < n - 1; i++) {
first = first.getNext();
helper = helper.getNext();
}
System.out.printf("出圈的小孩是%d\n",first.getNo());
first = first.getNext();
helper.setNext(first);
}
System.out.printf("最后留在圈中的小孩编号%d \n", first.getNo());
}
}
class Boy{
private int no;
private Boy next;
public Boy(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Boy getNext() {
return next;
}
public void setNext(Boy next) {
this.next = next;
}
}