介绍
??链表是有序的列表,链表是以节点的方式储存,每个节点包含data域、next域(指向下一个节点),链表的各个节点不一定是连续存储的;链表分带头节点的链表和没有头结点的链表。它在内存中的储存结构如下:
单链表
??单链表(带头节点)逻辑结构图如下
单链表的应用实例
??实现单链表的增删改操作 ??1. 定义数据类
public 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 + '\'' +
'}';
}
}
??2. 节点的增删改操作
public 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 addByOrder(HeroNode node) {
HeroNode temp = head;
boolean flag = false;
while (temp.next != null) {
if (node.no < temp.next.no) {
break;
} else if (node.no == temp.next.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
System.out.printf("插入的英雄编号%d已存在, 不能重复插入", node.no);
System.out.println();
} else {
node.next = temp.next;
temp.next = node;
}
}
public void update(HeroNode node) {
if (head.next == null) {
System.out.println("链表为空~~~");
return;
}
HeroNode temp = head;
boolean flag = false;
while (temp.next != null) {
if (temp.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
node.next = temp.next;
temp.name = node.name;
temp.nickname = node.nickname;
} else {
System.out.printf("未找到编号为%d的数据", node.no);
}
}
public void delete(HeroNode node) {
if (head.next == null) {
System.out.println("链表为空~~~");
return;
}
HeroNode temp = head;
boolean flag = false;
while (temp.next != null) {
if (temp.next.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.printf("未找到编号为%d的数据", node.no);
System.out.println();
}
}
public void list() {
if (head.next == null) {
System.out.println("链表为空~~~");
return;
}
HeroNode temp = head.next;
while (temp != null) {
System.out.println(temp.toString());
temp = temp.next;
}
}
}
??3. 测试类
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode node1 = new HeroNode(1, "宋江", "及时雨");
HeroNode node2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode node3 = new HeroNode(3, "吴用", "智多星");
HeroNode node4 = new HeroNode(4, "林冲", "豹子头");
SingleLinkedList list = new SingleLinkedList();
list.add(node1);
list.add(node2);
list.add(node3);
list.add(node4);
list.list();
System.out.println("====================================分割线=======================================");
list = new SingleLinkedList();
list.addByOrder(node1);
list.addByOrder(node4);
list.addByOrder(node2);
list.addByOrder(node2);
list.addByOrder(node3);
list.list();
System.out.println("====================================分割线=======================================");
node3 = new HeroNode(3, "吴用", "智少星");
list.update(node3);
list.list();
System.out.println("====================================分割线=======================================");
list.delete(node4);
list.delete(node1);
list.delete(node3);
list.delete(node2);
list.list();
}
}
单链表面试题
??1. 单链表有效节点个数
public static int getLength(HeroNode node) {
if (node.next == null) {
return 0;
}
int length = 0;
HeroNode temp = node.next;
while (temp != null) {
length++;
temp = temp.next;
}
return length;
}
??2. 单链表的倒数第K个节点
public static HeroNode findLastNode(HeroNode node, int index) {
if (node == null) {
return null;
}
int length = getLength(node);
if (index > length || index <= 0) {
System.out.printf("越界, 不存在倒数第%d的节点", index);
System.out.println();
return null;
}
HeroNode current = node.next;
for (int i = 0; i < length - index; i++) {
current = current.next;
}
return current;
}
??3. 单链表的反转
public static void reverse(HeroNode node) {
if (node.next == null || node.next.next == null) {
return;
}
HeroNode cur = node.next;
HeroNode next = null;
HeroNode reverseHead = new HeroNode(0, "", "");
while (cur != null) {
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
node.next = reverseHead.next;
}
??4. 从尾到头打印单链表
public static void reversePrint(HeroNode node) {
if (node.next == null) {
return;
}
Stack<HeroNode> stack = new Stack<>();
HeroNode temp = node.next;
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
双向链表
??单向链表查找的方向只有一个,而双向链表可以向前或者向后查找;单向链表不能自我删除,需要靠辅助节点,而双线链表可以自我删除
双向链表的应用实例
??实现双向链表的增删改操作 ??1. 定义数据类
public 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 "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
??2. 节点的增删改操作
public class DoubleLinkedList {
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
public void list() {
if (head.next == null) {
System.out.println("链表为空~~~");
return;
}
HeroNode2 temp = head.next;
while (temp != null) {
System.out.println(temp.toString());
temp = temp.next;
}
}
public void add(HeroNode2 node) {
HeroNode2 temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
node.pre = temp;
}
public void update(HeroNode2 node) {
if (head.next == null) {
System.out.println("链表为空~~~");
return;
}
HeroNode2 temp = head;
boolean flag = false;
while (temp.next != null) {
if (temp.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = node.name;
temp.nickname = node.nickname;
} else {
System.out.printf("未找到编号为%d的数据", node.no);
}
}
public void delete(HeroNode2 node) {
if (head.next == null) {
System.out.println("链表为空~~~");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;
while (temp != null) {
if (temp.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.pre.next = temp.next;
if (temp.next != null) {
temp.next.pre = temp.pre;
}
} else {
System.out.printf("未找到编号为%d的数据", node.no);
System.out.println();
}
}
public void addByOrder(HeroNode2 node) {
HeroNode2 temp = head;
boolean flag = false;
while (temp.next != null) {
if (node.no < temp.next.no) {
break;
} else if (node.no == temp.next.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
System.out.printf("插入的英雄编号%d已存在, 不能重复插入", node.no);
System.out.println();
} else {
if (temp.next != null) {
temp.next.pre = node;
}
node.next = temp.next;
node.pre = temp;
temp.next = node;
}
}
}
??3. 测试类
public class DoubleLinkedListDemo {
public static void main(String[] args) {
HeroNode2 node1 = new HeroNode2(1, "宋江1", "及时雨1");
HeroNode2 node2 = new HeroNode2(2, "卢俊义2", "玉麒麟2");
HeroNode2 node3 = new HeroNode2(3, "吴用3", "智多星3");
HeroNode2 node4 = new HeroNode2(4, "林冲4", "豹子头4");
System.out.println("====================================测试添加=======================================");
DoubleLinkedList list = new DoubleLinkedList();
list.add(node1);
list.add(node2);
list.add(node3);
list.add(node4);
list.list();
System.out.println("====================================测试修改=======================================");
node3 = new HeroNode2(3, "吴用", "智少星");
list.update(node3);
list.list();
System.out.println("====================================测试删除=======================================");
list.delete(node4);
list.list();
System.out.println("====================================测试顺序添加=======================================");
node1 = new HeroNode2(1, "宋江1", "及时雨1");
node2 = new HeroNode2(11, "卢俊义2", "玉麒麟2");
node3 = new HeroNode2(21, "吴用3", "智多星3");
node4 = new HeroNode2(31, "林冲4", "豹子头4");
list = new DoubleLinkedList();
list.addByOrder(node4);
list.addByOrder(node3);
list.addByOrder(node1);
list.addByOrder(node2);
HeroNode2 node5 = new HeroNode2(2, "林冲4", "豹子头4");
list.addByOrder(node5);
node5 = new HeroNode2(32, "林冲4", "豹子头4");
list.addByOrder(node5);
node5 = new HeroNode2(12, "林冲4", "豹子头4");
list.addByOrder(node5);
list.list();
}
}
源码地址:https://gitee.com/peachtec/data-structure-and-algorithm/tree/master/src/com/hxz/dataStructure/linkedList
|