Question: I WILL BE SURE TO UPVOTE!!!! :) Please do this in C# and modify and add below as needed so that it can run in
I WILL BE SURE TO UPVOTE!!!! :)
Please do this in C# and modify and add below as needed so that it can run in Visual Studio Code 2022! Create a doubly linked list that is generic without the use of C# library functions and add to the code below (and test them with both int and string):
-Delete(given a value)
-Delete(given a node)
-Reverse
*NOTE*:Please also be sure to add a ToString override in the node class.
CODE TO MODIFY:
namespace Program { public class Node
public Node(T value) { Value = value; Prev = null; Next = null; } } public class DoublyLinkedList
//methods //Add, same as AddLast public void Add(T someValue) //O(1) { AddLast(someValue);//O(1) }
public void AddLast(T someValue)//O(1) { //create a new node Node
if (IsEmpty()) { head = newNode; tail = newNode; } else { //link in the new node newNode.Prev = tail; tail.Next = newNode; //update tail tail = newNode; } }
//AddFirst public void AddFirst(T someValue) //O(1) { //create a new node Node
if (IsEmpty()) { head = newNode; tail = newNode; } else { //point newNode to the head newNode.Next = head; //point head to newNode head.Prev = newNode; //update head head = newNode; } } //IsEmpty private bool IsEmpty() { return head == null; } //Clear public void Clear() { head = null; }
//DeleteFirst public void DeleteFirst() //O(1) { if (head == null) { throw new Exception("You cannot delete from an empty list"); } else if (head == tail) //you have only one element in the list { head = null; tail = null; } else { //move the head head = head.Next; head.Prev = null; } }
//DeleteLast public void DeleteLast()//O(1) { if (tail == null) { throw new Exception("You cannot delete from an empty list"); } else if (head == tail) //you have only one element in the list { head = null; tail = null; } else { //traverse the list ... go to the second to last node tail = tail.Prev; tail.Next = null; } }
//DeleteValue public void DeleteValue(T someValue) { if (head == null) { throw new Exception("You cannot delete from an empty list"); } else if (someValue.Equals(head.Value)) { DeleteFirst(); } else if (someValue.Equals(tail.Value)) { DeleteLast(); } else { Node
if (finger != null) // we found the value { finger.Prev.Next = finger.Next; }
} } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
