Following program is developed using C# in Microsoft console application. It mainly contains 3 classes. Student class used to maintain student info and LinkedList class used to maintain linkedlist of student’s objects. Program class uses both Student & LinkedList class to run and show the required output.
using System;
namespace DataStructures
{
public class Student
{
// Constructors
public Student()
{
}
// Constructor Overloading
public Student(string fName)
{
FName = fName;
}
// Variables
private Student next;
// Properties
public Student Next
{
get { return next; }
set { next = value; }
}
public string FName { get; set; }
public string LName { get; set; }
public string CourseCode { get; set; }
public double Grade { get; set; }
// Methods
public string Display()
{
return “Name: ” + FName + ” ” + LName + ” Course: ” + CourseCode + ” Grade: ” + Grade + “\r\n”;
}
}
public class LinkedList
{
Student First { get; set; }
public void Insert(Student p_Student)
{
if (First == null)
First = p_Student;
else
{
Student current = First;
while (current.Next != null)
{
current = current.Next;
}
current.Next = p_Student;
}
}
public Student Delete(int p_Position)
{
Student current = First, previous = First;
if (p_Position > 0 && First != null)
{
int count = 1;
while (current.Next != null)
{
if (p_Position == count)
break;
previous = current;
current = current.Next;
count++;
}
previous.Next = current.Next;
if (First.Next == null && p_Position == 1)
First = null;
}
return current;
}
public string SearchByLastName(string p_LName)
{
Student current = First;
string names = string.Empty;
if (current != null)
{
while (current.Next != null)
{
if (current.LName.Equals(p_LName))
names = names + ” Student: ” + current.FName + ” ” + current.LName;
current = current.Next;
}
if (current.LName.Equals(p_LName))
names = “Student: ” + current.FName + ” ” + current.LName;
}
return names;
}
public int Length()
{
Student current = First;
int count = 0;
if (current != null)
{
count = 1;
while (current.Next != null)
{
count++;
current = current.Next;
}
}
return count;
}
public string Display()
{
return Display(0, 0);
}
//p_Start =0, p_End =0 displays whole list, Method Overloading
public string Display(int p_Start, int p_End)
{
int length = Length();
if (First == null)
return “Database Is Empty.”;
else if (p_Start < 0 || p_End < 0 || p_Start > length || p_End > length)
return “Invalid Range.”;
else
{
string studentsInfo = string.Empty;
if (First != null && p_Start >= 0 && p_End >= 0)
{
int count = 1;
Student current = First;
while (current.Next != null)
{
count++;
if (count – 1 >= p_Start &&
(p_End == 0 || (p_End != 0 && count <= p_End)))
studentsInfo = studentsInfo + current.Display();
current = current.Next;
}
studentsInfo = studentsInfo + current.Display();
}
return studentsInfo;
}
}
public double CourseAvg(string p_CourseCode)
{
double totGrade = 0;
int enrolledStudentsCount = 0;
if (First != null)
{
Student current = First;
while (current.Next != null)
{
if (current.CourseCode.Trim().ToUpper().Equals(p_CourseCode.ToUpper().Trim()))
{
enrolledStudentsCount++;
totGrade = totGrade + current.Grade;
}
current = current.Next;
}
if (current.CourseCode.Trim().ToUpper().Equals(p_CourseCode.ToUpper().Trim()))
{
enrolledStudentsCount++;
totGrade = totGrade + current.Grade;
}
}
return totGrade / (enrolledStudentsCount == 0 ? 1 : enrolledStudentsCount);
}
}
class Program
{
static void Main(string[] args)
{
LinkedList students = new LinkedList();
Student st1 = new Student { FName = “Ram”, LName = “One”, CourseCode = “DS”, Grade = 3.2 };
students.Insert(st1);
students.Insert(new Student { FName = “Ram”, LName = “Two”, CourseCode = “DS”, Grade = 3.3 });
Console.WriteLine(students.Display());
Console.ReadLine();
while (true)
{
Console.WriteLine(“Press 1 to insert a new record\n\r”
+ “Press 2 to delete a record\n\r”
+ “Press 3 to search the database (by last name)\n\r”
+ “Press 4 to print a range in the database\n\r”
+ “Press 5 to find the class average for a course\n\r”
+ “Press 9 to quit\n\r\n\rAnd Press Enter”
);
string choice = Console.ReadLine();
if (choice.Equals(“1”))
{
Console.WriteLine(“Enter Student’s First Name & Press Enter:\n\r”);
string fName = Console.ReadLine();
Console.WriteLine(“Enter Student’s Last Name & Press Enter:\n\r”);
string lName = Console.ReadLine();
Console.WriteLine(“Enter Student’s Course Code & Press Enter:\n\r”);
string courseCode = Console.ReadLine();
Console.WriteLine(“Enter Student’s Grade & Press Enter:\n\r”);
double grade = 0;
while (true)
{
try
{
grade = double.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine(“Enter valid Grade(1 to 4) & Press Enter\n\r”);
}
}
students.Insert(new Student { FName = fName, LName = lName, CourseCode = courseCode, Grade = grade });
Console.WriteLine(“Student details has been inserted into the database.\n\r”);
}
else if (choice.Equals(“2”))
{
Console.WriteLine(“Enter Delete Position & Press Enter:\n\r”);
int position = 0;
while (true)
{
try
{
position = Int32.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine(“Enter Valid Delete Postion & Press Enter\n\r”);
}
}
if (students.Delete(position) != null)
Console.WriteLine(“Student record from the database has been deleted from the specified position.\n\r”);
else
Console.WriteLine(“Database Is Empty.\n\r”);
}
else if (choice.Equals(“3”))
{
Console.WriteLine(“Enter Last Name To Search & Press Enter:\n\r”);
string lName = Console.ReadLine();
Console.WriteLine(“Serach Result:\n\r” + students.SearchByLastName(lName));
}
else if (choice.Equals(“4”))
{
Console.WriteLine(“Enter Start Position & Press Enter:\n\r”);
int startPosition = 0;
while (true)
{
try
{
startPosition = Int32.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine(“Enter Valid Start Postion & Press Enter\n\r”);
}
}
Console.WriteLine(“Enter End Position & Press Enter:\n\r”);
int endPosition = 0;
while (true)
{
try
{
endPosition = Int32.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine(“Enter Valid End Postion & Press Enter\n\r”);
}
}
Console.WriteLine(“Student Details For The Specified Range:\n\r” + students.Display(startPosition, endPosition));
}
else if (choice.Equals(“5”))
{
Console.WriteLine(“Enter Course Code & Press Enter:\n\r”);
string courseCode = Console.ReadLine();
Console.WriteLine(“Average Marks Of The Course: ” + students.CourseAvg(courseCode) + “\n\r”);
}
else if (choice.Equals(“9”))
break;
}
}
}
}
For more info –
Part1/Part2 Series –
http://www.youtube.com/watch?v=SbVxGOye5eE
http://www.youtube.com/watch?v=tTo72qNB7n4&feature=watch_response
Doubly Linked List –
http://www.codeproject.com/KB/recipes/doubly-linkedlist.aspx
Using C# In-built LinkedList DataStructure –