Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace LinkedList
- {
- public class Item<T>
- {
- public readonly T value;
- public Item<T> next;
- public Item(T value, Item<T> next)
- {
- this.value = value;
- this.next = next;
- }
- ~Item() => Console.WriteLine($"Garbage collecting item: {value}");
- public Item<T> this[int n]
- {
- get
- {
- var item = this;
- for (int i = 0; i < n; i++)
- {
- item = item?.next;
- }
- return item;
- }
- }
- public IEnumerable<Item<T>> GetIterator()
- {
- var item = this;
- while (item != null)
- {
- yield return item;
- item = item.next;
- }
- }
- public Item<T> PrintGetNext()
- {
- Console.Write(value);
- Console.Write(next == null ? "\n" : ", ");
- return next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement