单链表

/*
* 盒子类
* 其中box你可以理解为一个大盒子里面包裹着一个小盒子,或者将box理解为一个箭头
* 这里我更喜欢理解为那种礼品包装盒,一个大盒子里面包裹着一个小盒子,然后这个小盒子里面又包裹着一个小盒子,
* 如此循环往复下去(数据量足够多的情况下)
* */
public class Box {
    private int id;
    private String name;
    public Box box;//这里没赋值的时候是空的
    public Box(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Box{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", box=" + box +
                '}';
    }

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

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}
import java.util.Stack;
/*
* 方法类
* 这里面有几个面试题
* 分别是输出倒数第几个box,单链表反转,反向输出
* */
public class Manager {
    private Box header=new Box(0,"");

    public Box getHeader(){
        return header;
    }

    public void add(Box box){
        //把第一个盒子赋值给模板,这个temp就是第一个嘞,不是空
        Box temp=header;
        while (true){
            //判断temp后面有没有人,没有人就跳出循环
            if (temp.box==null){
                break;
            }
            temp=temp.box;
        }
        //把要添加的赋值给box,当做是一个尾巴
        temp.box=box;
    }

    public void printList(){
        //把header的尾巴赋值给temp
        Box temp=header.box;
        if (temp==null) return;
        while (true){
            if (temp==null){
                break;
            }
            System.out.println(temp);
            temp=temp.box;
        }
    }

    public void addByOrder(Box box){
        Box temp=header;
        boolean flag=false;
        while (true){
            if (temp.box==null){
                break;
            }
            if (temp.box.getId()>box.getId()){
                break;
            }else if(temp.box.getId()==box.getId()){
                flag=true;
                break;
            }
            //不得不说,这三个if和这个temp写的很精妙
            temp=temp.box;
        }
        if (flag){
            System.out.println("该id已经存在");
        }else {
            box.box=temp.box;
            temp.box=box;
        }
    }

    public void change(Box box){
        Box temp = header.box;
        boolean flag=false;
        while (true){
            if (temp==null){
                break;
            }else if (temp.getId()==box.getId()){
                flag=true;
                break;
            }
            temp=temp.box;
        }
        if (flag){
            temp.setName(box.getName());
        }
    }

    public void delete(int id){
        Box temp = header.box;
        boolean flag=false;
        while (true){
            if (temp==null){
                break;
            }else if (temp.getId()==id){
                flag=true;
                break;
            }
            temp=temp.box;
        }
        if (flag){
            temp.box=temp.box.box;
        }
    }

    public int getLength(Box header){
        Box cur = header.box;
        if (cur==null){
            return 0;
        }
        int len=0;
        while (cur!=null){
            len++;
            cur=cur.box;
        }
        return len;
    }

    public Box findLastBox(Box header,int index){
        Box cur = header.box;
        if (cur==null){
            return null;
        }
        int len=getLength(header);
        if (index>len||index<0){
            return null;
        }
        for (int i = 0; i < len - index; i++) {
            cur=cur.box;
        }
        return cur;
    }

    public void inVersion(Box one){//单链表的反转,腾讯面试题
        if (one.box==null||one.box.box==null){
            return;
        }
        Box newBox=new Box(0,"");
        Box cur=header.box;
        Box temp=null;
        while (cur!=null){
            temp=cur.box;
            cur.box=newBox.box;
            newBox.box=cur;
            cur=temp;
        }
        header.box=newBox.box;
    }

    public void reversePrint(Box box){
        Stack<Box> stack = new Stack<>();
        Box cur = box;
        if (cur==null){
            return;
        }
        while (cur!=null){
            stack.push(cur);
            cur=cur.box;
        }
        while (stack.size()>0){
            System.out.println(stack.pop());
        }
    }
}
/*
* 测试类
* */
public class Test {
    public static void main(String[] args) {
        Box one = new Box(1, "one");
        Box two = new Box(2, "two");
        Box three = new Box(3, "three");

        Manager manager = new Manager();
        manager.add(one);
        manager.add(two);
        manager.add(three);
    }
}
posted @ 2021-07-23 10:29  朱在春  阅读(37)  评论(1编辑  收藏  举报