1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BagOnLinkedList { class Program { static void Main(string[] args) { // fill stack with Count elements int Count = 10; Bag<string> bag = new Bag<string>(); //FixedGenericsStack<string> stack = new FixedGenericsStack<string>(Count); Random r = new Random(); Console.WriteLine("lets fill bag with Count elements"); for (int i = 0; i < Count; i++) { int someRandomNumber = r.Next(100); bag.push(someRandomNumber.ToString()); } // iterator foreach (var s in bag) { Console.WriteLine(s.ToString()); } Console.WriteLine("What size of bag now?"); Console.WriteLine(bag.size()); Console.WriteLine("If bag isEmpty?"); if (bag.isEmpty()) Console.WriteLine("Yes"); else Console.WriteLine("No"); Console.ReadLine(); } } public class Bag<T>:IEnumerable<T> { Node<T> first; private int N; class Node<T> { public T item; public Node<T> next; } public bool isEmpty() { return (first == null) || (N == 0); } public int size() { return N; } public void push(T item) { // adding to the begining Node<T> oldfirst = first; first = new Node<T>(); first.item = item; first.next = oldfirst; N++; } /* // no pop in stack method public T pop() { T item = first.item; first = first.next; N--; return item; } */ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } public IEnumerator<T> GetEnumerator() { var node = first; while (node != null) { yield return node.item; node = node.next; } } } } |