欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现单链表基础操作

程序员文章站 2022-06-15 13:55:07
关于链表链表是有序的列表链表是以节点的方式来存储每个节点包含data域,next域(指向下一个节点)分带头节点的链表和没有头节点的链表定义一个节点:package linkedqueue;public...

关于链表

链表是有序的列表链表是以节点的方式来存储每个节点包含data域,next域(指向下一个节点)分带头节点的链表和没有头节点的链表

Java实现单链表基础操作

定义一个节点:

package linkedqueue;

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 + '\'' +
                '}';
    }


}

定义一个链表:

实现以下功能:

添加节点

按序添加节点

删除节点

修改节点

遍历节点

反向打印节点

链表反转

统计节点个数

打印倒数第n个节点

程序:

package linkedqueue;


import java.util.stack;

public class singlelinkedlist {
    private heronode head = new heronode(0, "", "");

    public heronode gethead() {
        return head;
    }

    //反向打印节点
    public static void reverseprint(heronode head) {
        if (head.next == null) {
            return;
        }
    //    创建一个栈
        stack<heronode> heronodes = new stack<>();
        heronode cur = head.next;
        while (cur != null) {
            heronodes.push(cur); //入栈
            cur = cur.next;
        }
    //    出栈打印
        while (heronodes.size() > 0) {
            system.out.println(heronodes.pop());
        }
    }

    /**
     * 添加节点
     * @param heronode
     */
    public void add(heronode heronode) {
        heronode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heronode;
    }

    /**
     * 有序添加节点
     * @param hernode
     */
    public void addbyorder(heronode hernode) {
        heronode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > hernode.no) {
                break;
            } else if (temp.next.no==hernode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            system.out.println("你要插入的节点的编号已经存在!!!!");
        } else {
            hernode.next = temp.next;
            temp.next = hernode;
        }
    }

    /**
     * 更新节点数据
     * @param newheronode
     */
    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.println("没有找到编号" + newheronode.no + "的节点,不能修改");
        }

    }

    /**
     * 刪除节点
     * @param no
     */
    public void del(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;
        }

    }

    /**
     * 遍历节点
     */
    public void showlist() {

        if (head.next == null) {
            system.out.println("链表为空");
            return;
        }

        heronode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            system.out.println(temp);
            temp = temp.next;
        }

    }

    /**
     * 统计有效节点的个数
     */
    public static int getlength(heronode head) {
        if (head.next == null) { //空链表
            return 0;
        }
        int length = 0;
        heronode hernode = head.next;
        while (hernode != null) {
            length++; // 计数
            hernode=hernode.next; // 指针后移
        }
        return length;
    }

    /**
     * 求倒数第index个节点
     * @param head 头节点
     * @param index 倒数第k个
     * @return
     */
    public static heronode findlastindexnode(heronode head, int index) {
        // 判断是否是空链表
        if (head.next == null) {
            return null;
        }

        // 拿到链表的长度
        int size = getlength(head);
        // index校验,看是否在范围内
        if (index <= 0 || index > size) {
            return null;
        }
        // 定位倒数第index个节点
        heronode hernode = head.next;
        for (int i = 0; i < size - index; i++) {
            hernode = hernode.next;
        }
        return hernode;

    }

    //单链表反转
    public static void reverselist(heronode head) {
    //    如果当前链表为空或者只有一个节点,直接返回
        if (head.next == null || head.next.next == null) {
            return;
        }

    //    定义辅助变量来遍历链表
        heronode cur = head.next;
        heronode next = null;//指向当前节点[cur]的下一个节点
        heronode reversehead = new heronode(0, "", "");
    //    遍历原来节点,每遍历一个节点,将其取出,并放在新的链表的最前端
        while (cur != null) {
            next = cur.next;//暂时保存当前节点的下一个节点
            cur.next = reversehead.next;//将cur的下一个节点指向新的链表的最前端
            reversehead.next=cur;//将cur链接到新的链表
            cur = next;
        }
        head.next = reversehead.next;

    }

}

到此这篇关于java实现单链表基础操作的文章就介绍到这了,更多相关java单链表内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: java 单链表