Методы с string очередью - разобрать код - C#
Формулировка задачи:
Не могу понять как отрабатываю два последних метода. Помогите срочно пожалуйста.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FIFO { class Program { static void Main(string[] args) { try { Queue q1 = new Queue(); q1.Add(4); q1.Add(5); Console.WriteLine("В передней части {0}", q1.Head()); q1.Add(6); Console.WriteLine("Удаление из очереди {0}", q1.Remove()); Console.WriteLine("Очередь имеет размер {0}", q1.size); Console.WriteLine("Очередь пустая? {0}", q1.IsEmpty()); q1.Remove(); } catch (Exception e) { Console.WriteLine(e); } Console.ReadKey(); } public class Queue { private string[] data; public int size; private int front = -1; private int back = 0; private int count = 0; public Queue() { size = 10; data = new string[size]; } public Queue(int size) { this.size = size; data = new string[size]; } public bool IsEmpty() { return count == 0; } public bool IsFull() { return count == size; } public void Add(int i) { if (IsFull()) throw new ApplicationException("Очередь заполнена"); else { count++; data[back++ % size] = Convert.ToString(i); } } public string Remove() { if (IsEmpty()) throw new ApplicationException("Очередь пуста"); else { count--; return data[++front % size]; } } public string Head() { if (IsEmpty()) { throw new ApplicationException("Очередь пуста"); } else return data[(front + 1) % size]; } } } }
Решение задачи: «Методы с string очередью - разобрать код»
textual
Листинг программы
namespace System.Collections { using System; using System.Security.Permissions; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; [DebuggerTypeProxy(typeof(System.Collections.Queue.QueueDebugView))] [DebuggerDisplay("Count = {Count}")] [System.Runtime.InteropServices.ComVisible(true)] [Serializable] public class Queue : ICollection, ICloneable { private Object[] _array; private int _head; private int _tail; private int _size; private int _growFactor; private int _version; [NonSerialized] private Object _syncRoot; private const int _MinimumGrow = 4; private const int _ShrinkThreshold = 32; . public Queue() : this(32, (float)2.0) { } public Queue(int capacity) : this(capacity, (float)2.0) { } public Queue(int capacity, float growFactor) { if (capacity < 0) throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (!(growFactor >= 1.0 && growFactor <= 10.0)) throw new ArgumentOutOfRangeException("growFactor", Environment.GetResourceString("ArgumentOutOfRange_QueueGrowFactor", 1, 10)); Contract.EndContractBlock(); _array = new Object[capacity]; _head = 0; _tail = 0; _size = 0; _growFactor = (int)(growFactor * 100); } public Queue(ICollection col) : this((col==null ? 32 : col.Count)) { if (col==null) throw new ArgumentNullException("col"); Contract.EndContractBlock(); IEnumerator en = col.GetEnumerator(); while(en.MoveNext()) Enqueue(en.Current); } public virtual int Count { get { return _size; } } public virtual Object Clone() { Queue q = new Queue(_size); q._size = _size; int numToCopy = _size; int firstPart = (_array.Length - _head < numToCopy) ? _array.Length - _head : numToCopy; Array.Copy(_array, _head, q._array, 0, firstPart); numToCopy -= firstPart; if (numToCopy > 0) Array.Copy(_array, 0, q._array, _array.Length - _head, numToCopy); q._version = _version; return q; } public virtual bool IsSynchronized { get { return false; } } public virtual Object SyncRoot { get { if( _syncRoot == null) { System.Threading.Interlocked.CompareExchange(ref _syncRoot, new Object(), null); } return _syncRoot; } } public virtual void Clear() { if (_head < _tail) Array.Clear(_array, _head, _size); else { Array.Clear(_array, _head, _array.Length - _head); Array.Clear(_array, 0, _tail); } _head = 0; _tail = 0; _size = 0; _version++; } public virtual void CopyTo(Array array, int index) { if (array==null) throw new ArgumentNullException("array"); if (array.Rank != 1) throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); if (index < 0) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); Contract.EndContractBlock(); int arrayLen = array.Length; if (arrayLen - index < _size) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); int numToCopy = _size; if (numToCopy == 0) return; int firstPart = (_array.Length - _head < numToCopy) ? _array.Length - _head : numToCopy; Array.Copy(_array, _head, array, index, firstPart); numToCopy -= firstPart; if (numToCopy > 0) Array.Copy(_array, 0, array, index+_array.Length - _head, numToCopy); } public virtual void Enqueue(Object obj) { if (_size == _array.Length) { int newcapacity = (int)((long)_array.Length * (long)_growFactor / 100); if (newcapacity < _array.Length + _MinimumGrow) { newcapacity = _array.Length + _MinimumGrow; } SetCapacity(newcapacity); } _array[_tail] = obj; _tail = (_tail + 1) % _array.Length; _size++; _version++; } public virtual IEnumerator GetEnumerator() { return new QueueEnumerator(this); } public virtual Object Dequeue() { if (Count == 0) throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyQueue")); Contract.EndContractBlock(); Object removed = _array[_head]; _array[_head] = null; _head = (_head + 1) % _array.Length; _size--; _version++; return removed; } public virtual Object Peek() { if (Count == 0) throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyQueue")); Contract.EndContractBlock(); return _array[_head]; } [HostProtection(Synchronization=true)] public static Queue Synchronized(Queue queue) { if (queue==null) throw new ArgumentNullException("queue"); Contract.EndContractBlock(); return new SynchronizedQueue(queue); } public virtual bool Contains(Object obj) { int index = _head; int count = _size; while (count-- > 0) { if (obj == null) { if (_array[index] == null) return true; } else if (_array[index] != null && _array[index].Equals(obj)) { return true; } index = (index + 1) % _array.Length; } return false; } internal Object GetElement(int i) { return _array[(_head + i) % _array.Length]; } public virtual Object[] ToArray() { Object[] arr = new Object[_size]; if (_size==0) return arr; if (_head < _tail) { Array.Copy(_array, _head, arr, 0, _size); } else { Array.Copy(_array, _head, arr, 0, _array.Length - _head); Array.Copy(_array, 0, arr, _array.Length - _head, _tail); } return arr; } private void SetCapacity(int capacity) { Object[] newarray = new Object[capacity]; if (_size > 0) { if (_head < _tail) { Array.Copy(_array, _head, newarray, 0, _size); } else { Array.Copy(_array, _head, newarray, 0, _array.Length - _head); Array.Copy(_array, 0, newarray, _array.Length - _head, _tail); } } _array = newarray; _head = 0; _tail = (_size == capacity) ? 0 : _size; _version++; } public virtual void TrimToSize() { SetCapacity(_size); } [Serializable] private class SynchronizedQueue : Queue { private Queue _q; private Object root; internal SynchronizedQueue(Queue q) { this._q = q; root = _q.SyncRoot; } public override bool IsSynchronized { get { return true; } } public override Object SyncRoot { get { return root; } } public override int Count { get { lock (root) { return _q.Count; } } } public override void Clear() { lock (root) { _q.Clear(); } } public override Object Clone() { lock (root) { return new SynchronizedQueue((Queue)_q.Clone()); } } public override bool Contains(Object obj) { lock (root) { return _q.Contains(obj); } } public override void CopyTo(Array array, int arrayIndex) { lock (root) { _q.CopyTo(array, arrayIndex); } } public override void Enqueue(Object value) { lock (root) { _q.Enqueue(value); } } [SuppressMessage("Microsoft.Contracts", "CC1055")] public override Object Dequeue() { lock (root) { return _q.Dequeue(); } } public override IEnumerator GetEnumerator() { lock (root) { return _q.GetEnumerator(); } } [SuppressMessage("Microsoft.Contracts", "CC1055")] public override Object Peek() { lock (root) { return _q.Peek(); } } public override Object[] ToArray() { lock (root) { return _q.ToArray(); } } public override void TrimToSize() { lock (root) { _q.TrimToSize(); } } } [Serializable] private class QueueEnumerator : IEnumerator, ICloneable { private Queue _q; private int _index; private int _version; private Object currentElement; internal QueueEnumerator(Queue q) { _q = q; _version = _q._version; _index = 0; currentElement = _q._array; if (_q._size == 0) _index = -1; } public Object Clone() { return MemberwiseClone(); } public virtual bool MoveNext() { if (_version != _q._version) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumFailedVersion)); if (_index < 0) { currentElement = _q._array; return false; } currentElement = _q.GetElement(_index); _index++; if (_index == _q._size) _index = -1; return true; } public virtual Object Current { get { if (currentElement == _q._array) { if (_index == 0) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted)); else throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded)); } return currentElement; } } public virtual void Reset() { if (_version != _q._version) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumFailedVersion)); if (_q._size == 0) _index = -1; else _index = 0; currentElement = _q._array; } } internal class QueueDebugView { private Queue queue; public QueueDebugView( Queue queue) { if( queue == null) throw new ArgumentNullException("queue"); Contract.EndContractBlock(); this.queue = queue; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public Object[] Items { get { return queue.ToArray(); } } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д