Question: Using C# 1. Code => LinkedList Method - T Minimum() method which will return the smallest item/node value - T GetLastNode() which will return the

Using C#

1. Code => LinkedList Method

- T Minimum() method which will return the smallest item/node value - T GetLastNode() which will return the last element in the linked list

- LinkedListLibraryTest => creating two linked lists of integers and doubles ( containing at least 5 elements each )

and calling the methods Minimum() and GetLastNode()

NO1. based on using this code add method

namespace LinkedListLibrary { // class to represent one node in a list class ListNode { // automatic read-only property Data public T Data { get; private set; }

// automatic property Next public ListNode Next { get; set; }

// constructor to create ListNode that refers to dataValue // and is last node in list public ListNode(T dataValue) : this(dataValue, null) { }

// constructor to create ListNode that refers to dataValue // and refers to next ListNode in List public ListNode(T dataValue, ListNode nextNode) { Data = dataValue; Next = nextNode; } }

// class List declaration public class List { private ListNode firstNode; private ListNode lastNode; private string name; // string like "list" to display

// construct empty List with specified name public List(string listName) { name = listName; firstNode = lastNode = null; }

// construct empty List with "list" as its name public List() : this("list") { }

// Insert T at front of List. If List is empty, // firstNode and lastNode will refer to same T. // Otherwise, firstNode refers to new node. public void InsertAtFront(T insertItem) { if (IsEmpty()) { firstNode = lastNode = new ListNode(insertItem); } else { firstNode = new ListNode(insertItem, firstNode); } }

// Insert T at end of List. If List is empty, // firstNode and lastNode will refer to same T. // Otherwise, lastNode's Next property refers to new node. public void InsertAtBack(T insertItem) { if (IsEmpty()) { firstNode = lastNode = new ListNode(insertItem); } else { lastNode = lastNode.Next = new ListNode(insertItem); } }

// remove first node from List public T RemoveFromFront() { if (IsEmpty()) { throw new EmptyListException(name); }

T removeItem = firstNode.Data; // retrieve data

// reset firstNode and lastNode references if (firstNode == lastNode) { firstNode = lastNode = null; } else { firstNode = firstNode.Next; }

return removeItem; // return removed data }

// remove last node from List public T RemoveFromBack() { if (IsEmpty()) { throw new EmptyListException(name); }

T removeItem = lastNode.Data; // retrieve data

// reset firstNode and lastNode references if (firstNode == lastNode) { firstNode = lastNode = null; } else { ListNode current = firstNode;

// loop while current.Next is not lastNode while (current.Next != lastNode) { current = current.Next; // move to next node }

// current is new lastNode lastNode = current; current.Next = null; }

return removeItem; // return removed data } // MAX public T Max(params T[] values) where T : IComparable { T max = values[0]; for (int i = 1; i < values.Length; i++) if (values[i].CompareTo(max) > 0) max = values[i]; return max; } // return true if List is empty public bool IsEmpty() { return firstNode == null; }

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

ListNode current = firstNode;

// output current node data while not at end of list while (current != null) { Console.Write($"{current.Data} "); current = current.Next; }

Console.WriteLine(" "); } } }

// class EmptyListException declaration public class EmptyListException : Exception { // parameterless constructor public EmptyListException() : base("The list is empty") { }

// one-parameter constructor public EmptyListException(string name) : base($"The {name} is empty") { }

// two-parameter constructor public EmptyListException(string exception, Exception inner) : base(exception, inner) { } } }

==========================================================================

2. Queue ( generic version) : to enhance class library project QueueInheritanceLibrary ( generic version), which is derived from LinkedListLibrary (from no1)

- T GetLast() which will just return the last element in the queue and not delete it.

- QueueInheritanceLibraryTest by creating two linked lists based queue objects of integers and doubles

and calling the method GetLast() and Minimum().

I need C# code both of them( Q1, Q2)

using LinkedListLibrary;

namespace QueueInheritanceLibrary { // class QueueInheritance inherits List's capabilities public class QueueInheritance : List { // pass name "queue" to List constructor public QueueInheritance() : base("queue") { }

// place dataValue at end of queue by inserting // dataValue at end of linked list public void Enqueue(object dataValue) { InsertAtBack(dataValue); }

// remove item from front of queue by removing // item at front of linked list public object Dequeue() { return RemoveFromFront(); } } }

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!