单链表 (英雄节点)

package itheima_shuju02_danlianbiao;


import java.util.Scanner;

public class HeroNode {
    public static void main(String[] args) {
        ControlLink control = new ControlLink();
        Scanner input = new Scanner(System.in);
        while(true){
            System.out.println("添加英雄--->输入a(addhero)");
            System.out.println("显示英雄--->输入s(showhero)");
            String N = input.next();
            switch (N){
                case "a":
                    control.addHero(control.getHero());
                    break;
                case "s":
                    control.showHero(control.getHero());
                    break;
                default:
                    System.out.printf("退出系统!");
                    return;
            }
        }
    }
}
class ControlLink{
    private Hero hero = new Hero(0,"","");
    Hero temp = hero;

    public Hero getHero() {
        return hero;
    }

    public Hero addHero(Hero hero){
        while(true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        Scanner input = new Scanner(System.in);
        System.out.printf("请输入英雄编号:");
        int number = input.nextInt();
        System.out.printf("请输入英雄姓名:");
        String name = input.next();
        System.out.printf("请输入英雄绰号:");
        String nickname = input.next();
        temp.next = new Hero(number,name,nickname);
        return temp;
    }
    public void showHero(Hero hero){
        hero = hero.next;
        while (true){
            if (hero == null){
                break;
            }
            System.out.println(hero.toString());
            hero =hero.next;
        }
    }
}

class Hero{
    int number;
    String name;
    String nickname;
    Hero next;

    public Hero(int number, String name, String nickname) {
        this.number = number;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "number=" + number +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值