C# 有谁能帮我验证链表中函数的工作情况吗

C# 有谁能帮我验证链表中函数的工作情况吗,c#,linked-list,C#,Linked List,我试图在一个链接中传递一个学生类的值,但函数不知何故不能正常工作。特定函数将执行一次,并且不会移动到另一个函数 //这个类正在实现这些函数 namespace ListAssignment { public class Node { public Node next; // reference for the next node private Student

我试图在一个链接中传递一个学生类的值,但函数不知何故不能正常工作。特定函数将执行一次,并且不会移动到另一个函数

//这个类正在实现这些函数

namespace ListAssignment
{ 
    public class Node
     {
          public Node next;                                    // reference for the next node         
    private Student s;                                   //object for student class

    public Node(String name, int age, int manummer, double grade)
        {
            s = new Student(name, age, manummer, grade);
            next = null;
        }

    internal Student S { get => s; set => s = value; }
}

    public class List
    {
        public Node headstudent= null;                                 //student1, list is initially empty
        public Node current;
        public Node previous;
        public int count;

    public List()
    {
        headstudent = null;
        current = null;
        previous = null;
    }

    public void Push(String name, int age, int manummer, double grade)
    {
        Node newnode = new Node(name, age, manummer, grade);
        newnode.next = headstudent;
        headstudent = newnode;
        count++;
    }

    public void PrintList()
        {
            Node ptr = headstudent;
            if (ptr.next != null)
            {
                //Console.WriteLine("details");
                Console.WriteLine(ptr.S.Name + ptr.S.Age + ptr.S.MatriculationNumber + ptr.S.Grade);
                Console.ReadLine();
                ptr = ptr.next;
            }
            else
                Console.WriteLine("the list is empty");
        count++;
        }

    public void AddAtEnd(String name, int age, int manummer, double grade)
    {
        Node newnode = new Node(name, age, manummer, grade);
        if (headstudent == null)
        {
            headstudent = newnode;
        }
        else
        {
            current = headstudent;
            while (current != null)
            {
                previous = current;
                current = current.next;
            }
            previous.next = newnode;
        }
    }

    public void AddAtStart(String name, int age, int manummer, double grade)
        {        
               if (headstudent == null)
            {
            headstudent = new Node(name, age, manummer, grade);
            }
            else
            {
            current = new Node(name, age, manummer, grade);
            current.next = headstudent;
            count++;
            }         
    }

    public object RemoveFirst()
    {
         if (count > 0)
                {
                    headstudent.next = headstudent.next.next;
                    count--;
                }
                else
                {
                    Console.WriteLine("list is empty");
                }
                return headstudent;
       }

    public void Replace(int manummer)
    {
        current = headstudent;
        while (current != null)
        {
            if (current.S.MatriculationNumber == manummer)
            {
                Console.WriteLine("repalce by another number");
                current.S.MatriculationNumber = manummer;
                current = current.next;
            }
            else
            {
                Console.WriteLine("enter other no.");
            }
        }
    }

}
}


//在replace函数中,我试图用一个新的整数值替换现有值。我试图直接传递函数中的值。作为参考,我还添加了输出。

开始使用调试器;放置断点,同时使用“监视”和“输出”窗口。见下图:

为了快速查看,我发现了几个错误:

PrintList方法没有打印所有节点的循环。但它只打印第一个节点。 每次添加节点时,计数不会增加。要做到这一点:增加所有两个方法顶部的计数:AddAtStart和AddAtEnd,并在推送时将其设置为等于1。并将其从所有其他地方移除。 AddAtStart不会将headstudent设置为方法末尾的新节点。 此外,您还可以使用一些快捷方式,如摆脱学生构造函数

[编辑]:添加代码

    public void Push(String name, int age, int manummer, double grade)
    {
        Node newnode = new Node(name, age, manummer, grade);
        current = headstudent = newnode;

        count = 1;
    }

    public void PrintList()
    {
        if (headstudent == null)
        {
            Console.WriteLine("the list is empty");
            return;
        }

        Node ptr = headstudent;
        while (ptr != null)
        {
            Console.WriteLine(ptr.S.Name + " " + ptr.S.Age + " " + ptr.S.MatriculationNumber + " " + ptr.S.Grade);
            ptr = ptr.next;
        }

        Console.ReadLine();

        // remove these lines:
        //else
        //    Console.WriteLine("the list is empty");

        // You should not increment count here anyway!
        //count++;
    }

    public void AddAtEnd(String name, int age, int manummer, double grade)
    {
        Node newnode = new Node(name, age, manummer, grade);

        if (headstudent == null)
            headstudent = newnode;
        else
        {
            current = headstudent;
            while (current != null)
            {
                previous = current;
                current = current.next;
            }
            previous.next = newnode;        // try current =
        }

        count++;
    }

    public void AddAtStart(String name, int age, int manummer, double grade)
    {
        Node newnode = new Node(name, age, manummer, grade);

        if (headstudent == null)
            current = headstudent = newnode;
        else
        {
            current = newnode;
            current.next = headstudent;
            headstudent = newnode;
        }

        count++;
    }

    public void RemoveFirst()
    {
        if (count > 0)
        {
            headstudent = headstudent.next;
            count--;
        }
        else
        {
            Console.WriteLine("list is empty");
        }
    }

    public void Replace(int manummer, int newNumber)
    {
        current = headstudent;
        while (current != null)
        {
            if (current.S.MatriculationNumber == manummer)
            {
                current.S.MatriculationNumber = newNumber;
                Console.WriteLine(manummer + " is replaced by: " + newNumber);
                break;
            }
            current = current.next;
        }
    }

您是否需要自己实现List类?因为您可以使用.NET中已经存在的List类。@BassemAkl是的,我是从头开始实现的。如果我遗漏了什么,请纠正我,但是您的l.PrintList没有以任何方式进行迭代,您只需打印一次头部。尝试使用while而不是if?@BassemAkl如果可能的话你能纠正我的打印功能吗…@R2D2,当然。看我编辑的答案!