代码的作用是创建了一个链表,采用头插法插入节点,每个节点内保存的是超级英雄的名字和排名,实现了简单的增删改查显五个内容。
20/4/5修改一次,将链表抽象化为一个类,增加了尾插法和有序插法。
package begin;
import java.util.Scanner;
class heronode {
int no; //存储排名
String name; //存储英雄名字
heronode next;
heronode(){}
//创建节点用
heronode(int no, String name) {
this.no = no;
this.name = name;
}
//在删除节点的时候用了一次
String tostring(){
return "排名"+no+"的英雄" + name;
}
}
class linklist{
heronode L=new heronode();
heronode tail=L;
linklist(){
L.next=null;
}
//头插法
void addhead(int i, String name) {
heronode q = new heronode(i, name);
q.next=L.next;
L.next = q;
}
//尾插法
void addtail(int i, String name){
heronode q = new heronode(i, name);
q.next=tail.next;
tail.next = q;
tail=q;
}
//排序插法,根据排名
void addsort(int i, String name){
heronode q = new heronode(i, name);
heronode w;
w=L;
while (w.next!=null){
if(w.next.no<i)
w=w.next;
else
break;
}
q.next=w.next;
w.next=q;
}
//根据排名删除
String noadd(int i){
heronode q;
q=L;
while(q.next.no!=i)
q=q.next;
String lang=q.next.tostring();
q.next=q.next.next;
return lang;
}
//根据名字删除
String noadd(String i){
heronode q;
q=L;
while(!q.next.name.equals(i))
q=q.next;
String lang=q.next.tostring();
q.next=q.next.next;
return lang;
}
//修改
void reio(int oldi,int yongi){
heronode q;
q=L.next;
while (q.no!=oldi)
q=q.next;
q.no=yongi;
System.out.println("修改成功");
}
//修改
void reio(String oldname,String yongname){
heronode q;
q=L.next;
while (!q.name.equals(oldname))
q=q.next;
q.name=yongname;
System.out.println("修改成功");
}
//查找位置
int find(int i){
heronode q;
q=L.next;
int ii=1;
while (q.no!=i){
if(q.next==null)
return 0;
q=q.next;
ii++;
}
return ii;
}
//根据名字查找
int find(String namea){
heronode q;
q=L.next;
int ii=1;
while (!q.name.equals(namea)){
if(q.next==null)
return 0;
q=q.next;
ii++;
}
return ii;
}
void appear() {
heronode i;
i = L.next;
if(i==null) {
System.out.println("链表为空");
return;
}
while (i != null) {
System.out.println("英雄:"+i.name+"的排名为:" + i.no);
i = i.next;
}
}
}
public class link {
public static void main(String[] args) {
String name;int io; //辅助存储名字和排名
linklist L=new linklist();
System.out.println("1增加节点\n2删除节点\n3修改节点\n4查找\n5显示");
System.out.println("请输入操作数");
Scanner shou=new Scanner(System.in); //开始循环,选择各种操作
int ruler=shou.nextInt();
while(true){
if (ruler == 1){
System.out.println("请输入您要插入的英雄名字和排名");
name=shou.next();
io=shou.nextInt();
L.addsort(io,name); //头插法插入
}
else if(ruler==2){
System.out.println("请输入您要删除的英雄的排名或者名字");
if(shou.hasNextInt()) { //判断输入的是名字还是排名
ruler = shou.nextInt();
if(L.find(ruler)!=0) //判断链表是否包含这个元素
System.out.println("删除的为:"+L.noadd(ruler));
else
System.out.println("排名为"+ ruler +"的英雄不在连表内");
}
else{
name=shou.next();
System.out.println("删除的名字为:"+name);
if(L.find(name)!=0) //判断链表是否包含这个元素
System.out.println("删除的为:"+L.noadd(name));
else
System.out.println("英雄"+name+"不在链表内");
}
}
else if(ruler==3){
System.out.println("请输入要修改的名字或者排名");
if(shou.hasNextInt()){ //判断输入的是名字还是排名
ruler=shou.nextInt();
if(L.find(ruler)!=0){ //判断链表是否包含这个元素
int w;
System.out.println("请输入新排名:");
w=shou.nextInt();
L.reio(ruler,w);
}
else
System.out.println("没有这个排名");
}
else{
name=shou.next();
if(L.find(name)!=0){ //判断链表是否包含这个元素
String w;
System.out.println("请输入新名字");
w=shou.next();
L.reio(name,w);
}
else
System.out.println("没有这个名字");
}
}
else if(ruler==4){
System.out.println("请输入要查询的名字或者排名");
if(shou.hasNextInt()){ //判断输入的是名字还是排名
ruler=shou.nextInt();
System.out.println("排名为"+ruler+"的节点在第"+L.find(ruler)+"个");
}
else{
name=shou.next();
System.out.println("排名为"+name+"的节点在第"+L.find(name)+"个");
}
}
else if(ruler==5)
L.appear();
else
break;
System.out.println("请输入操作数");
ruler=shou.nextInt();
}
shou.close();
System.out.println("Bye Bye");
}
}
第一代代码
package begin;
import java.util.Scanner;
class heronode {
int no; //存储排名
String name; //存储英雄名字
heronode next;
//空白构造方法,创建辅助指针时候用
heronode() {
}
//创建节点用
heronode(int no, String name) {
this.no = no;
this.name = name;
}
//在删除节点的时候用了一次
String tostring(){
return "排名"+no+"的英雄" + name;
}
}
public class link {
public static void main(String[] args) {
String name;int io; //辅助存储名字和排名
heronode L=new heronode();
L.next = null;
System.out.println("1增加节点\n2删除节点\n3修改节点\n4查找\n5显示");
System.out.println("请输入操作数");
Scanner shou=new Scanner(System.in); //开始循环,选择各种操作
int ruler=shou.nextInt();
while(true){
if (ruler == 1){
System.out.println("请输入您要插入的英雄名字和排名");
name=shou.next();
io=shou.nextInt();
add(L,io,name); //头插法插入
}
else if(ruler==2){
System.out.println("请输入您要删除的英雄的排名或者名字");
if(shou.hasNextInt()) { //判断输入的是名字还是排名
ruler = shou.nextInt();
if(find(L,ruler)!=0) //判断链表是否包含这个元素
System.out.println("删除的为:"+noadd(L,ruler));
else
System.out.println("排名为"+ ruler +"的英雄不在连表内");
}
else{
name=shou.next();
System.out.println("删除的名字为:"+name);
if(find(L,name)!=0) //判断链表是否包含这个元素
System.out.println("删除的为:"+noadd(L,name));
else
System.out.println("英雄"+name+"不在链表内");
}
}
else if(ruler==3){
System.out.println("请输入要修改的名字或者排名");
if(shou.hasNextInt()){ //判断输入的是名字还是排名
ruler=shou.nextInt();
if(find(L,ruler)!=0){ //判断链表是否包含这个元素
int w;
System.out.println("请输入新排名:");
w=shou.nextInt();
reio(L,ruler,w);
}
else
System.out.println("没有这个排名");
}
else{
name=shou.next();
if(find(L,name)!=0){ //判断链表是否包含这个元素
String w;
System.out.println("请输入新名字");
w=shou.next();
reio(L,name,w);
}
else
System.out.println("没有这个名字");
}
}
else if(ruler==4){
System.out.println("请输入要查询的名字或者排名");
if(shou.hasNextInt()){ //判断输入的是名字还是排名
ruler=shou.nextInt();
System.out.println("排名为"+ruler+"的节点在第"+find(L,ruler)+"个");
}
else{
name=shou.next();
System.out.println("排名为"+name+"的节点在第"+find(L,name)+"个");
}
}
else if(ruler==5)
appear(L);
else
break;
System.out.println("请输入操作数");
ruler=shou.nextInt();
}
shou.close();
System.out.println("Bye Bye");
}
//增加
static void add(heronode L, int i, String name) {
heronode q = new heronode(i, name);
q.next=L.next;
L.next = q;
}
//根据排名删除
static String noadd(heronode L,int i){
heronode q;
q=L;
while(q.next.no!=i)
q=q.next;
String lang=q.next.tostring();
q.next=q.next.next;
return lang;
}
//根据名字删除
static String noadd(heronode L,String i){
heronode q;
q=L;
while(!q.next.name.equals(i))
q=q.next;
String lang=q.next.tostring();
q.next=q.next.next;
return lang;
}
//修改
static void reio(heronode L,int oldi,int yongi){
heronode q;
q=L.next;
while (q.no!=oldi)
q=q.next;
q.no=yongi;
System.out.println("修改成功");
}
//修改
static void reio(heronode L,String oldname,String yongname){
heronode q;
q=L.next;
while (!q.name.equals(oldname))
q=q.next;
q.name=yongname;
System.out.println("修改成功");
}
//查找位置
static int find(heronode L,int i){
heronode q;
q=L.next;
int ii=1;
while (q.no!=i){
if(q.next==null)
return 0;
q=q.next;
ii++;
}
return ii;
}
//根据名字查找
static int find(heronode L,String namea){
heronode q;
q=L.next;
int ii=1;
while (!q.name.equals(namea)){
if(q.next==null)
return 0;
q=q.next;
ii++;
}
return ii;
}
static void appear(heronode L) {
heronode i;
i = L.next;
while (i != null) {
System.out.println("英雄:"+i.name+"的排名为:" + i.no);
i = i.next;
}
}
}