Jay_

Linked Lists in C#

Nov 12th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. /**
  2.  * Week 3: Exercise 1
  3.  * This exercise is an introduction to linked lists and demonstrates the basic implementation of one
  4.  * @Author James Wilkinson
  5.  *
  6. **/
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. namespace Student
  13. {
  14.     public class Student
  15.     {
  16.         // Global variables where our data will be stored
  17.         public string forename;
  18.         public string surname;
  19.         public int id_number;
  20.         public float averageGrade;
  21.  
  22.         // Global variable for holding the next pointer in the list
  23.         public Student next;
  24.  
  25.         /**
  26.          * Method: Student
  27.          * Appends data to the global variables and then updates the pointer
  28.          * to the next element within the list.
  29.          * @author James Wilkinson
  30.          * */
  31.         public Student(string fname, string surname, int id_number)
  32.         {
  33.             this.forename = fname;
  34.             this.surname = surname;
  35.             this.id_number = id_number;
  36.             this.averageGrade = 0.0F;
  37.  
  38.             // When adding a new entry to the list ensure that
  39.             // the pointer is set to null so that it's aware we're at the end of the list
  40.             this.next = null;
  41.         }
  42.  
  43.         /**
  44.          * Method: printAllStudent
  45.          * Loops through all elements in the list and prints out the first name of each
  46.          * student.
  47.          * @author: James Wilkinson
  48.          * */
  49.         static void printAllStudent(Student student)
  50.         {
  51.             // retrieve the current student object the student parameter is referring to
  52.             Student thisStudent = student;
  53.  
  54.             // Loop through all instances until it reaches null
  55.             while (thisStudent != null)
  56.             {
  57.                 Console.WriteLine(thisStudent.forename + " " + thisStudent.surname + " " + thisStudent.id_number);
  58.                 // Update the current element of the index to the next object referred to by the
  59.                 // next pointer
  60.                 thisStudent = thisStudent.next;
  61.             }
  62.         }
  63.  
  64.         static void Main(string[] args)
  65.         {
  66.             // Append the data
  67.             Student studentOne = new Student("Jay", "Wilkinson", 4000);
  68.             Student studentTwo = new Student("Chris", "Thompson", 3827);
  69.  
  70.             // Assign the next pointer to the index of the object referred to as the last
  71.             // structure of data added.
  72.             studentOne.next = studentTwo;
  73.  
  74.             printAllStudent(studentOne);
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment