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 property Next public ListNode
// 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
// class List declaration public class List
// 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
// 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
// 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
// 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
// output List contents public void Display() { if (IsEmpty()) { Console.WriteLine($"Empty {name}"); } else { Console.Write($"The {name} is: ");
ListNode
// 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
- 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
Get step-by-step solutions from verified subject matter experts
