Question: LinkedListLibrary using System; namespace LinkedListLibrary { // class to represent one node in a list class ListNode { public T Data { get; private set;

 LinkedListLibrary using System; namespace LinkedListLibrary { // class to represent one

LinkedListLibrary using System;

namespace LinkedListLibrary { // class to represent one node in a list class ListNode { public T Data { get; private set; } public ListNode Next { get; set; } public ListNode(T dataValue) : this(dataValue, null) { } public ListNode(T dataValue, ListNode nextNode) { Data = dataValue; Next = nextNode; } } public class List { private ListNode firstNode; private ListNode lastNode; private string name; public List(string listName) { name = listName; firstNode = lastNode = null; }

public List() : this("list") { }

public void Count() { int counter = 0; ListNode currentNode = firstNode; while (currentNode != null) { counter++; currentNode = currentNode.Next; } Console.WriteLine("The number of elements in List is:" + counter + " "); }

public void Search(T insertItem) { ListNode currentNode = firstNode; string result = ""; while (currentNode != null) { if (insertItem.ToString() == currentNode.Data.ToString()) { result = string.Format("{0} has been found", currentNode.Data.ToString()); } else { result = string.Format("Cannot find {0} in the list ", insertItem); } currentNode = currentNode.Next; } Console.WriteLine(result + " "); } public void Avg() { int counter = 0; double convertNodeToDouble = 0; double temp = 0; double result = 0; ListNode currentNode = firstNode; while (currentNode != null) { counter++; double.TryParse(currentNode.Data.ToString(), out convertNodeToDouble); temp += convertNodeToDouble; result = temp / counter; currentNode = currentNode.Next; } Console.WriteLine("The average of the list is " + result + " "); }

public void InsertAtFront(T insertItem) { if (IsEmpty()) { firstNode = lastNode = new ListNode(insertItem); } else { firstNode = new ListNode(insertItem, firstNode); } }

public void InsertAtBack(T insertItem) { if (IsEmpty()) { firstNode = lastNode = new ListNode(insertItem); } else { lastNode = lastNode.Next = new ListNode(insertItem); } }

public T RemoveFromFront() { if (IsEmpty()) { throw new EmptyListException(name); }

T removeItem = firstNode.Data; if (firstNode == lastNode) { firstNode = lastNode = null; } else { firstNode = firstNode.Next; }

return removeItem; }

public T RemoveFromBack() { if (IsEmpty()) { throw new EmptyListException(name); }

T removeItem = lastNode.Data; if (firstNode == lastNode) { firstNode = lastNode = null; } else { ListNode current = firstNode;

while (current.Next != lastNode) { current = current.Next; }

lastNode = current; current.Next = null; }

return removeItem; }

public bool IsEmpty() { return firstNode == null; }

public void Display() { if (IsEmpty()) { Console.WriteLine($"Empty {name}"); } else { Console.Write($"The {name} is: ");

ListNode current = firstNode;

while (current != null) { Console.Write($"{current.Data} "); current = current.Next; }

Console.WriteLine(" "); } } } public class EmptyListException : Exception { public EmptyListException() : base("The list is empty") { }

public EmptyListException(string name) : base($"The {name} is empty") { }

public EmptyListException(string exception, Exception inner) : base(exception, inner) { } } }

LinkedListTest

// Fig. 19.5: ListTest.cs // Testing class List. using System; using LinkedListLibrary;

// class to test List class functionality class ListTest { static void Main() { var list = new List();

list.InsertAtFront(34.56); list.Display(); list.InsertAtFront(56.43); list.Display(); list.InsertAtBack(743.3321); list.Display(); list.InsertAtBack(534.21); list.Display(); list.Search(32.1); list.Count(); list.Avg();

// remove data from list and display after each removal try { object removedObject = list.RemoveFromFront(); Console.WriteLine($"{removedObject} removed"); list.Display();

removedObject = list.RemoveFromFront(); Console.WriteLine($"{removedObject} removed"); list.Display();

removedObject = list.RemoveFromBack(); Console.WriteLine($"{removedObject} removed"); list.Display();

removedObject = list.RemoveFromBack(); Console.WriteLine($"{removedObject} removed"); list.Display(); } catch (EmptyListException emptyListException) { Console.Error.WriteLine($" {emptyListException}"); } } }

In the Lab Assignment 02, you have built generic LinkedListLibrary (a.dll file) and then tested it in the LinkedListTest. You need to extend that LinkedListlibrary by adding the following methods in the LinkedListTest project. a) GetFirst() it returns the first element of the LinkedList. b) Getlast() - > it returns the last element of the LinkedList Also add validation here such if linked list is empty, then return null etc

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!