文章目录
- 1.单链表
- 1.基本介绍
-
- 2.应用实例
- 1.需求分析
- 2.思路分析
- 3.完成添加和显示链表信息,直接添加到链表的尾部
- 4.根据排名添加,如果排名重复则给出提示
- 5.根据新节点的编号来修改信息
- 6.删除指定id的节点
- 3.单链表面试题
- 1.题目
- 2.面试题一
- 2.面试题二
- 3.面试题三
- 4.面试题四
- 2.双向链表
-
- 3.单向环形链表
- 1.题目
- 2.思路分析
- 3.首先构建一个环形链表
-
- 4.具体问题解决
-
1.基本介绍
1.定义
2.逻辑结构
2.应用实例
1.需求分析
2.思路分析
3.完成添加和显示链表信息,直接添加到链表的尾部
package com.sun.linkedlist;import java.util.Scanner;class Test {public static void main(String[] args) {SingleLinkedList singleLinkedList = new SingleLinkedList();Node node1 = new Node(1, "李白", "青莲剑歌");Node node2 = new Node(2, "杜甫", "青莲剑歌");Node node3 = new Node(3, "周瑜", "青莲剑歌");Node node4 = new Node(4, "诸葛亮", "青莲剑歌");singleLinkedList.add(node2);singleLinkedList.add(node1);singleLinkedList.add(node4);singleLinkedList.add(node3);boolean flag = true;while (flag) {System.out.println("1:显示,2:入队,3:出队,4:退出");Scanner scanner = new Scanner(System.in);int cmd = scanner.nextInt();switch (cmd) {case 1:singleLinkedList.list();break;case 2:System.out.println("请输入要添加的数据:");System.out.println("编号:");int no = scanner.nextInt();System.out.println("姓名:");String name = scanner.next();System.out.println("技能");String skill = scanner.next();Node node = new Node(no, name, skill);singleLinkedList.add(node);System.out.println("添加成功!");break;case 3:break;case 4:flag = false;System.out.println("退出系统");}}}
}
public class SingleLinkedList {private Node head = new Node();public void add(Node node) {Node temp = head;while (true) {if (temp.next != null) {temp = temp.next;continue;}temp.next = node;break;}}public void list() {Node temp = head;while (temp.next != null) {System.out.println(temp.next);temp = temp.next;}}}
class Node {public int no;public String name;public String skill;public Node next;public Node() {}public Node(int no, String name, String skill) {this.no = no;this.name = name;this.skill = skill;}@Overridepublic String toString() {return "Node{" +"no=" + no +", name='" + name + '\'' +", skill='" + skill + '\'' +'}';}
}
4.根据排名添加,如果排名重复则给出提示
public void addBySequential(Node node) {Node temp = head;while (temp.next != null && temp.next.no <= node.no) {temp = temp.next;}node.next = temp.next;temp.next = node;}
5.根据新节点的编号来修改信息
public void update(Node newNode) {Node temp = head;while (temp.next != null) {if (temp.next.no == newNode.no) {temp.next.name = newNode.name;temp.next.skill = newNode.skill;System.out.println("修改成功!修改后的节点信息为:" + temp.next);break;}temp = temp.next;}}
6.删除指定id的节点
public void del(int no) {Node temp = head;while (temp.next != null) {if (temp.next.no == no) {temp.next = temp.next.next;System.out.println("节点" + no + "删除成功!");break;}temp = temp.next;}}
3.单链表面试题
1.题目
2.面试题一
public int getLength(Node head) {Node temp = head;int length = 0; while (temp.next != null) {length++;temp = temp.next;}return length;}
2.面试题二
public Node numberOfCollapses(Node head, int tail) {int length = getLength(head);if (tail > length) {throw new RuntimeException("输入有误!");}int index = length - tail + 1;Node temp = head;int temp_index = 0; while (temp.next != null) {temp_index++;if (temp_index == index) {return temp.next;}temp = temp.next;}return null;}
3.面试题三
public void linkedListInversion(Node head) {if (head.next == null || head.next.next == null) {return;}Node temp1 = head.next;Node temp2 = new Node();Node reverseHead = new Node();while (temp1 != null) {if (temp1.next != null) {temp2 = temp1.next;} else {temp2 = null;}temp1.next = reverseHead.next;reverseHead.next = temp1;temp1 = temp2;}head.next = reverseHead.next;}
4.面试题四
public void reversePrint(Node head) {if (head.next == null) {return;}Stack<Node> stack = new Stack<>();Node temp = head.next;while (temp != null) {stack.push(temp);temp = temp.next;}while (stack.size() > 0) {System.out.println(stack.pop());}}
1.增删改查分析图解
2.代码实现
package com.sun.linkedlist;
class DoubleLinkedListDemoTest {public static void main(String[] args) {DoubleLinkedListDemo doubleLinkedListDemo = new DoubleLinkedListDemo();Node2 node1 = new Node2(1, "李白", "青莲剑歌");Node2 node2 = new Node2(2, "杜甫", "青莲剑歌");Node2 node3 = new Node2(3, "周瑜", "青莲剑歌");Node2 node4 = new Node2(4, "诸葛亮", "青莲剑歌");doubleLinkedListDemo.add(node1);doubleLinkedListDemo.add(node2);doubleLinkedListDemo.add(node3);doubleLinkedListDemo.add(node4);doubleLinkedListDemo.list();Node2 updateNode = new Node2(4, "4", "4");doubleLinkedListDemo.update(updateNode);doubleLinkedListDemo.del(4);doubleLinkedListDemo.list();System.out.println("====================================");Node2 node5 = new Node2(5, "5", "5");Node2 node6 = new Node2(6, "6", "6");Node2 node7 = new Node2(7, "7", "7");Node2 node8 = new Node2(8, "8", "8");doubleLinkedListDemo.insertByOrder(node8);doubleLinkedListDemo.insertByOrder(node7);doubleLinkedListDemo.insertByOrder(node6);doubleLinkedListDemo.insertByOrder(node5);doubleLinkedListDemo.list();}
}public class DoubleLinkedListDemo {private Node2 head = new Node2(-1, "", "");public Node2 getHead() {return head;}public void list() {if (head.next == null) {System.out.println("链表为空");return;}Node2 temp = head.next;while (temp != null) {System.out.println(temp);temp = temp.next;}}public void add(Node2 node2) {Node2 temp = head;while (temp.next != null) {temp = temp.next;}temp.next = node2;node2.pre = temp;}public void update(Node2 newNode) {Node2 temp = head;while (temp.next != null) {if (temp.next.no == newNode.no) {temp.next.name = newNode.name;temp.next.skill = newNode.skill;System.out.println("修改成功!修改后的节点信息为:" + temp.next);break;}temp = temp.next;}}public void del(int no) {if (head.next == null) {System.out.println("链表为空,不能删除");return;}Node2 temp = head.next;while (temp != null) {if (temp.no == no) {temp.pre.next = temp.next;if (temp.next != null) {temp.next.pre = temp.pre;}System.out.println("节点" + no + "删除成功!");break;}temp = temp.next;}}public void insertByOrder(Node2 newNode) {Node2 temp = head;while (temp.next != null && temp.next.no < newNode.no) {temp = temp.next;}if (temp.next == null) {temp.next = newNode;newNode.pre = temp;return;}temp.next.pre = newNode;newNode.next = temp.next;temp.next = newNode;newNode.pre = temp;}}
class Node2 {public int no;public String name;public String skill;public Node2 next;public Node2 pre;public Node2() {}public Node2(int no, String name, String skill) {this.no = no;this.name = name;this.skill = skill;}@Overridepublic String toString() {return "Node{" +"no=" + no +", name='" + name + '\'' +", skill='" + skill + '\'' +'}';}
}
3.单向环形链表
1.题目
2.思路分析
3.首先构建一个环形链表
1.思路分析
2.代码
package com.sun.linkedlist;
public class Josephu {private Boy first = null;public static void main(String[] args) {Josephu josephu = new Josephu();josephu.addBoy(3);josephu.show();}public void addBoy(int nums) {if (nums < 1) {System.out.println("输入数据有误!");return;}Boy boy1 = new Boy(1);first = boy1;first.setNext(first);Boy curBoy = first;for (int i = 2; i <= nums; i++) {Boy boy = new Boy(i);curBoy.setNext(boy);boy.setNext(first);curBoy = boy;}}public void show() {Boy temp = first;while (true) {System.out.print(temp.getNo() + " ");if (temp.getNext() == first) {return;} else {temp = temp.getNext();}}}
}
class Boy {private int no;private Boy next;public Boy(int no) {this.no = no;}public Boy getNext() {return next;}public void setNext(Boy next) {this.next = next;}public int getNo() {return no;}
}
4.具体问题解决
1.思路分析
2.代码
public void countChild(int ranking, int count, int total) {if (ranking < 1 || ranking > total) {System.out.println("输入数据有误");return;}addBoy(total);Boy helper = first;while (true) {if (helper.getNext() == first) {break;}helper = helper.getNext();}for (int i = 0; i < ranking - 1; i++) {first = first.getNext();helper = helper.getNext();}while (true) {if (helper == first) {System.out.println(helper.getNo() + "出队");break;}for (int i = 0; i < count - 1; i++) {first = first.getNext();System.out.println(first.getNo() + "出队");helper = helper.getNext();}first = first.getNext();helper.setNext(first);System.out.println();}}