Need help creating counter for linked list in C #

I am trying to create my own linked list class that I created and make an enumerator for use with it in one of my C # programs. I don’t want to show too much of my code, so I hope this is enough.

I'm not sure, but is that how the counter should look?

class SinglyLinkedListEnumerator<T> : IEnumerator<T> { private Node<E> node; private Node<E> start; public SinglyLinkedListEnumerator(Node<T> node) { this.node = node; start = node; } public T Current { get { return node.getData(); } } public Boolean MoveNext() { if (node.getNext() != null) { node = node.getNext(); return true; } return false; } public void Reset() { node = start; } public void IDisposable.Dispose() { } } 
+4
source share
4 answers

Iterator blocks greatly simplify the creation of an object that implements IEnumerable<T> :

 public static IEnumerable<T> Iterate<T>(Node<T> root) { var current = root; while (current != null) { yield return current.getData(); current = current.getNext(); } } 

It removes most of the entire template code and allows you to define all the logic to determine what the nodes are in the sequence, while still preserving the full functionality of writing out all the code, just like you.

+3
source

In the C # world, this concept is called enumeration instead of iteration. Not to be confused with enum .

In any case, the corresponding interfaces you are looking for are IEnumerable<T> and IEnumerator<T> in the System.Collections.Generic namespace. Check their documentation and you should be good to go. The concept itself is pretty much identical to what you did in Java.

0
source

I don't know what your Node<T> code looks like, but it looks weird:

 public Boolean MoveNext() { if (node.getNext() != null) { node = node.getNext(); return true; } return false; } 

Ideally, it seems that MoveNext would look like this:

 public Boolean MoveNext() { // make sure current node is not null if (node != null) { node = node.getNext(); } // simply return whether current node is null or not return node != null; } 

In your original method, you get the next node twice, which may give you false results. I assume that getNext just returns a link to the current node next brother. Otherwise, I don't see any initial problems with your enumerator (or iterator) class.

For clarity, I would use currentNode instead of node and change start to startNode . Also, if node.getNext() does not return a node object, then the method should be renamed to indicate what it is doing.

I made the assumption that node.getNext() moved the internal link, but @Servy fixed me. Thank you

I can still recommend some name changes to clarify the operation. :)

0
source

The enumerator for the linked list should look like this:

 public class MyLinkedList : IEnumerable { Node head = null; Node current = null; public IEnumerator GetEnumerator() { current = head; while (current != null) { yield return current.Data; current = current.Next; } } 

While node

 public class Node { public int Data; public Node Next; public Node(int value) { Data = value; } } 

For a binary tree, you must implement all three: Current, MoveNext and Reset +, do not forget about the constructor.

0
source

Source: https://habr.com/ru/post/1482468/


All Articles