单向链表各种操作

package com.atguigu.linkedlist;

import java.util.Stack;

public class SingleLinkedList {
    public static void main(String[] args) {
        HeroNode heroNode1=new HeroNode(0,"2","2");
        HeroNode heroNode2=new HeroNode(3,"3","3");
        HeroNode heroNode3=new HeroNode(2,"4","4");
        HeroNode heroNode4=new HeroNode(1,"5","5");
        SingleLinkList singleLinkList=new SingleLinkList();
        singleLinkList.addByOrder(heroNode1);
        singleLinkList.addByOrder(heroNode2);
        singleLinkList.addByOrder(heroNode3);
        singleLinkList.addByOrder(heroNode4);
        singleLinkList.list();
        System.out.println("**************");
        reversePrint(singleLinkList.getHead());
        System.out.println("**************");
        HeroNode heroNode5=new HeroNode(3,"34","42");
        singleLinkList.update(heroNode5);
        singleLinkList.list();
        System.out.println("***********");
        singleLinkList.delete(heroNode5);
        singleLinkList.delete(heroNode1);
        singleLinkList.list();
        System.out.println(getLength(singleLinkList.getHead()));
        System.out.println("************");
        reverseList(singleLinkList.getHead());
        singleLinkList.list();
        HeroNode lastNode = findLastNode(singleLinkList.getHead(), 1);
        System.out.println(lastNode);
    }
    //链表反向输出
    public static void reversePrint(HeroNode heroNode){
        if(heroNode.next==null){
            return;
        }
        Stack<HeroNode> stack=new Stack<HeroNode>();
        HeroNode temp=heroNode.next;
        while (temp!=null){
            stack.push(temp);
            temp=temp.next;
        }
        while (stack.size()>0){
            System.out.println(stack.pop());
        }
    }
    //链表反转
    public static void reverseList(HeroNode heroNode){
        if(heroNode.next==null||heroNode.next.next==null){
            return;
        }
        HeroNode cur=heroNode.next;
        HeroNode next=null;
        HeroNode temp=new HeroNode(0,"","");
        while (cur!=null){
            next=cur.next;
            cur.next=temp.next;
            temp.next=cur;
            cur=next;
        }
        heroNode.next=temp.next;
    }
    //查询链表最后k个节点
    public static HeroNode findLastNode(HeroNode heroNode,int index){
        if(heroNode.next==null){
            return null;
        }
        int length=getLength(heroNode);
        if(index<0||index>length){
            return null;
        }
        HeroNode temp=heroNode.next;
        for(int i=0;i<length-index;i++){
            temp=temp.next;
        }
        return temp;
    }
    //获取链表长度
    public static int getLength(HeroNode heroNode){
        if(heroNode.next==null){
            return 0;
        }
        int length=0;
        HeroNode temp=heroNode;
        while (temp.next!=null){
//            if(){
//                break;
//            }
            length++;
            temp=temp.next;

        }
        return length;
    }
}
//定义链表
class SingleLinkList{
    //初始化一个头节点
    private HeroNode head=new HeroNode(0,"","");

    public HeroNode getHead() {
        return head;
    }

    //添加节点
    public void add(HeroNode heroNode){
        HeroNode temp=head;
        while(true){
            if(temp.next==null){
                break;
            }
            temp=temp.next;
        }
        temp.next=heroNode;
    }
    //按序添加节点
    public void addByOrder(HeroNode heroNode){
        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.println("英雄已经存在");
        }else {
            heroNode.next=temp.next;
            temp.next=heroNode;
        }
    }
    //修改节点
    public void update(HeroNode heroNode){
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=head;
        boolean flag=false;//是否找到该节点
        while (true){

            if(temp.no==heroNode.no){
                flag=true;
                break;
            }
            if(temp.next==null){
                break;
            }
            temp=temp.next;
        }
        if(flag){
            temp.name= heroNode.name;
            temp.nickName= head.nickName;
        }else{
            System.out.println("没有找到该英雄");
        }
    }
    //删除节点
    public void delete(HeroNode heroNode){
        HeroNode temp=head;
        boolean flag=false;
        while (true){
            if(temp.next==null){
                break;
            }
            if(temp.next.no==heroNode.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("链表为空");
        }
        HeroNode temp=head.next;
        while (true){
            System.out.println(temp);

            if(temp.next==null){
                break;
            }
            temp=temp.next;
        }
    }

}
//定义节点
class HeroNode{
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;
    public HeroNode(int hNo,String hName,String hNickName){
        this.no=hNo;
        this.name=hName;
        this.nickName=hNickName;
    }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值