Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Week 3: Exercise 1
- * This exercise is an introduction to linked lists and demonstrates the basic implementation of one
- * @Author James Wilkinson
- *
- **/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Student
- {
- public class Student
- {
- // Global variables where our data will be stored
- public string forename;
- public string surname;
- public int id_number;
- public float averageGrade;
- // Global variable for holding the next pointer in the list
- public Student next;
- /**
- * Method: Student
- * Appends data to the global variables and then updates the pointer
- * to the next element within the list.
- * @author James Wilkinson
- * */
- public Student(string fname, string surname, int id_number)
- {
- this.forename = fname;
- this.surname = surname;
- this.id_number = id_number;
- this.averageGrade = 0.0F;
- // When adding a new entry to the list ensure that
- // the pointer is set to null so that it's aware we're at the end of the list
- this.next = null;
- }
- /**
- * Method: printAllStudent
- * Loops through all elements in the list and prints out the first name of each
- * student.
- * @author: James Wilkinson
- * */
- static void printAllStudent(Student student)
- {
- // retrieve the current student object the student parameter is referring to
- Student thisStudent = student;
- // Loop through all instances until it reaches null
- while (thisStudent != null)
- {
- Console.WriteLine(thisStudent.forename + " " + thisStudent.surname + " " + thisStudent.id_number);
- // Update the current element of the index to the next object referred to by the
- // next pointer
- thisStudent = thisStudent.next;
- }
- }
- static void Main(string[] args)
- {
- // Append the data
- Student studentOne = new Student("Jay", "Wilkinson", 4000);
- Student studentTwo = new Student("Chris", "Thompson", 3827);
- // Assign the next pointer to the index of the object referred to as the last
- // structure of data added.
- studentOne.next = studentTwo;
- printAllStudent(studentOne);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment