I need to be able to go back through this linked list, however no matter what I try it doesn't work. Can you point me in the right direction?
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();
}
}
}
Your linked list is only singly linked - each item contains a reference to the next item, but not the previous one. You can't walk backwards through a list like that. (Not without effectively building another collection with all of the items in.) You need to create a doubly linked list instead. Then it's easy. Follow the links (no pun intended) to find out more about singly and doubly linked lists.
I'd also strongly urge you to start following .NET naming conventions, and use properties instead of public fields.
See more on this question at Stackoverflow