数据结构 --java实现单链表

1) 链表是以节点的方式来存储, 是链式存储
2) 每个节点包含 data 域, next 域:指向下一个节点.
3) 链表的各个节点不一定是连续存储.
4) 链表分 带头节点的链表和 没有头节点的链表,根据实际的需求来确定

代码实现

package com.testlist;

/**
 * 数据结构-链表的实现
 */
public class LinkedListDemo {
    public static void main(String[] args) {

        MyLinkedList linkedList = new MyLinkedList();

        HeroNode node1 = new HeroNode(1, "李白", null);
        HeroNode node2 = new HeroNode(2, "李二", null);
        HeroNode node3 = new HeroNode(3, "李三", null);

        linkedList.add(node1);
        linkedList.add(node2);
        linkedList.add(node3);
        linkedList.list();
        //节点删除
        // linkedList.delete(3);
        linkedList.list();
        HeroNode updateNode = new HeroNode(2, "修改", null);
        linkedList.update(updateNode);
        linkedList.list();

    }


    /**
     * 定义链表类
     */
    static class MyLinkedList {

        /**
         * 定义一个头节点
         */
        private HeroNode head = new HeroNode(0, "", null);

        /**
         * 定义add方法
         */
        public void add(HeroNode node) {

            //temp是一个辅助节点,相当一一个指针,方便进行遍历
            HeroNode temp = head;
            while (true) {
                if (temp.next == null) {
                    temp.next = node;
                    break;
                } else {
                    temp = temp.next;
                }
            }
        }

        /**
         * 进行链表的遍历
         */
        public void list() {

            /**
             * 定义一个临时节点,相当一个指针,方便进行遍历
             */
            HeroNode temp = head;
            while (true) {
                //最后一个节点
                if (temp.next == null) {

                    //打印最后一个节点
                    System.out.println(temp);
                    break;
                } else {

                    System.out.println(temp);
                    temp = temp.next;
                }
            }

        }

        /**
         * 节点的删除
         */
        public void delete(int no) {

            //临时节点
            HeroNode temp = head;
            while (true) {
                if (temp.next == null) {
                    System.out.println("该node不存在,删除失败");
                    break;
                } else {
                    if (temp.next.no == no) {
                        temp.next = temp.next.next;
                        break;
                    }
                    temp = temp.next;
                }
            }
        }

        /**
         * 节点的修改
         */
        public void update(HeroNode node) {
            //临时节点
            HeroNode temp = head;
            while (true) {
                if (temp.next == null) {
                    System.out.println("该节点不存在,节点修改失败");
                    break;
                } else {
                    if (temp.next.no == node.no) {
                        temp.next.name = node.name;
                        break;
                    }
                    temp = temp.next;
                }
            }


        }

    }

    /**
     * 定义node节点信息
     */
    static class HeroNode {
        private int no;
        private String name;
        private HeroNode next;

        public HeroNode(int no, String name, HeroNode next) {
            this.no = no;
            this.name = name;
            this.next = next;
        }

        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public HeroNode getNext() {
            return next;
        }

        public void setNext(HeroNode next) {
            this.next = next;
        }

        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    "" +
                    '}';
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值