数据结构-单向链表练习


public class SingLinkedList2 {
    public static void main(String[] args) {
        HeroNode2 h1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 h2 = new HeroNode2(3, "林冲", "豹子头");
        HeroNode2 h3 = new HeroNode2(4, "吴用", "智多星");
        HeroNode2 h4 = new HeroNode2(2, "卢俊义", "玉麒麟");
        LinkList2 linkList = new LinkList2();
        linkList.add(h1);
        linkList.add(h2);
        linkList.add(h3);
        linkList.add(h4);
        linkList.List();


//        HeroNode2 newh4 = new HeroNode2(2, "武松", "禁军教头");
//        System.out.println("=========更新=============");
//        linkList.update(newh4);
//        linkList.List();

//        System.out.println("=========删除=============");
//        linkList.dele(2);
//        linkList.dele(3);
//        linkList.dele(1);
//        linkList.dele(4);
//        linkList.List();

//        System.out.println(linkList.dataNum());
//        linkList.dele(1);
//        System.out.println(linkList.dataNum());

//        System.out.println("====倒数第K个元素====");
//        HeroNode2 h= linkList.getDataByK(1);
//        System.out.println(h);

//        链表反转
//        System.out.println("======链表反转=======");
//        linkList.rollback(linkList.getHead());
//        linkList.List();

    }
}

//定义linkList
class LinkList2 {
    private HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead() {
        return head;
    }

    //    添加
    public void add(HeroNode2 hero) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.getNo() > hero.getNo()) {
                break;
            } else if (temp.next.getNo() == hero.getNo()) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("该数据已存在");

        } else {
            hero.next = temp.next;
            temp.next = hero;

        }

    }

    //    更新
    public void update(HeroNode2 h) {
        HeroNode2 temp = head;
        if (temp.next == null) {
            System.out.println("链表为空");
            return;
        }
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.getNo() == h.getNo()) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.setName(h.getName());
            temp.setNickname(h.getNickname());
            System.out.println("更新成功");
        } else {
            System.out.println("没有该编号,更新失败");
        }
    }

    //    删除
    public void dele(int no) {
        HeroNode2 temp = head;
        if (temp.next == null) {
            System.out.println("链表为空");
            return;
        }
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.getNo() == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {

            temp.next = temp.next.next;

        } else {
            System.out.println("删除失败");
        }

    }

    //    遍历
    public void List() {
        if (head.next == null) {
            System.out.println("链表为空");
        }
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;

        }


    }

    //    链表中有效数据的个数
    public int dataNum() {
        int index = 0;
        if (head.next == null) {
            return index;
        }
        HeroNode2 temp = head;
        while (true) {
            if (temp.next != null) {
                index++;
            } else {
                break;
            }
            temp = temp.next;
        }
        return index;
    }

    //查找单链表中倒数第k个节点
    /*
    1.链表有效数据总数
    2.位置 :总数 - k + 1;
     */
    public HeroNode2 getDataByK(int k) {
        HeroNode2 temp = head;
        if (k <= 0 || k > dataNum()) {
            throw new RuntimeException("输入越界");
        }
        int index = 0;
        while (true) {
            if (index == dataNum() - k + 1) {
                return temp;
            }
            index++;
            temp = temp.next;
        }
    }

    //    单链表反转
    /*
    1.新建临时链表 tempHead
    2.便利旧链表
    3.每遍历一个存放到新链表中  放在第一个节点的位置
     */
    public void rollback(HeroNode2 h) {
        HeroNode2 temphead = new HeroNode2(0, "", "");
        HeroNode2 temp = head.next;
        HeroNode2 next = null;

        if (temp == null || temp.next == null) {
            return;
        }
        while (temp != null) {
            next = temp.next;
            temp.next = temphead.next;
            temphead.next = temp;
            temp = next;

        }
        h.next = temphead.next;
    }

//    从尾到头打印单链表


}


//定义HeroNode,每个HeroNode对象都是一个节点
class HeroNode2 {
    public HeroNode2 next;
    private int no;
    private String name;
    private String nickname;

    public HeroNode2(int no, String name, String nickname) {
        this.name = name;
        this.nickname = nickname;
        this.no = no;
    }

    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 String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

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

                '}';
    }
}

 

posted @ 2023-02-02 22:22  java小铭  阅读(23)  评论(0)    收藏  举报