单链表的实现与CRUD
- 单链表的实现与增删改查
- 链表实现
- 结果: Hero{id=1, name='dog1', otherName='aadog1'} Hero{id=2, name='dog2', otherName='aadog2'} Hero{id=3, name='dog3', otherName='aadog3'} Hero{id=4, name='dog4', otherName='aadog4'}
- 准备插入的英雄编号1已经存在,不能加入 准备插入的英雄编号4已经存在,不能加入 准备插入的英雄编号3已经存在,不能加入 准备插入的英雄编号2已经存在,不能加入 Hero{id=1, name='dog1', otherName='aadog1'} Hero{id=2, name='dog2', otherName='aadog2'} Hero{id=3, name='dog3', otherName='aadog3'} Hero{id=4, name='dog4', otherName='aadog4'}
- Hero{id=1, name='dog1', otherName='aadog1'} Hero{id=2, name='bigdog2', otherName='dog2'} Hero{id=3, name='dog3', otherName='aadog3'} Hero{id=4, name='dog4', otherName='aadog4'}
- Hero{id=1, name='dog1', otherName='aadog1'} Hero{id=3, name='dog3', otherName='aadog3'} Hero{id=4, name='dog4', otherName='aadog4'}
@一只大狗头
单链表的实现与增删改查
单链表的实现
欢迎探讨【狗头】
链表实现
1.准备
class Hero{
public int id;
public String name;
public String otherName;
public Hero next;
public Hero(int id, String name, String otherName){
this.id = id;
this.name = name;
this.otherName = otherName;
}
@Override
public String toString() {
return "Hero{" +
"id=" + id +
", name='" + name + '\'' +
", otherName='" + otherName + '\'' +
'}';
}
}
2.增(不能实现随机输入还能顺序输出)
class SingleLinklist{
private Hero head = new Hero(0,"","");
//第一种增加
public void add(Hero hero){
Hero temp = head;
while (true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = hero;
}
}
.增(能实现随机输入还能顺序输出)
//第二种方式按顺序写入
public void addByorder(Hero hero){
Hero temp = head;
boolean flag = false;
while (true){
if(temp.next == null){
break;
}
if(temp.next.id > hero.id){
break;
}else if(temp.next.id == hero.id){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
System.out.printf("准备插入的英雄编号%d已经存在,不能加入\n",hero.id);
}else {
hero.next = temp.next;
temp.next = hero;
}
}
3.删
public void delete(int id){
Hero temp = head;
boolean bool = false;
while(true){
if(temp.next == null){
break;
}
if(temp.next.id == id){
bool = true;
break;
}
temp = temp.next;
}
if(bool){
temp.next = temp.next.next;
}else {
System.out.printf("要删除的%d节点不存在\n");
}
}
4.改
//更新
public void update(Hero newhero){
Hero temp = head.next;
boolean bool = false;
if(head.next == null){
System.out.println("链表为空");
return;
}
while (true){
if(temp == null){
break;
}
if(temp.id == newhero.id){
bool = true;
break;
}
temp = temp.next;
}
if(bool){
temp.name = newhero.name;
temp.otherName = newhero.otherName;
}else {
System.out.printf("没有找到该%d编号的信息",newhero.id);
}
}
5.查
//查询
public void seekByid(int id){
Hero temp = head;
boolean bool = false;
while (true){
if(temp.next == null){
break;
}
if(temp.next.id == id){
bool = true;
break;
}
temp = temp.next;
}
if(bool){
System.out.printf("要查询的编号为%d号的信息为:",id);
System.out.println(temp.next);
}else {
System.out.println("没有查到信息");
}
}
6.显示全部数据
//显示所有数据
public void list(){
if(head.next == null){
System.out.println("链表为空");
return;
}
Hero temp = head.next;
while (true){
if(temp == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
7.测试(因为懒选择手动调试):
public static void main(String[] args) {
Hero hero1 = new Hero(1, "dog1", "aadog1");
Hero hero2 = new Hero(2, "dog2", "aadog2");
Hero hero3 = new Hero(3, "dog3", "aadog3");
Hero hero4 = new Hero(4, "dog4", "aadog4");
//测试
SingleLinklist singleLinklist = new SingleLinklist();
//按顺序增加
singleLinklist.add(hero1);
singleLinklist.add(hero2);
singleLinklist.add(hero3);
singleLinklist.add(hero4);
singleLinklist.list();
System.out.println("----------------------");
//不按顺序增加
singleLinklist.addByorder(hero1);
singleLinklist.addByorder(hero4);
singleLinklist.addByorder(hero3);
singleLinklist.addByorder(hero2);
singleLinklist.list();
System.out.println("-----------------------");
//
//更新链表
Hero newhero2 = new Hero(2, "bigdog2", "dog2");
singleLinklist.update(newhero2);
singleLinklist.list();
System.out.println("--------------------------");
//
//删除链表
singleLinklist.delete(2);
singleLinklist.list();
//
//查找链表数据
System.out.println("--------------------------");
singleLinklist.seekByid(3);
}
结果:
Hero{id=1, name=‘dog1’, otherName=‘aadog1’}
Hero{id=2, name=‘dog2’, otherName=‘aadog2’}
Hero{id=3, name=‘dog3’, otherName=‘aadog3’}
Hero{id=4, name=‘dog4’, otherName=‘aadog4’}
准备插入的英雄编号1已经存在,不能加入
准备插入的英雄编号4已经存在,不能加入
准备插入的英雄编号3已经存在,不能加入
准备插入的英雄编号2已经存在,不能加入
Hero{id=1, name=‘dog1’, otherName=‘aadog1’}
Hero{id=2, name=‘dog2’, otherName=‘aadog2’}
Hero{id=3, name=‘dog3’, otherName=‘aadog3’}
Hero{id=4, name=‘dog4’, otherName=‘aadog4’}
Hero{id=1, name=‘dog1’, otherName=‘aadog1’}
Hero{id=2, name=‘bigdog2’, otherName=‘dog2’}
Hero{id=3, name=‘dog3’, otherName=‘aadog3’}
Hero{id=4, name=‘dog4’, otherName=‘aadog4’}
Hero{id=1, name=‘dog1’, otherName=‘aadog1’}
Hero{id=3, name=‘dog3’, otherName=‘aadog3’}
Hero{id=4, name=‘dog4’, otherName=‘aadog4’}
要查询的编号为3号的信息为:Hero{id=3, name=‘dog3’, otherName=‘aadog3’}
附录##完整代码:
/**
* @Author big dog
* Date on 2020/8/23 14:16
*/
public class Linklist {
public static void main(String[] args) {
Hero hero1 = new Hero(1, "dog1", "aadog1");
Hero hero2 = new Hero(2, "dog2", "aadog2");
Hero hero3 = new Hero(3, "dog3", "aadog3");
Hero hero4 = new Hero(4, "dog4", "aadog4");
//测试
SingleLinklist singleLinklist = new SingleLinklist();
//按顺序增加
singleLinklist.add(hero1);
singleLinklist.add(hero2);
singleLinklist.add(hero3);
singleLinklist.add(hero4);
singleLinklist.list();
System.out.println("----------------------");
//不按顺序增加
singleLinklist.addByorder(hero1);
singleLinklist.addByorder(hero4);
singleLinklist.addByorder(hero3);
singleLinklist.addByorder(hero2);
singleLinklist.list();
System.out.println("-----------------------");
//
//更新链表
Hero newhero2 = new Hero(2, "bigdog2", "dog2");
singleLinklist.update(newhero2);
singleLinklist.list();
System.out.println("--------------------------");
//
//删除链表
singleLinklist.delete(2);
singleLinklist.list();
//
//查找链表数据
System.out.println("--------------------------");
singleLinklist.seekByid(3);
}
}
class Hero{
public int id;
public String name;
public String otherName;
public Hero next;
public Hero(int id, String name, String otherName){
this.id = id;
this.name = name;
this.otherName = otherName;
}
@Override
public String toString() {
return "Hero{" +
"id=" + id +
", name='" + name + '\'' +
", otherName='" + otherName + '\'' +
'}';
}
}
class SingleLinklist{
private Hero head = new Hero(0,"","");
//第一种增加
public void add(Hero hero){
Hero temp = head;
while (true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = hero;
}
//第二种方式按顺序写入
public void addByorder(Hero hero){
Hero temp = head;
boolean flag = false;
while (true){
if(temp.next == null){
break;
}
if(temp.next.id > hero.id){
break;
}else if(temp.next.id == hero.id){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
System.out.printf("准备插入的英雄编号%d已经存在,不能加入\n",hero.id);
}else {
hero.next = temp.next;
temp.next = hero;
}
}
//更新
public void update(Hero newhero){
Hero temp = head.next;
boolean bool = false;
if(head.next == null){
System.out.println("链表为空");
return;
}
while (true){
if(temp == null){
break;
}
if(temp.id == newhero.id){
bool = true;
break;
}
temp = temp.next;
}
if(bool){
temp.name = newhero.name;
temp.otherName = newhero.otherName;
}else {
System.out.printf("没有找到该%d编号的信息",newhero.id);
}
}
//
//删除
public void delete(int id){
Hero temp = head;
boolean bool = false;
while(true){
if(temp.next == null){
break;
}
if(temp.next.id == id){
bool = true;
break;
}
temp = temp.next;
}
if(bool){
temp.next = temp.next.next;
}else {
System.out.printf("要删除的%d节点不存在\n");
}
}
//查询
public void seekByid(int id){
Hero temp = head;
boolean bool = false;
while (true){
if(temp.next == null){
break;
}
if(temp.next.id == id){
bool = true;
break;
}
temp = temp.next;
}
if(bool){
System.out.printf("要查询的编号为%d号的信息为:",id);
System.out.println(temp.next);
}else {
System.out.println("没有查到信息");
}
}
//显示所有数据
public void list(){
if(head.next == null){
System.out.println("链表为空");
return;
}
Hero temp = head.next;
while (true){
if(temp == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
}