单链表
/*
* 盒子类
* 其中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);
}
}
这一路,灯火通明
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现