介绍
通过类实现单链表。
Student类:单链表存储数据为学生类的对象,成员有名字、学号、性别、专业
Node类:节点
NotFoundException类:用于处理单链表各种异常情况
Link类:单链表类,方法有添加节点,删除节点,查看特定节点,查看所有节点,删除末尾结点,删除特定节点,修改指定节点
Test类:主方法实现
实现代码
Node类
public class Node {
// 学生对象
private Student student;
// 下个节点地址
Node next;
@Override
public String toString() {
return "学生信息:\n" +
"姓名'" + student.getName() + '\'' +
", 学号='" + student.getSno() + '\'' +
", 性别='" + student.getSex() + '\'' +
", 专业='" + student.getMajor() + '\'';
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
Link类
import java.util.Scanner;
public class Link {
// 头节点
Node header;
// 单链表元素个数
private int size;
// 构造方法
public Link(){
}
// 获取元素个数
public int size(){
return size;
}
// 添加元素
public void add(Node node){
if(header == null){
header = node;
size++;
}else{
Node node1 = header;
Node lastNode = findLastNode(node1);
lastNode.next = node;
node.next = null;
size++;
}
}
// 找到单链表中最后一个元素
private Node findLastNode(Node node) {
if(node.next == null){
return node;
}else{
return findLastNode(node.next);
}
}
// 删除末尾元素
public void remove() throws NotFoundException{
Node node = header;
Node pre = header;
if(node == null){
throw new NotFoundException("单链表暂无数据");
}
if(node.next == null){
header = null;
size--;
System.out.println("删除成功");
}
while(node.next != null){
if(node.next.next == null){
node.next = null;
size--;
System.out.println("删除成功");
}else{
node = node.next;
}
}
}
// 查找指定元素
public Node find(Student student) throws NotFoundException{
Node node = header;
if(node == null)
throw new NotFoundException("该单链表中无数据!");
while(node != null){
if(node.getStudent().equals(student))
return node;
else
node = node.next;
}
throw new NotFoundException("未查找到相关学生信息");
}
// 删除指定学生
public void remove(Student student) throws NotFoundException{
// 当前节点
Node node = header;
if(node == null)
throw new NotFoundException("删除失败,单链表无数据");
if(node.next == null && !node.getStudent().equals(student)){
throw new NotFoundException("删除失败,单链表无数据");
}else if(node.next == null){
header = null;
}
while(node.next != null){
if(node.next.getStudent().equals(student)){
this.size--;
node.next = node.next.next;
System.out.println("删除成功!");
break;
}else{
node = node.next;
}
}
throw new NotFoundException("删除失败,单链表无数据");
}
// 修改学生信息
public void modify(Student student) throws NotFoundException{
Node node = header;
if(node == null)
throw new NotFoundException("该单链表中无数据!");
while(node != null){
if(node.getStudent().equals(student)) {
System.out.println(node.getStudent());
Scanner s = new Scanner(System.in);
System.out.println("修改信息:");
System.out.print("学生姓名:");
node.getStudent().setName(s.next());
System.out.print("学生性别:");
node.getStudent().setSex(s.next());
System.out.print("学生学号:");
node.getStudent().setSno(s.next());
System.out.print("学生专业:");
node.getStudent().setMajor(s.next());
System.out.println("修改成功,新信息如下:");
System.out.println(node);
return;
}
else
node = node.next;
}
throw new NotFoundException("未查找到相关学生信息");
}
// 查看所有学生信息
public void allInformation() throws NotFoundException{
Node node = header;
if(node == null){
throw new NotFoundException("单链表暂无数据!");
}
while(node != null){
System.out.println(node);
node = node.next;
}
}
}
Student类
public class Student {
// 姓名
private String name;
// 学号
private String sno;
// 性别
private String sex;
// 专业
private String major;
public Student() {
}
// 构造方法
public Student(String name, String sno, String sex, String major) {
this.name = name;
this.sno = sno;
this.sex = sex;
this.major = major;
}
// setter and getter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof Student)) return false;
if(this == obj) return true;
Student s = (Student)obj;
return this.sno.equals(s.getSno());
}
}
Test类
public class Test {
public static void main(String[] args) {
Link stuLink = new Link();
Node s1 = new Node();
Node s2 = new Node();
Node s3 = new Node();
Node s4 = new Node();
Node s5 = new Node();
Node s6 = new Node();
Node s7 = new Node();
s1.setStudent(new Student("张三","02121","男","计算机"));
s2.setStudent(new Student("李四","02111","男","计算机"));
s3.setStudent(new Student("王五","02131","男","电子通信"));
s4.setStudent(new Student("夏琪","02141","女","计算机"));
s5.setStudent(new Student("吴去","02151","男","大数据"));
s6.setStudent(new Student("贾生","02161","男","计算机"));
stuLink.add(s1);
stuLink.add(s2);
stuLink.add(s3);
stuLink.add(s4);
stuLink.add(s5);
stuLink.add(s6);
// 显示全部学生信息
stuLink.allInformation();
// 查找测试
try {
System.out.println(stuLink.find(s2.getStudent()));
} catch (NotFoundException e) {
System.out.println(e.getMessage());
}
// 删除指定测试
try{
stuLink.remove(s3.getStudent());
}catch(NotFoundException e){
System.out.println(e.getMessage());
}
try {
stuLink.allInformation();
} catch (NotFoundException e) {
System.out.println(e.getMessage());
}
System.out.println(stuLink.size());
try {
System.out.println(stuLink.find(s3.getStudent()));
} catch (NotFoundException e) {
System.out.println(e.getMessage());
}
//修改测试
try {
stuLink.modify(s3.getStudent());
} catch (NotFoundException e) {
System.out.println(e.getMessage());
}
try {
System.out.println(stuLink.find(s3.getStudent()));
} catch (NotFoundException e) {
System.out.println(e.getMessage());
}
// 删除尾结点测试
try {
stuLink.remove();
} catch (NotFoundException e) {
System.out.println(e.getMessage());
}
try {
stuLink.allInformation();
} catch (NotFoundException e) {
System.out.println(e.getMessage());
}
}
}
NotFoundException类
public class NotFoundException extends Exception{
public NotFoundException() {
}
public NotFoundException(String message) {
super(message);
}
}