//Constructor with parameter public MySinglyLinkedList(student_data value) { // Converts the information from the student_data given into the class student_forename = value.forename; student_surname = value.surname; student_id_num = value.id_number; student_AverageGrade = value.averageGrade; next = null; }
// populateStruct function - amended to include programme_title and programme_code parameters. // takes an uninitialized struct and populates each field based on parameters inputted static void populateStruct(out student_data student, string fname, string surname, int id_number, string programme_title, string programme_code) { // Sets the relevant data field in relation to parameter input student.forename = fname; student.surname = surname; student.id_number = id_number; student.averageGrade = 0.0F; student.programme_title = programme_title; student.programme_code = programme_code; }
//Insert a new node with a value after the current node public void InsertNode(MySinglyLinkedList current, student_data value) { MySinglyLinkedList node = new MySinglyLinkedList(value); if (current.next == null) { current.next = node; // Inserts the next node after the current node if there currently is none } else { // - current is the temporary node in this case // - node is the new node to be allocated node.next = current.next; // assigns the new node's next pointer to the current node's next current.next = node; // re-assigns the current node's next to point to the new node } }
// Program entry point static void Main(string[] args) { // Prompt and store input from the user Console.WriteLine("Enter the number of students:"); int input = Convert.ToInt32(Console.ReadLine()); // Dynamically allocate an array based on the user's input student_data[] students = new student_data[input]; // For every element that has been specified for (int i = 0; i < students.Length; i++) { // Prompt and store name input Console.WriteLine("Person " + (i + 1) + ", please enter your name:"); string u_name = Console.ReadLine(); // locally store // Prompt and store surname input Console.WriteLine("Person " + (i + 1) + ", please enter your surname:"); string u_lname = Console.ReadLine(); // locally store // Prompt and store id input Console.WriteLine("Person " + (i + 1) + ", please enter your ID number:"); int u_id = Convert.ToInt32(Console.ReadLine()); // locally store // Prompt and store programme title input Console.WriteLine("Person " + (i + 1) + ", please enter your Programme Title:"); string u_pTitle = Console.ReadLine(); // locally store // Prompt and store name input Console.WriteLine("Person " + (i + 1) + ", please enter your Programme Code:"); string u_pCode = Console.ReadLine(); // locally store Console.WriteLine(); // initialise struct with inputs given from the user for this iteration populateStruct(out students[i], u_name, u_lname, u_id, u_pTitle, u_pCode); } // Print every student's data out to the console printAllStudent(ref students); }