数据结构--单链表(Java)

数据结构–单链表(Java)

博客说明

文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!

简介

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。

节点结构

在这里插入图片描述

单链表的头指针head和终端结点

单链表中每个结点的存储地址是存放在其前趋结点next域中,而开始结点无前趋,故应设头指针head指向开始结点。链表由头指针唯一确定,单链表可以用头指针的名字来命名。

终端结点无后继,故终端结点的指针域为空,即NULL。

图解

在这里插入图片描述

代码实现

package link;

import java.util.Stack;

public class LinkList {
    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(3,"吴用hh","智多星hh");

        //创建一个链表
        SingleLinkList singleLinkList = new SingleLinkList();
        //添加
        singleLinkList.addByOrder(hero3);
        singleLinkList.addByOrder(hero1);
        singleLinkList.addByOrder(hero2);

        //修改
//        singleLinkList.update(hero4);

        //删除
//        singleLinkList.delete(1);

        //显示
        singleLinkList.list();

        //有效个数
//        System.out.println(getLength(singleLinkList.getHead()));

        //获取倒数第k个节点的数据
//        HeroNode res = findLastK(singleLinkList.getHead(),1);
//        System.out.println("res="+res);

        //反转
//        System.out.println("反转");
//        reverseList(singleLinkList.getHead());
//        singleLinkList.list();
        System.out.println("逆序打印");
        reversePrint(singleLinkList.getHead());
    }

    /**
     * @param head 链表的头结点
     * @return 有效个数
     */
    public static int getLength(HeroNode head){
        if (head.next == null){
            return 0;
        }
        int length = 0;
        HeroNode temp = head.next;
        while (temp != null){
            length++;
            temp = temp.next;
        }
        return length;
    }

    public static HeroNode findLastK(HeroNode head,int index){
        if (head.next == null){
            return null;
        }
        int size = getLength(head);
        if (index <= 0 || index > size){
            return null;
        }
        HeroNode temp = head.next;
        for (int i=0;i<size-index;i++){
            temp = temp.next;
        }
        return temp;
    }

    public static void reverseList(HeroNode head){
        if (head.next == null || head.next.next == null){
            return;
        }
        HeroNode temp = head.next;
        HeroNode next = null;//指向当前节点temp的下一个节点
        HeroNode reverseHead = new HeroNode(0,"",""); //new一个新的链表的头结点
        //遍历
        while (temp != null){
            next = temp.next;
            temp.next = reverseHead.next;//将temp的下一个节点指向新链表的头结点的下一个节点(最前端)
            reverseHead.next = temp;//连接到新的链表上
            temp = next;
        }
        //还回
        head.next = reverseHead.next;
    }

    public static void reversePrint(HeroNode head){
        if (head.next == null){
            return;
        }
        //创建栈
        Stack<HeroNode> stack = new Stack<HeroNode>();
        HeroNode temp = head.next;
        while (temp != null){
            stack.push(temp);  //入栈
            temp = temp.next;
        }
        //出栈
        while (stack.size() > 0){
            System.out.println(stack.pop());
        }
    }


}

class SingleLinkList{
    //初始化一个头结点
    private HeroNode head = new HeroNode(0,"","");

    //返回头结点
    public HeroNode getHead(){
        return head;
    }

    //添加一个节点到单链表
    //找到最后一个节点的位置
    //将最后节点的next指向新的节点
    public void add(HeroNode heroNode){
        //辅助节点temp
        HeroNode temp = head;
        while (true){
            if(temp.next == null){
                break;
            }
            temp = temp.next;
        }
        //当循环结束时,temp指向链表的最后
        temp.next = heroNode;
    }

    //插入
    public void addByOrder(HeroNode heroNode){
        //辅助temp
        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 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 update(HeroNode heroNode){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        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的节点,不能修改",heroNode.no);
        }
    }

    //显示链表
    public void list(){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        //辅助节点temp
        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;
    }

    //重写toString方法
    public String toString(){
        return "HeroNode [ no = "+ no +",name = "+ name +",nickName = "+ nickName +"]";
    }
}

感谢

百度百科

万能的网络

以及勤劳的自己

发布了177 篇原创文章 · 获赞 456 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_45163122/article/details/105011781