一:链表介绍:
链表是有序的列表,但是它在内存中是存储如下
总结:
1)链表是以节点的方式来存储,是链式存储
2)每个节点包含 data 域, next 域:指向下一个节点.
3)如图:发现链表的各个节点不一定是连续存储.
4)链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
二:单链表:
单链表(带头结点) 逻辑结构示意图如下
单链表的应用实例:
使用带head头的单向链表实现 –水浒英雄排行榜管理 完成对英雄人物的增删改查操作
package com.liuq.linkedlist;
import java.util.Stack;
import static com.liuq.linkedlist.SingleLinkedList.*;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode hero1 = new HeroNode(1, "Song", "JISHIYU");
HeroNode hero2 = new HeroNode(2, "LU", "YUQILING");
HeroNode hero3 = new HeroNode(3, "WU", "ZHIDUOXING");
HeroNode hero4 = new HeroNode(4, "LINBG", "BAOZITOU");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.add(hero3);
singleLinkedList.add(hero2);
singleLinkedList.add(hero1);
singleLinkedList.add(hero4);
singleLinkedList.list();
singleLinkedList.update(new HeroNode(7,"XXXXX","XXXXXXXX"));
singleLinkedList.list();
singleLinkedList.delete(2);
singleLinkedList.list();
}
}
class SingleLinkedList {
private HeroNode head = new HeroNode(0, " ", " ");
//返回头节点
public HeroNode getHead() {
return head;
}
public void add(HeroNode node) {
HeroNode temp = head;
while (true) {
if (temp.getNext() == null) {
break;
}
temp = temp.getNext();
}
temp.setNext(node);
}
public void delete(int id){
HeroNode temp= head;
boolean flag=false;
if(temp.getNext()==null){
System.out.println("linkedList is null ");
}
while (true){
if(temp.getNext()==null){
System.out.println("this node not exist--delete");
break;
}
if(temp.getNext().getId()==id){
flag=true;
break;
}
temp=temp.getNext();
}
if(flag){
temp.setNext(temp.getNext().getNext());
}
}
public void update(HeroNode node){
HeroNode temp= head;
if(temp.getNext()==null){
System.out.println("linkedList is null ");
}
while (true){
if(temp==null){
System.out.println("this id not exist");
break;
}
if(temp.getId()==node.getId()){
temp.setName(node.getName());
temp.setNikeName(node.getNikeName());
break;
}
temp=temp.getNext();
}
}
public void list() {
//判断链表是否为空
if (head.getNext() == null) {
System.out.println("链表为空");
return;
}
HeroNode temp = head.getNext();
while (true) {
if (temp == null) {
System.out.println("遍历结束");
break;
}
System.out.println(temp.toString());
temp = temp.getNext();
}
}
}
class HeroNode {
private int id;
private String name;
private String nikeName;
private HeroNode next;
public HeroNode(int id, String name, String nikeName) {
this.id = id;
this.name = name;
this.nikeName = nikeName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNikeName() {
return nikeName;
}
public void setNikeName(String nikeName) {
this.nikeName = nikeName;
}
public HeroNode getNext() {
return next;
}
public void setNext(HeroNode next) {
this.next = next;
}
@Override
public String toString() {
return "HeroNode{" +
"id=" + id +
", name='" + name + '\'' +
", nikeName='" + nikeName + '\'' +
'}';
}
}
public void addByOrder(HeroNode node) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.getNext() == null) {
flag = true;
break;
}
if (temp.getNext().getId() < node.getId()) {
flag = true;
break;
}
if(temp.getNext().getId() == node.getId()){
break;
}
temp = temp.getNext();
}
if (flag) {
//插入到链表中, temp的后面
node.setNext(temp.getNext());
temp.setNext(node);
}
}
求单链表中有效节点的个数:
public static int getLengthSelf(HeroNode head) {
HeroNode temp = head;
int count = 0;
if (temp.getNext() == null) {
return 0;
}
while (true) {
if (temp == null) {
break;
}
if (temp.getNext() != null) {
count++;
}
temp = temp.getNext();
}
System.out.println("length: " + count);
return count;
}
查找单链表中的倒数第k个结点 :
//查找单链表中的倒数第k个结点 【新浪面试题】
public static HeroNode findLastIndexNode(HeroNode head ,int lastIndex){
if(head==null){
return null;
}
int length = getLengthSelf(head);
if(length==0 || length-lastIndex<0){
return null;
}
HeroNode cur = head.getNext(); //3 // 3 - 1 = 2
for(int i = 0; i < length-lastIndex; i++) {
cur=cur.getNext();
}
System.out.println(cur.getId());
return cur;
}
//将单链表反转
public static void reversetList(HeroNode head) {
if(head.getNext()==null || head.getNext().getNext()==null){
return;
}
//相当于一个临时的链表
HeroNode reversetHead=new HeroNode(0," ","");
HeroNode cur =head.getNext();
//临时的节点 存储cur的状态
HeroNode next =null;
while (cur!=null){
next=cur.getNext();
//相当于把 cur.next节点 改成了reversetHead(头结点的下一个节点)的下一个节点
//是因为现在cur节点还是目标链表的元素
cur.setNext(reversetHead.getNext());
//上一行代码的赋值
reversetHead.setNext(cur);
cur = next;//让cur后移
}
//反转完成后 赋值给目标节点
// head=reversetHead; 为什么这样不行
head.setNext(reversetHead.getNext());
}
//反转打印链表 利用栈的先进后出特点 java自带的api Stack
public static void reversetPrint(HeroNode head) {
if(head.getNext()==null ){
return;
}
Stack stack=new Stack<HeroNode>();
HeroNode cur=head.getNext();
while (cur!=null){
stack.push(cur);
cur=cur.getNext();
}
while (stack.size()>0){
System.out.println("stack pop :"+ stack.pop());
}
}
三:双链表:
使用带head头的双向链表实现 –水浒英雄排行榜
管理单向链表的缺点分析: 单向链表,查找的方向只能是一个方向,而双向链 表可以向前或者向后查找。
单向链表不能自我删除,需要靠辅助节点 ,而双向 链表,则可以自我删除,所以单链表删除 时节点,总是找到temp,temp是待删除节点的前一 个节点(认真体会).
双向链表的增删改查:
package com.liuq.linkedlist;
public class DoubleLinkedListDemo {
public static void main(String [] args){
// 测试
System.out.println("DoubleLinkedListDemo Test");
// 先创建节点
HeroNode2 hero1 = new HeroNode2(1, "1", "11");
HeroNode2 hero2 = new HeroNode2(2, "2", "22");
HeroNode2 hero3 = new HeroNode2(3, "3", "33");
HeroNode2 hero4 = new HeroNode2(4, "4", "44");
// 创建一个双向链表
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(hero1);
doubleLinkedList.add(hero2);
doubleLinkedList.add(hero3);
doubleLinkedList.add(hero4);
doubleLinkedList.list();
doubleLinkedList.update(new HeroNode2(3,"33","333"));
doubleLinkedList.list();
}
}
// 创建一个双向链表的类
class DoubleLinkedList {
// 先初始化一个头节点, 头节点不要动, 不存放具体的数据
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
public void add(HeroNode2 node){
HeroNode2 temp= head;
while (true){
//null 时 指向了链表的最后
if(temp.next==null){
temp.next=node;
node.pre=temp;
node.next=null;
break;
}
temp=temp.next;
}
}
public void dele(HeroNode2 node){
HeroNode2 temp= head.next;
if(temp==null){
return;
}
while (true){
if(temp==null){
break;
}
if(temp.no==node.no){
temp.pre.next=temp.next;
//注意这里如果是最后一个节点 这里会报空指针
if (temp.next != null) {
temp.next.pre = temp.pre;
}
}
}
}
public void update(HeroNode2 node){
HeroNode2 temp= head.next;
if(temp==null){
return;
}
while (true){
if(temp==null){
break;
}
if(temp.no==node.no){
// node.next=temp.next;
// node.pre=temp.pre;
temp.name = node.name;
temp.nickname = node.nickname;
break;
}
temp=temp.next;
}
}
public void list(){
if(head.next==null){
return;
}
HeroNode2 temp=head;
while (temp!=null){
if(temp.next==null){
break;
}
System.out.println(temp.next.toString());
temp=temp.next;
}
}
}
// 定义HeroNode2 , 每个HeroNode 对象就是一个节点
class HeroNode2 {
public int no;
public String name;
public String nickname;
public HeroNode2 next; // 指向下一个节点, 默认为null
public HeroNode2 pre; // 指向前一个节点, 默认为null
// 构造器
public HeroNode2(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
// 为了显示方法,我们重新toString
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}