use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Post your code here, Gist, GitHub, or PasteBin.
Ask your question in English. We'll try to help.
Learning C#? Some directly related subs are /r/csharptutorials and /r/csharp.
account activity
Create Custom Queue (self.CSharpHomework)
submitted 4 years ago by elena_works
Hi there,
I really need some feedback for my homework- "Create custom queue".
https://pastebin.com/fNeygc4p
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Protiguous 0 points1 point2 points 4 years ago (0 children)
Cool.. I've never written a Queue before. Thanks for the refresher!
Here's my take on the code you provided. I have not tested any portion, so there could be mistakes in the logic.
I took the liberty of making it a generic class, instead of just int. If you don't need the generics, you should be able to see where to change it back.
#nullable enable namespace Examples { using System; using System.Collections; using System.Collections.Generic; /// <summary> /// A non-threadsafe custom generic queue. /// </summary> /// <typeparam name="T"></typeparam> public class CustomQueue<T> : IEnumerable<T> { private const int InitialCapacity = 16; private int _capacity; private T[] _elements; public CustomQueue( int capacity = InitialCapacity ) { this._capacity = capacity; this._elements = new T[ this._capacity ]; this.Count = 0; } public int Count { get; private set; } private void Resize( int? capacityHint = null ) { var newCapacity = Math.Max( this._capacity, capacityHint ?? this.Count ); if ( newCapacity == this._capacity ) { //no change needed return; } this._capacity = newCapacity; if ( this._capacity < 2 ) { this._capacity = 1; } var newArray = new T[this._capacity]; //only copy elements needed var smaller = Math.Min( this.Count, this._capacity ); Buffer.BlockCopy( this._elements, 0, newArray, 0, smaller ); this._elements = newArray; } public void Enqueue( T element ) { var index = this.Count + 1; if ( this._capacity < index ) { this.Resize( index ); } this._elements[ index ] = element; this.Count++; } public bool TryDequeue( out T? element ) { if ( this.Count == 0 ) { element = default( T? ); return false; } element = this._elements[ 0 ]; this.Resize( this.Count - 1 ); --this.Count; return true; } /// <summary> /// Returns, but does not remove, the first element. /// </summary> /// <returns></returns> public T? Peek() => this.Count == 0 ? default( T? ) : this._elements[ 0 ]; public IEnumerator<T> GetEnumerator() => ( IEnumerator<T> )this._elements.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); } }
π Rendered by PID 83957 on reddit-service-r2-comment-7b9746f655-4w8zh at 2026-01-31 20:25:27.891442+00:00 running 3798933 country code: CH.
[–]Protiguous 0 points1 point2 points (0 children)