2

我需要能够通过这个链接列表返回,但是无论我尝试什么它都不起作用。你能为我指出正确的方向吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student
{ 
    class Program
    {
        public class student_data
        {  
            public string forename;
            public string surname;
            public int id_number;   
            public float averageGrade;  
            public string programme_title;    
            public string programme_code;  
            public student_data nextItem;
            //Declaring public title and public code as a string   
        }

        // Notice that a reference to the struct is being passed in  
        static void populatestudent(out student_data student, string

        fname, string surname, int id_number, string ptitle, string pcode)
        {
            student = new student_data();
            student.forename = fname;   
            student.surname = surname;
            student.id_number = id_number;  
            student.averageGrade = 0.0F;   
            student.programme_title = ptitle;   
            student.programme_code = pcode;
            //populating structre code by adding p code and p title  
        }

        static void Main(string[] args)
        {
            student_data student0, student1, student2, student3;

            populatestudent(out student0, "Mark", "Anderson", 0, "Progrmming Language", "M9604");//student 0 info
            populatestudent(out student1, "Jon", "Smith", 1, "Progrmming Language", "M9604");//student 1 info
            populatestudent(out student2, "Tom", "Jones", 2, "Progrmming Language", "M9604");//student 3 info
            populatestudent(out student3, "Ewan", "Evans", 3, "Progrmming Language", "M9604");//student 4 info

            student_data head = student0;
            student_data tail = student3;

            head.nextItem = student0;
            student0.nextItem = student1;
            student1.nextItem = student2;
            student2.nextItem = student3;

            student_data current = head;
            while (current != null)
            {
                Console.WriteLine("Name: " + current.forename + " " + current.surname);  
                Console.WriteLine("Id: " + current.id_number);  
                Console.WriteLine("Av grade: " + current.averageGrade);   
                Console.WriteLine("Prog title: " + current.programme_title);//find and display students programme title   
                Console.WriteLine("Prog code: " + current.programme_code);
                current = current.nextItem; 
            }
            Console.ReadKey();  
        }   
    }
}
4

2 回答 2

14

您的链接列表只是链接的 - 每个项目都包含对下一个项目的引用,但不包含对前一个项目的引用。你不能在这样的列表中倒退。(如果没有有效地构建另一个包含所有项目的集合。)您需要创建一个双向链表。然后很容易。按照链接(不是双关语)了解有关单链表和双链表的更多信息。

我还强烈建议您开始遵循 .NET 命名约定,并使用属性而不是公共字段。

于 2013-10-30T21:01:40.283 回答
1

您可以使用递归向后迭代它。例如:

private void recurse(student_data current)
{
    if(current.nextItem != null)
        recurse(current.nextItem);
    //Do your iteration logic here
    Console.WriteLine("Name: " + current.forename + " " + current.surname);  
    Console.WriteLine("Id: " + current.id_number);  
    Console.WriteLine("Av grade: " + current.averageGrade);   
    Console.WriteLine("Prog title: " + current.programme_title);//find and display students programme title   
    Console.WriteLine("Prog code: " + current.programme_code);
}

然后您所要做的就是将列表的头部传递给递归方法。

于 2013-10-30T21:07:48.547 回答