Advertisement
tomdodd4598

Untitled

Oct 23rd, 2021
1,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace LinkedList
  5. {
  6.     public class Item<T>
  7.     {
  8.         public readonly T value;
  9.         public Item<T> next;
  10.  
  11.         public Item(T value, Item<T> next)
  12.         {
  13.             this.value = value;
  14.             this.next = next;
  15.         }
  16.  
  17.         ~Item() => Console.WriteLine($"Garbage collecting item: {value}");
  18.  
  19.         public Item<T> this[int n]
  20.         {
  21.             get
  22.             {
  23.                 var item = this;
  24.                 for (int i = 0; i < n; i++)
  25.                 {
  26.                     item = item?.next;
  27.                 }
  28.                 return item;
  29.             }
  30.         }
  31.  
  32.         public IEnumerable<Item<T>> GetIterator()
  33.         {
  34.             var item = this;
  35.             while (item != null)
  36.             {
  37.                 yield return item;
  38.                 item = item.next;
  39.             }
  40.         }
  41.  
  42.         public Item<T> PrintGetNext()
  43.         {
  44.             Console.Write(value);
  45.             Console.Write(next == null ? "\n" : ", ");
  46.             return next;
  47.         }
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement