单链表的实现((教职工管理系统)

单链表的实现((教职工管理系统)

package e_1;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *  数据结构-实验1
 * 利用链表实现一个简单的学校教职工信息管理系统:
 * (1)教职工信息包括工号、姓名、性别、职称、学位、职务、毕业学校和所学专业等;
 * (2)系统的主要功能包括:教职工信息的创建、输出教职工信息、查询教职工信息、增加教职工信息、删除教职工信息。
 */
class Teach {
    String sno;//工号
    String name;//姓名
    String sex;//性别
    String tit;//职称
    String deg;//学位
    String pos;//职务
    String scho;//毕业学校
    String pro;//所学专业

    public Teach(String sno, String name, String sex, String tit, String deg,
                 String pos, String scho, String pro) {
        super();
        this.sno = sno;
        this.name = name;
        this.sex = sex;
        this.tit = tit;
        this.deg = deg;
        this.pos = pos;
        this.scho = scho;
        this.pro = pro;
    }

    //为了显示方便,重写toString()方法
    public String toString(Teach t) {
        return t.sno + "," + t.name + "," + t.sex + "," + t.tit + ","
                + t.deg + "," + t.pos + "," + t.scho + "," + t.pro;
    }
}

/**
 * 链表节点
 */
class Node {
    Teach tea;  //data域
    Node next;  //next域
}

class Main {
    private static void menu() {
        System.out.println("-------------学校教职工信息管理系统-----------");
        System.out.println(" 1.查询教职工");
        System.out.println(" 2.显示所有教职工");
        System.out.println(" 3.增加教职工");
        System.out.println(" 4.删除教职工");
        System.out.println(" 5.修改教职工");
        System.out.println(" 6.导出教职工信息到文件");
        System.out.println(" 0.退出");
        System.out.println("----------------------------------------");
    }

    //入口
    public static void main(String[] args) {
        Node head = new Node();
        Node current = head;    //现在的
        Scanner scn = new Scanner(System.in);
        String cmd = "";    //存放用户输入
        menu();
        do {
            System.out.print("\n-cdm-: 请输入你得选择:(0-6)> ");
            cmd = scn.nextLine();   //用户输入
            if (cmd.equals("1")) {
                System.out.print("> 请输入要查询的学号:");
                String sno = scn.nextLine();
                Node tmp = head.next;   //从头指针head出发,指向链表的第一个结点付给tmp
                while (tmp != null &&   //遍历
                        !tmp.tea.sno.equals(sno)) {
                    tmp = tmp.next;
                }
                if (tmp != null) {
                    Teach t = tmp.tea;
                    System.out.println(t.toString(t));
                    tmp = tmp.next;
                } else {
                    System.out.println("error!! 查询失败!");//
                }

            } else if (cmd.equals("2")) {
                Node tmp = head.next;
                if (tmp == null) System.out.println("链表为空");//head.next == null表示链表为空
                while (tmp != null) {
                    Teach t = tmp.tea;
                    System.out.println(t.toString(t));
                    tmp = tmp.next;
                }
            } else if (cmd.equals("3")) {
                System.out.print("> 请输入工号: ");
                String sno = scn.nextLine();
                System.out.print("> 请输入姓名: ");
                String name = scn.nextLine();
                System.out.print("> 请输入性别: ");
                String sex = scn.nextLine();
                System.out.print("> 请输入职称: ");
                String tit = scn.nextLine();
                System.out.print("> 请输入学位: ");
                String deg = scn.nextLine();
                System.out.print("> 请输入职务: ");
                String pos = scn.nextLine();
                System.out.print("> 请输入毕业学校: ");
                String scho = scn.nextLine();
                System.out.print("> 请输入专业: ");
                String pro = scn.nextLine();
                Teach t = new Teach(sno, name, sex, tit, deg, pos, scho, pro);
                Node node = new Node(); //初始化一个新的链表节点
                node.tea = t;
                current.next = node;
                current = node;
                System.out.println("添加成功!");
            } else if (cmd.equals("4")) {
                System.out.print("> 请输入要删除的工号:");
                String sno = scn.nextLine();
                Node pre = head;    //头指针(前一个节点)
                Node tmp = head.next;   //第一个节点(后一个节点,两个节点挨着)
                while (tmp != null &&
                        !tmp.tea.sno.equals(sno)) {
                    pre = tmp;
                    tmp = tmp.next;
                }
                if (tmp != null) {
                    pre.next = tmp.next;
                    System.out.println("删除成功!");
                } else {
                    System.out.println("error!! 删除失败!");//
                }
            } else if (cmd.equals("5")) {
                System.out.print(">> 请输入要修改的学号:");
                String sno = scn.nextLine();
                Node tmp = head.next;
                while (tmp != null &&
                        !tmp.tea.sno.equals(sno)) {
                    tmp = tmp.next;
                }
                if (tmp != null) {
                    System.out.print("> 请输入姓名:");
                    String name = scn.nextLine();
                    System.out.print("> 请输入性别:");
                    String sex = scn.nextLine();
                    System.out.print("> 请输入职称:");
                    String tit = scn.nextLine();
                    System.out.print("> 请输入学位:");
                    String deg = scn.nextLine();
                    System.out.print("> 请输入职务:");
                    String pos = scn.nextLine();
                    System.out.print("> 请输入毕业学校:");
                    String scho = scn.nextLine();
                    System.out.print("> 请输入专业:");
                    String pro = scn.nextLine();
                    Teach t = tmp.tea;  //初始化此时节点的数据
                    t.name = name;
                    t.sex = sex;
                    t.tit = tit;
                    t.deg = deg;
                    t.pos = pos;
                    t.scho = scho;
                    t.pro = pro;
                    System.out.println("修改成功!");
                } else {
                    System.out.println(String.format("error: 修改失败!(Tips:找不到学号为:{%s} 的信息)", sno));//
                }
            } else if (cmd.equals("6")) {
                System.out.print("> 请输入保存路径:");
                String filepath = scn.nextLine();
                try {
                    FileWriter fw = new FileWriter(filepath);
                    Node tmp = head.next;
                    while (tmp != null) {
                        Teach s = tmp.tea;
                        fw.write(s.sno + "\t" + s.name + "\t" + s.sex + "\t" + s.tit + "\t"
                                + s.deg + "\t" + s.pos + "\t" + s.scho + "\t" + s.pro + "\n");
                        fw.flush();
                        tmp = tmp.next;
                    }
                    fw.close();
                    System.out.println("导出成功!");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } while (!cmd.equals("0"));
        System.out.println("退出成功!");
        scn.close();
        System.exit(0);
    }
}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表可以用来实现员工通讯录管理系统。下面是一种可能的实现方式: 定义员工结构体: ``` typedef struct Employee { int id; // 员工编号 char name[20]; // 员工姓名 char phone[20]; // 员工电话 struct Employee *next; // 指向下一个员工的指针 } Employee; ``` 定义链表结构体: ``` typedef struct EmployeeList { Employee *head; // 链表头指针 int length; // 链表长度 } EmployeeList; ``` 初始化链表: ``` EmployeeList initEmployeeList() { EmployeeList list; list.head = NULL; list.length = 0; return list; } ``` 添加员工: ``` void addEmployee(EmployeeList *list, Employee employee) { Employee *newEmployee = (Employee*)malloc(sizeof(Employee)); *newEmployee = employee; newEmployee->next = NULL; if(list->head == NULL) { list->head = newEmployee; } else { Employee *p = list->head; while(p->next != NULL) { p = p->next; } p->next = newEmployee; } list->length++; } ``` 删除员工: ``` void deleteEmployee(EmployeeList *list, int id) { if(list->head == NULL) { return; } if(list->head->id == id) { Employee *temp = list->head; list->head = list->head->next; free(temp); list->length--; return; } Employee *p = list->head; while(p->next != NULL) { if(p->next->id == id) { Employee *temp = p->next; p->next = p->next->next; free(temp); list->length--; return; } p = p->next; } } ``` 查找员工: ``` Employee* findEmployee(EmployeeList *list, int id) { Employee *p = list->head; while(p != NULL) { if(p->id == id) { return p; } p = p->next; } return NULL; } ``` 遍历链表: ``` void traverse(EmployeeList *list) { Employee *p = list->head; while(p != NULL) { printf("id:%d name:%s phone:%s\n", p->id, p->name, p->phone); p = p->next; } } ``` 这样,我们就可以使用单链表实现员工通讯录管理系统了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值