Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C#: Using Custom Data Structures, Generic Collections, LINQ, Lambdas and Delegates Re-create a generic class (use T instead of object ) LinkedListLibrary ( .dll )

C#: Using Custom Data Structures, Generic Collections, LINQ, Lambdas and Delegates

Re-create a generic class (use T instead of object ) LinkedListLibrary ( .dll ) by following the naming guidelines mentioned in the assignment and do the following enhancement/modifications: a) You need to create a separate .cs file for each class. b) Type of the data in the node should be double. ( ListNode class) c) Add following methods apart from the existing methods: 1) Search( ) method which searches a given element in the liked list. Use appropriate argument(s) for the method. 2) Count( ) method which count the number of elements in the linked list. 3) Avg() method which returns the average of elements in the linked list 4) Test it by creating and calling all the methods to demonstrate their working.

Exercise # 1(a):

Re-create generic class ( use T instead of object ) StackInheritanceLibrary as per naming guidelines and modify it to maintain a stack of elements of type double. Also add one more method Peek() which returns the top element of the stack and test it by adding the reference of this library and demonstrate use of Push(), Pop() and Peek() methods.

Exercise #1(b):

Recreate generic class (use T instead of object) QueueInheritanceLibrary as per naming guidelines and modify it to maintain a queue of elements of type string. (add few names) Test it by adding the reference of this library and calling methods for inserting and deleting names from the queue.

image text in transcribed

-----LinkedListLibtary-----

// Fig. 19.4: LinkedListLibrary.cs // ListNode, List and EmptyListException class declarations. using System;

namespace LinkedListLibrary { // class to represent one node in a list class ListNode { // automatic read-only property Data public object 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(object dataValue) : this(dataValue, null) { }

// constructor to create ListNode that refers to dataValue // and refers to next ListNode in List public ListNode(object 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 object at front of List. If List is empty, // firstNode and lastNode will refer to same object. // Otherwise, firstNode refers to new node. public void InsertAtFront(object insertItem) { if (IsEmpty()) { firstNode = lastNode = new ListNode(insertItem); } else { firstNode = new ListNode(insertItem, firstNode); } }

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

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

object 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 object RemoveFromBack() { if (IsEmpty()) { throw new EmptyListException(name); }

object 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 }

// 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) { } } }

-----ListTest-----

// 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(); // create List container

// create data to store in List bool aBoolean = true; char aCharacter = '$'; int anInteger = 34567; string aString = "hello";

// use List insert methods list.InsertAtFront(aBoolean); list.Display(); list.InsertAtFront(aCharacter); list.Display(); list.InsertAtBack(anInteger); list.Display(); list.InsertAtBack(aString); list.Display();

// 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}"); } } }

-----QueueInheritanceLibrary-----

// Fig. 19.16: QueueInheritanceLibrary.cs // Implementing a queue by inheriting from class List. 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(); } } }

-----QueueTest-----

// Fig. 19.17: QueueTest.cs // Testing class QueueInheritance. using System; using QueueInheritanceLibrary; using LinkedListLibrary;

// demonstrate functionality of class QueueInheritance class QueueTest { static void Main() { QueueInheritance queue = new QueueInheritance();

// create objects to store in the queue bool aBoolean = true; char aCharacter = '$'; int anInteger = 34567; string aString = "hello";

// use method Enqueue to add items to queue queue.Enqueue(aBoolean); queue.Display(); queue.Enqueue(aCharacter); queue.Display(); queue.Enqueue(anInteger); queue.Display(); queue.Enqueue(aString); queue.Display();

// use method Dequeue to remove items from queue object removedObject = null;

// remove items from queue try { while (true) { removedObject = queue.Dequeue(); Console.WriteLine($"{removedObject} dequeued"); queue.Display(); } } catch (EmptyListException emptyListException) { // if exception occurs, write stack trace Console.Error.WriteLine(emptyListException.StackTrace); } } }

-----StackCompositionLibrary-----

// Fig. 19.15: StackCompositionLibrary.cs // StackComposition class encapsulates functionality of class List. using LinkedListLibrary;

namespace StackCompositionLibrary { // class StackComposition encapsulates List's capabilities public class StackComposition { private List stack;

// construct empty stack public StackComposition() { stack = new List("stack"); }

// add object to stack public void Push(object dataValue) { stack.InsertAtFront(dataValue); }

// remove object from stack public object Pop() { return stack.RemoveFromFront(); }

// determine whether stack is empty public bool IsEmpty() { return stack.IsEmpty(); }

// output stack contents public void Display() { stack.Display(); } } }

-----StackInheritanceLibrary-----

// Fig. 19.13: StackInheritanceLibrary.cs // Implementing a stack by inheriting from class List. using LinkedListLibrary;

namespace StackInheritanceLibrary { // class StackInheritance inherits class List's capabilities public class StackInheritance : List { // pass name "stack" to List constructor public StackInheritance() : base("stack") { }

// place dataValue at top of stack by inserting // dataValue at front of linked list public void Push(object dataValue) { InsertAtFront(dataValue); }

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

-----StackInheritanceTest-----

// Fig. 19.14: StackInheritanceTest.cs // Testing class StackInheritance. using System; using StackInheritanceLibrary; using LinkedListLibrary;

// demonstrate functionality of class StackInheritance class StackInheritanceTest { static void Main() { StackInheritance stack = new StackInheritance();

// create objects to store in the stack bool aBoolean = true; char aCharacter = '$'; int anInteger = 34567; string aString = "hello";

// use method Push to add items to stack stack.Push(aBoolean); stack.Display(); stack.Push(aCharacter); stack.Display(); stack.Push(anInteger); stack.Display(); stack.Push(aString); stack.Display();

// remove items from stack try { while (true) { object removedObject = stack.Pop(); Console.WriteLine($"{removedObject} popped"); stack.Display(); } } catch (EmptyListException emptyListException) { // if exception occurs, write stack trace Console.Error.WriteLine(emptyListException.StackTrace); } } }

LinkedListLibrary ListTest QueuelnheritanceLibrary QueueTest StackCompositionLibrary StacklnheritanceLibrary StacklnheritanceTest 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions

Question

What is the education level of your key public?

Answered: 1 week ago

Question

What are the cultural/ethnic/religious traits of your key public?

Answered: 1 week ago