![]() You could also consider to have a constructor with a vector of items: public LinkedList(IEnumerable items) So your list should have a default constructor with no arguments: public LinkedList() Having a one and only constructor that takes a node (or item) is not a good idea, because you often want to instantiate an empty list and provide it as argument to a method or something like that. This will require that you have private nodes for head and tail as illustrated above. Public T Tail => _tailNode.Data // TODO check for null The client should be agnostic about the inner implementation of your list and only "communicate" objects of type T with it: public T Head => _headNode.Data // TODO check for null It's fine to have a public Head and Tail property, but they should be of type T and not Node. It's easier to maintain, if you know that there is a one-to-one relationship between a data object and a Node. As shown above I've also made the Node class immutable for the Data property and hence no default constructor. In this way you can skip the type parameter on the Node class. So IMO keep this Node class as a dedicated node type for this linked list: public class LinkedList I don't think you can use this Node type as a node in a binary tree, because nodes in binary trees typically has references to other nodes like Parent, Left and Right. I've made the Node class public so I can use it for a binary tree implementation later Using (var linkedListEnumerator = myLinkedList.GetEnumerator()) Var myLinkedList = new LinkedList(new Node(99)) Using (var itr = someLinkedList.GetEnumerator()) ![]() Public static Node GetNodeFromList(LinkedList someLinkedList, Node someNode) where T : struct Var theNode = GetNodeFromList(myLinkedList, node2) Public void RemoveLast_Next_Node_Should_Be_Tail() Var theNode = GetNodeFromList(myLinkedList, node1) Var myLinkedList = new LinkedList(new Node(777)) Public void RemoveFirst_Next_Node_Should_Be_Head() Var myLinkedList = new LinkedList(new Node(35)) Public void AddToLast_Node_Should_Become_Tail() Var theNode = GetNodeFromList(myLinkedList, nodeToAdd) Var myLinkedList = new LinkedList(new Node(45)) Public void AddToFirst_Node_Should_Become_Head() Using DataStructuresAndAlgorithms.DataStructures Namespace DataStructuresAndAlgorithms.DataStructures namespace DataStructuresAndAlgorithms.DataStructures I've made the Node class public so I can use it for a binary tree implementation later. I'm wondering if there needs to be more tests for the LinkedList class and also if I'm writing them correctly. This is my first time writing a unit test. I want to see how other people think of my code, so here it goes. Here is the link for round 2: Linked list implementation along with unit test ![]() I've already made all the changes suggested here.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |