Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(I WILL BE SURE TO UPVOTE)!!!!!!!!!!! :) Please create a doubly linked list that is generic in C# and modify the code below as needed

(I WILL BE SURE TO UPVOTE)!!!!!!!!!!! :)

Please create a doubly linked list that is generic in C# and modify the code below as needed so that it will run in visual studio 2022. You MUST use the code below for SinglyLinkedList as a starting point, and change that as needed for your DoublyLinkedList. You are expected to fully implement this class, without the use of the C# library functions (from namespaces such as System.Collections and System.Collections.Generic). Make sure it includes the following methods (and test them with both int and string):

AddFirst

AddLast

DeleteFirst

DeleteLast

Delete(given a value)

Delete(given a node)

Reverse

IsEmpty

Clear

Print

CODE TO MODIFY:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace CSC340._515_SinglyLinkedList { public class SinglyLinkedList { //data Node head;

//methods //Add, same as AddBack public void Add(T someValue) //O(n) { AddBack(someValue);//O(n) }

public void AddBack(T someValue)//O(n) { //create a new node Node newNode = new Node(someValue);

if (IsEmpty()) { head = newNode; } else { //find the last node: Node finger = head; //as long as there is a node to the right of finger while (finger.Next != null) { // ... move there finger = finger.Next; }

//when you get here, finger points to the last node finger.Next = newNode;//link in the new node }

} //AddFirst public void AddFirst(T someValue) //O(1) { //create a new node Node newNode = new Node(someValue);

//point newNode to the head newNode.Next = head;

//move the head head = newNode; } //DeleteFirst public void DeleteFirst() //O(1) { if(head==null) { throw new Exception("You cannot delete from an empty list"); } else { //move the head head = head.Next; } }

//DeleteLast public void DeleteLast()//O(n) { if(head==null) { throw new Exception("You cannot delete from an empty list"); } else if(head.Next == null) //you have only one element in the list { head = null; } else { //traverse the list ... go to the next to last node Node finger = head;

while (finger.Next.Next != null) { finger = finger.Next; }

//link the last node out finger.Next = null; } }

//DeleteValue public void DeleteValue(T someValue) { if (someValue.Equals(head.Value)) DeleteFirst(); else { Node finger = head; while(finger.Next!=null && !finger.Next.Value.Equals(someValue)) { finger = finger.Next; }

if(finger.Next != null) // we found the value { finger.Next = finger.Next.Next; } } }

//SearchValue public Node Search(T someValue) { Node finger = head; while (finger != null) { if(finger.Value.Equals(someValue))//check if someValue is at that location { return finger; //we found it! } finger = finger.Next; //move "right" }

//you get here if someValue was not found return null; }

//ToString public override string ToString() { String result = "";

Node finger = head; //as long as finger points to a not null node, add it to result while(finger!=null) { result += finger.ToString(); finger = finger.Next; //move finger to the right }

return result; }

//Clear public void Clear() { head = null; }

//IsEmpty public bool IsEmpty() { return head == null; }

//ctors public SinglyLinkedList() { //head = null; } } }

I will be sure to upvote. thank you!

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

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions

Question

1. How will you, as city manager, handle these requests?

Answered: 1 week ago

Question

1. Identify the sources for this conflict.

Answered: 1 week ago