1、链表
链表是有序的列表,但是它在内存中是存储如下 小结:
- 链表是以节点的方,来存储是链式存储
- 每个节点包含data域,next域:指向下一个节点
- 如图:发现链表的各个节点不一定是连续存储
- 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
- 单链表(带头结点)逻辑结构示意图如下
2、单链表的应用
使用带head头的单向链表实现-水浒英雄排行榜管理完成对英雄人物的増删改査操作,
- 第一种方法在添加英雄时,直接添加到链表的尾部
- 第二种方式在添加英雄时,根据排名将英雄插入到指定位置(如果有这个排名,则添加失败,并给出提示)思路的分析示意图:
- 修改节点功能
思路(1)先找到该节点,通过遍历,(2) temp.name= newheroNode name; temp. nickname= newheroNode.nickname
- 删除节点
思路分析示意图:
3、代码实现
package com.jl.linkedlist;
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 singleLinkedList = new SingleLinkedList();
singleLinkedList.addByOrder(hero1);
singleLinkedList.addByOrder(hero4);
singleLinkedList.addByOrder(hero3);
singleLinkedList.addByOrder(hero2);
singleLinkedList.list();
HeroNode newHeroBNode = new HeroNode(2, "小鹿", "玉麒麟!!!");
singleLinkedList.update(newHeroBNode);
System.out.println("修改后的情况~~");
singleLinkedList.list();
singleLinkedList.delete(1);
singleLinkedList.delete(2);
singleLinkedList.delete(4);
singleLinkedList.delete(3);
System.out.println("删除后的链表情况");
singleLinkedList.list();
}
}
class SingleLinkedList{
private 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 addByOrder(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){
System.out.printf("准备插入的英雄的编号%d已经存在,不能加入\n",heroNode.no);
}else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
public void update(HeroNode newHeroNode){
if(head.next == null){
System.out.println("链表为空");
return;
}
HeroNode temp = head.next;
boolean flag = false;
while (true){
if (temp == null){
break;
}
if (temp.no == newHeroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
}else {
System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no);
}
}
public void delete(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.next == null){
System.out.println("链表为空");
}
HeroNode temp = head.next;
while (true){
if (temp == null){
break;
}
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 + '\'' +
'}';
}
}
4、单链表常见面试题(新浪、百度、腾讯)
- 获取单链表的节点个数
- 查找单链表的倒数第k个节点
- 单链表的反转
- 从尾到头打印单链表
package com.jl.linkedlist;
import java.util.Stack;
public class LinkedList {
public static int getLength(HeroNode head){
if (head.next == null){
return 0;
}
int length = 0;
HeroNode cur = head.next;
while (cur != null){
length++;
cur = cur.next;
}
return length;
}
public static HeroNode findLastIndexNode(HeroNode head,int index){
if (head.next == null){
return null;
}
int size = getLength(head);
if (index < 0 || index > size){
return null;
}
HeroNode cur = head.next;
for (int i = 0; i < size - index; i++) {
cur = cur.next;
}
return cur;
}
public static void reversetList(HeroNode head){
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 static void reversePrint(HeroNode head){
if (head.next == null){
return;
}
Stack<HeroNode> stack = new Stack<HeroNode>();
HeroNode cur = head.next;
while (cur != null){
stack.push(cur);
cur = cur.next;
}
System.out.println(stack.pop());
}
}
5、双向链表应用
双向链表的操作分析
使用带head头的双向链表实现-水浒英雄排行榜 管理单向链表的缺点分析:
- 单向链表,査找的方向只能是一个方向,而双向链表可以向前或者向后查找。
- 单向链表不能自我删除,需要靠辅助节点,而双向链表,则可以自我删除,所以前面我们单链表删除时节点,总是找到temp,temp是待删除节点的前一个节点(认真体会)
- 分析了双向链表如何完成遍历,添加,修改和删除的思路
代码实现
package com.jl.linkedlist;
public class DoubleLinkedListDemo {
public static void main(String[] args) {
System.out.println("双向链表的测试");
HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(hero1);
doubleLinkedList.add(hero2);
doubleLinkedList.add(hero3);
doubleLinkedList.add(hero4);
doubleLinkedList.list();
HeroNode2 newHeroNode = new HeroNode2(4,"公孙胜","入云龙");
doubleLinkedList.update(newHeroNode);
System.out.println("修改后的链表情况");
doubleLinkedList.list();
doubleLinkedList.delete(3);
System.out.println("删除后的链表情况");
doubleLinkedList.list();
}
}
class DoubleLinkedList{
private HeroNode2 head = new HeroNode2(0,"","");
public HeroNode2 getHead(){
return head;
}
public void list(){
if (head.next == null){
System.out.println("链表为空");
}
HeroNode2 temp = head.next;
while (true){
if (temp == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
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 update(HeroNode2 newHeroNode){
if(head.next == null){
System.out.println("链表为空");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;
while (true){
if (temp == null){
break;
}
if (temp.no == newHeroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
}else {
System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no);
}
}
public void delete(int no){
if (head.next == null){
System.out.println("链表为空无法删除");
}
HeroNode2 temp = head.next;
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 {
System.out.printf("要删除的%d节点不存在",no);
}
}
}
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 + '\'' +
'}';
}
}
|