c# - 我如何反转链表

标签 c# linked-list

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

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();  
        }   
    }
}

最佳答案

你的链表只有singly linked - 每个项目都包含对下一个项目的引用,但不包含对前一个项目的引用。您不能在这样的列表中倒退。 (并非没有有效地构建另一个包含所有项目的集合。)您需要创建一个 doubly linked list反而。然后就很容易了。点击链接(无双关语意)了解更多关于单链表和双向链表的信息。

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

关于c# - 我如何反转链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19693683/

相关文章:

c# - 无法确定条件表达式的类型 (Func)

c# - 我使用 Win32_Fan 获取并显示我的 CPU 风扇速度,但它不起作用,为什么?

python - 如何在 Cython 中声明带有指针的链表

c++ - 链接列表打印不正确?

c# - 使用自定义属性跳过body方法

c# - Xamarin.Android 中的 Android 数据绑定(bind)库

c# - 从 MS CRM 工作流事件调用 SQL Server Reporting Services

java - 将当前节点设置为空?

在 C 中正确实现链表

在 C 中将项目强制转换到链表末尾