广义表

本科生导师制问题

一、问题描述:

在高校的教学改革中,有很多学校实行了本科生导师制。一个班级的学生被分给几个老师,每个老师带领 n 个学生,如果老师还带研究生,那么研究生也可直接负责本科生。
本科生导师制问题中的数据元素具有如下形式:
⑴导师带研究生:(老师,((研究生 1,(本科生 1,…,本科生 m)),…))
⑵导师不带研究生:(老师,(本科生 1,…,本科生 m))
导师的自然情况只包括姓名、职称;
研究生的自然情况只包括姓名、班级;
本科生的自然情况只包括姓名、班级。

二、功能要求:

要求完成以下功能:
⑴插入:将某位本科生或研究生插入到广义表的相应位置;
⑵删除:将某本科生或研究生从广义表中删除;
⑶查询:查询导师、本科生(研究生)的情况;
⑷统计:某导师带了多少个研究生和本科生;
⑸输出:将某导师所带学生情况输出。

三、算法描述以及实现:

广义表头尾链表实现。

点击查看代码
//Lists.java
import java.util.Objects;

/**
 * @author dongyudeng
 */
public class Lists {
    Node head;

    public Lists() {
        head = new Node("Head", "", "");
    }

    public Node find(Node now, String targetName) {
        if (now.name.equals(targetName)) return now;
        if (now.next != null) {
            Node result = find(now.next, targetName);
            if (result != null) return result;
        }
        if (now.down != null) {
            Node result = find(now.down, targetName);
            if (result != null) return result;
        }
        return null;
    }

    public Node find(String targetName) {
        return find(head, targetName);
    }

    public void insert(String targetName, Node now) {
        //是导师
        if (Objects.equals(targetName, "")) {
            Node node = head;
            while (node.next != null) node = node.next;
            node.next = now;
            now.pre = node;
        } else {
            Node target = find(targetName);
            if (target == null) throw new RuntimeException("Target not found");
            now.next = target.down;
            now.up = target;
            target.down.pre = now;
            target.down = now;
        }
    }

    public void remove(String targetName) {
        Node target = find(head, targetName);
        switch (target.nodeType) {
            case UNDERGRADUATE -> {
                target.next.up = target.up;
                target.next.pre = null;
                target.up.down = target.next;
            }
            case POSTGRADUATE -> {
                if (target.down == null) {
                    target.next.up = target.up;
                    target.next.pre = null;
                    target.up.down = target.next;
                } else {
                    target.up.down = target.down;
                    target.down.up = target.up;
                    Node downNode = target.down;
                    while (downNode.next != null) downNode = downNode.next;
                    target.next.pre = downNode;
                    downNode.next = target.next;
                }
            }
            default -> throw new RuntimeException("Wrong type");
        }
    }

    public int count(String name) {
        Node target = find(name), now = target.down;
        int cnt = 0;
        while (now != null) {
            cnt++;
            now = now.next;
        }
        return cnt;
    }

    public void print(String name) {
        Node target = find(name), now = target.down;
        while (now != null) {
            System.out.println(now);
            now = now.next;
        }
    }
}

class Node {
    NodeType nodeType;
    String name, status;
    Node pre, next, up, down;

    public Node(String nodeType, String name, String status) {
        switch (nodeType) {
            case "Head" -> this.nodeType = NodeType.HEAD;
            case "Tutor" -> this.nodeType = NodeType.TUTOR;
            case "Postgraduate" -> this.nodeType = NodeType.POSTGRADUATE;
            case "Undergraduate" -> this.nodeType = NodeType.UNDERGRADUATE;
            default -> throw new RuntimeException("Unknown type");
        }
        this.name = name;
        this.status = status;
        this.pre = this.next = this.up = this.down = null;
    }

    @Override
    public String toString() {
        return "Type=" + nodeType +
                ", name='" + name + '\'' +
                ", status='" + status;
    }
}

enum NodeType {
    HEAD, TUTOR, POSTGRADUATE, UNDERGRADUATE
}
点击查看代码
//Main.java
import java.util.Scanner;

/**
 * @author dongyudeng
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Lists lists = new Lists();
        while (true) {
            int operation = scanner.nextInt();
            switch (operation) {
                //1.创建一名导师(targetName为空字符串),将某位本科生或研究生划归某位导师(对于本科生还可以是研究生)
                case 1 -> {
                    String type = scanner.next(), name = scanner.next(), status = scanner.next();
                    Node now = new Node(type, name, status);
                    String targetName = scanner.next();
                    lists.insert(targetName, now);
                }
                //将某本科生或研究生从广义表中删除
                case 2 -> {
                    String name = scanner.next();
                    lists.remove(name);
                }
                //查询导师、本科生(研究生)的情况;
                case 3 -> {
                    String name = scanner.next();
                    Node now = lists.find(name);
                    if (now == null) System.out.println("Not found");
                    else System.out.println(now);
                }
                //某导师带了多少个研究生和本科生
                case 4 -> {
                    String name = scanner.next();
                    System.out.println(lists.count(name));
                }
                //将某导师所带学生情况输出
                case 5 -> {
                    String name = scanner.next();
                    lists.print(name);
                }
                default -> {

                }
            }
        }
    }
}

posted @ 2022-06-04 22:58  nofind  阅读(82)  评论(0编辑  收藏  举报