weixin_45237495 2020-10-29 17:51 采纳率: 0%
浏览 48

C#实现两个学生表交叉合并

(程序是书上的程序,程序没问题。)程序的功能是实现两个学生表交叉合并。在创建含有5个学生的信息表时,有一步"StudentNode node1=list1.Head while(node1 != null)",在StudentLinkList类中定义到Head为两个参数的变量,类型分别是”Student”“ Next“ ,但在while循环中,让"node1"和"null"比较,我看了node1来自Head,Head最后的变量是"Student[20200001]""Student[20200002]",这点费解,求大佬解释。 程序如下:

public struct Student

        {
            static readonly string[] LastNames = new string[] { "赵", "钱", "孙", "李", "周", "吴", "郑", "王" };
            static readonly string[] FirstNames = new string[] { "一", "二", "三", "四", "五", "六", "七", "八" };
            static readonly Random R = new Random();
            public readonly string Name;
            public readonly int Number;
            public readonly int Grade;
            public readonly int Result;
            public Student(int number)
            {
                Name = LastNames[R.Next(LastNames.Length)] + FirstNames[R.Next(FirstNames.Length)];
                Number = number;
                Grade = R.Next(1, 5);
                Result = R.Next(101);
            }
        }


public class StudentNode
        {
            public Student Student { get; set; }
            public StudentNode Next { get; set; }
            public StudentNode(Student student , StudentNode next)
            {
                Student = student;
                Next = next;
            }
        }

        public class StudentLinkList
        {
            public StudentNode Head { get; set; }
            public StudentLinkList(int count)
            {
                Head = new StudentNode(new Student(20200001 + count), null);
                for(int i = count - 1; i > 0; i--)
                {
                    Head = new StudentNode(new Student(20200001 + i), Head);
                }
            }
        }



                static void Main(string[] args)
        {
                StudentLinkList list1 = new StudentLinkList(5);
            StudentNode node1 = list1.Head;
            while (node1 != null)
            {
                Write(node1.Student.Name + "\t");
                node1 = node1.Next;
            }
            StudentLinkList list2 = new StudentLinkList(5);
            StudentNode node2 = list2.Head;
            while(node2 != null)
            {
                Write(node2.Student.Name + "\t");
                node2 = node2.Next;
            }

            node1 = list1.Head;
            node2 = list2.Head;
            while(node1 != null && node2 != null)
            {
                list1.Head = node2.Next;
                node2.Next = node1.Next;
                node1.Next = node2;
                node1 = node2.Next;
                node2 = list2.Head;
            }

            node1 = list1.Head;
            while(node1 != null)
            {
                Write(node1.Student.Name + "\t");
                node1 = node1.Next;
            }
        }

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作