import java.util.*;
//学生类
class Stu
{
private int math;//学生的学号
private String name;//学生的名字
public Stu(int math, String name)
{
this.math = math;
this.name = name;
}
//获得学生的名字
public String getName() {
return name;
}
//修改学生的分数
public void setMath(int math) {
this.math = math;
}
//重写toString方法
public String toString()
{
return name + ": " + math;
}
}
//链表节点类
class Point
{
Stu st = null; //链表数据域
Point next = null; //链表指针域
public Point(Stu st)
{
this(st, null); //调用另一个构造方法
}
public Point(Stu st, Point next)
{
this.st = st;
this.next = next;
}
}
//建立链表类,并实现其查找,添加,修改,删除,打印的功能
class MyList
{
private Point head = null; //表头
private Point tail = null; //表尾
Scanner read = new Scanner(System.in);
//构造方法,默认表头和表尾都为空
public MyList()
{
this.head = null;
this.tail = null;
}
//判断链表是否为空
public boolean isEmpty()
{
return head == null;
}
//建立表头
public void addHead(Stu st)
{
head = new Point(st, head);
//判断表尾是否为空,这里的主要作用是让表尾指向表头
if(null == tail)
{
tail = head;
}
}
//建立表尾
public void addTail(Stu st)
{
//判断表头是否为空,为空则先建立表头
if(isEmpty()) //调用判断判断链表是否为空的方法
{
this.addHead(st);//调用建立表头的方法
}
else
{
Point temp = new Point(st);
tail.next = temp;
tail = tail.next;
}
}
//按学生名字,实现查找功能
public void find(String name)
{
System.out.println("/n/n/n查询结果为:");
if(isEmpty())
{
System.out.println("链表为空");
}
else
{
//遍历查找法
for(Point temp = head; temp != null; temp=temp.next)
{
if(name.equals(temp.st.getName()))
{
System.out.println(temp.st.toString());
}
}
}
}
//根据学生的名字,修改其成绩
public void amend(String name)
{
int amendStMath = 0;
if(isEmpty())
{
System.out.println("链表为空");
}
else
{
//遍历查找法
for(Point temp = head; temp != null; temp=temp.next)
{
if(name.equals(temp.st.getName()))
{
System.out.print("请输入" + name + "的分数:");
amendStMath = read.nextInt();
temp.st.setMath(amendStMath);
}
}
}
}
//打印出链表
public void disMyList()
{
for(Point temp = head; temp != null; temp=temp.next)
{
System.out.println(temp.st.toString());
}
}
}
public class Link {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] math = {90, 80, 89, 87, 95};
String[] name = {"院长", "华女", "san女", "舍长仔", "阿金"};
Stu st = null;
MyList list = new MyList();
//为链表添加节点
for(int i = 0; i < math.length; i++)
{
st = new Stu(math[i], name[i]);
list.addTail(st);
}
//打印链表
System.out.println("建立的链表为:");
list.disMyList();
//查找名字为华女的学生的信息
list.find("华女");
//修改名字为华女的学生的信息
list.amend("华女");
System.out.println("修改后的学生信息为:");
//打印链表
list.disMyList();
}
}