Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C#: Write a console app which makes use of generic LinkedList data structure

ID: 3919832 • Letter: I

Question

In C#:

Write a console app which makes use of generic LinkedList data structure ( Refer code examples covered during the lecture) to maintain the list of students ( add at least 5, you need to create a student class ( Student.cs, with properties – ID and Name and appropriate constructors and overriding of ToString() method.) and demonstrate the use of following operations on the student linked list.

a) Add a student to the list (You need to define a method – AddLinkedListItem( LinkedList name , Student object) and inside that you can use built-in method of LinkedList class – AddFirst or AddLast

b) Remove a student from the list (You need to define a method – RemoveLinkedListItem( LinkedList name , Student object) and inside that you can use built-in method of LinkedList class – Remove)

c) Displaying the items of the linked list. (You need to define a method – PrintLinkedList( LinkedList name) , and inside that you can use built-in method of LinkedList class)

d) Search a student (You need to define a method – SearchLinkedListItem( LinkedList name, Student object) , and inside that you can use built-in method of LinkedList class)

e) Clearing the linked list. (You need to define a method – RemoveAllLinkedListItems( LinkedList name), and inside that you can use built-in method of LinkedList class)

Explanation / Answer

ScreenShot

----------------------------------

Program

//Packages used for I/O
using System;
using System.Collections.Generic;
//Student class
public class Student
{
    //Member variable
   private string sId;
   private string sName;
    //Default constructor
    public Student(){}
    //Parameterised constructor
    public Student(string id,string name)
    {
        sId = id;
        sName = name;
    }
    //tostring method override
    public override string ToString()
    {
        return "Student Id : " + sId + "         Student Name: " + sName;
    }
}
//Main class
namespace LinkedListAPP
{
    class Program
    {
        //Function to add in linked list
        private static void AddLinkedListItem(LinkedList<Student> linked, Student s)
        {
            linked.AddFirst(s);
        }
        //Function to remove from linked list
        private static void RemoveLinkedListItem(LinkedList<Student> linked, Student s)
        {
            linked.Remove(s);
        }
        //Function to print values from linked list
        private static void PrintLinkedList(LinkedList<Student> linked)
        {
            foreach (var student in linked)
            {
                Console.WriteLine(student.ToString());
            }
        }
        //Function to search a student
        private static void SearchLinkedListItem(LinkedList<Student> linked, Student obj)
        {
            var val = 1;
            foreach (var student in linked)
            {
                if (student == obj)
                {
                    Console.WriteLine(student.ToString());
                    val = 0;
                    break;
                }
            }
            if (val == 1)
            {
                Console.WriteLine("Not found!!!");
            }
          
        }
        //Clear linked list
        private static void RemoveAllLinkedListItems(LinkedList<Student> linked)
        {
            linked.Clear();
        }
        static void Main(string[] args)
        {
            //Variables to read name and id;
           string name,id;
            //Create a student linked list
            LinkedList<Student> linked = new LinkedList<Student>();
            //Object creation
          
            //Add first in linked list check
             Console.Write("Enter first student id: ");
             id = Console.ReadLine();
             Console.Write("Enter student name: ");
              name = Console.ReadLine();
              Student s= new Student(id,name);
              AddLinkedListItem(linked, s);

            Console.Write("Enter second student id: ");
            id = Console.ReadLine();
            Console.Write("Enter student name: ");
            name = Console.ReadLine();
            Student s1 = new Student(id, name);
            AddLinkedListItem(linked, s1);

            Console.Write("Enter third student id: ");
            id = Console.ReadLine();
            Console.Write("Enter student name: ");
            name = Console.ReadLine();
            Student s2 = new Student(id, name);
            AddLinkedListItem(linked, s2);

            //Print linked list
            Console.WriteLine("After adding students in linked list:");
            PrintLinkedList(linked);
            //Remove a student
            RemoveLinkedListItem(linked,s);
            Console.WriteLine("After Removing a student:");
            PrintLinkedList(linked);
            //Search a student
            Console.WriteLine("Search a student:");
            SearchLinkedListItem(linked, s1);
            //Clear linked list
            RemoveAllLinkedListItems(linked);
            Console.WriteLine("Remoove all:");
            PrintLinkedList(linked);

}

   }

}

---------------------------------------

Output

Enter first student id: M123
Enter student name: Madhav Das
Enter second student id: D143
Enter student name: Diya Raj
Enter third student id: K478
Enter student name: Kay watt
After adding students in linked list:
Student Id : K478         Student Name: Kay watt
Student Id : D143         Student Name: Diya Raj
Student Id : M123         Student Name: Madhav Das
After Removing a student:
Student Id : K478         Student Name: Kay watt
Student Id : D143         Student Name: Diya Raj
Search a student:
Student Id : D143         Student Name: Diya Raj
Remoove all:
Press any key to continue . . .