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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace QueueOnOneLinkedList { class Program { static void Main(string[] args) { // fill stack with Count elements int Count = 10; CustomQueueOnLinkedList<string> queue = new CustomQueueOnLinkedList<string>(); Random r = new Random(); Console.WriteLine("lets fill queue with Count elements"); for (int i = 0; i < Count; i++) { int someRandomNumber = r.Next(100); queue.Enque(someRandomNumber.ToString()); } // iterator foreach (var s in queue) { Console.WriteLine(s.ToString()); } Console.WriteLine("What size of queue now?"); Console.WriteLine(queue.size()); Console.WriteLine("lets take last 5 elements from queue"); for (int i = 0; i < 5; i++) { Console.WriteLine(queue.Deque()); } Console.WriteLine("What size of queue now?"); Console.WriteLine(queue.size()); Console.WriteLine("If queue isEmpty?"); if (queue.isEmpty()) Console.WriteLine("Yes"); else Console.WriteLine("No"); Console.ReadLine(); } } class CustomQueueOnLinkedList<T> : IEnumerable<T> { Node<T> first; Node<T> last; private int N; class Node<T> { public T item; public Node<T> next; } public bool isEmpty() { return ((last == null) || (N == 0)); } public int size() { return N; } public void Enque(T item) { Node<T> lastOld = last; last = new Node<T>(); last.item = item; last.next = null; if (isEmpty()) first = last;else lastOld.next = last; N++; } public T Deque() { T item = first.item; first = first.next; if (isEmpty()) last = null; 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; } } } } |