一、题目

面试题:C#编程实现以下要求(不允许用封装方法,需要自己构建):

1. 依照数据,实现Student类

2. 实现单向链表类

类方法:

1) Student Get(int id); 根据学号查询,参数为int regID, 返回为Student对象

2) Student Locate(int pos); 根据链表位置查询,参数为int pos, 返回为Student对象

3) bool Insert(Student stu); 以学号为key升序插入链表,参数为Student对象,返回值为true/false

3.读取数据文件,格式化为Student对象,并添加到List里

4.根据学号,查询该学生信息

5.根据位置,查询该学生信息

数据文件内容(姓名,学号,成绩):

郑英,20216793,94

尹超,20196587,79

吴霞,20228110,92

贺勇,20206374,73

作答时长要求1h以内

二、参考答案(AI作答)

以下作答内容为AI作答,仅供参考,若对答案有异议,恳请评论区指出谢谢!

①学生实体类:

public class Student
{
    public string Name { get; set; }
    public int ID { get; set; }
    public int Score { get; set; }

    public Student(string name, int id, int score)
    {
        Name = name;
        ID = id;
        Score = score;
    }
}

②单向链表类:

public class LinkedList
{
    private class Node
    {
        public Student Data { get; set; }
        public Node Next { get; set; }

        public Node(Student student)
        {
            Data = student;
            Next = null;
        }
    }

    private Node head;

    public LinkedList()
    {
        head = null;
    }

    public Student Get(int id)
    {
        Node current = head;
        while (current != null)
        {
            if (current.Data.ID == id)
                return current.Data;
            current = current.Next;
        }
        return null; // if not found
    }

    public Student Locate(int pos)
    {
        Node current = head;
        int index = 0;
        while (current != null && index < pos)
        {
            current = current.Next;
            index++;
        }
        if (current != null)
            return current.Data;
        else
            return null; // if position is out of bounds
    }

    public bool Insert(Student stu)
    {
        Node newNode = new Node(stu);

        if (head == null || head.Data.ID > stu.ID)
        {
            newNode.Next = head;
            head = newNode;
            return true;
        }

        Node current = head;
        while (current.Next != null && current.Next.Data.ID < stu.ID)
        {
            current = current.Next;
        }

        newNode.Next = current.Next;
        current.Next = newNode;
        return true;
    }
}

③读取数据文件并格式化为 Student 对象

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        LinkedList studentList = new LinkedList();

        // 读取数据文件
        string[] lines = File.ReadAllLines("data.txt");

        foreach (string line in lines)
        {
            string[] parts = line.Split(',');
            if (parts.Length == 3)
            {
                string name = parts[0];
                int id = int.Parse(parts[1]);
                int score = int.Parse(parts[2]);
                Student student = new Student(name, id, score);
                studentList.Insert(student);
            }
        }

        // 示例:根据学号查询学生信息
        int searchID = 20216793;
        Student foundStudent = studentList.Get(searchID);
        if (foundStudent != null)
        {
            Console.WriteLine($"Student found: {foundStudent.Name}, ID: {foundStudent.ID}, Score: {foundStudent.Score}");
        }
        else
        {
            Console.WriteLine($"Student with ID {searchID} not found.");
        }

        // 示例:根据位置查询学生信息
        int position = 2;
        Student studentAtPosition = studentList.Locate(position);
        if (studentAtPosition != null)
        {
            Console.WriteLine($"Student at position {position}: {studentAtPosition.Name}, ID: {studentAtPosition.ID}, Score: {studentAtPosition.Score}");
        }
        else
        {
            Console.WriteLine($"No student found at position {position}.");
        }
    }
}

(工资参考:5K-10K)

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部