Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
generic stack implementation
//Generic Stack //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { MyStack<int> myStack = new MyStack<int>(5); // default constructor will initialize the stack for 100 capacity. Another constructor to take an array will be nice. myStack.push(1); Assert(1 == myStack.pop(), "First item pop"); try { myStack.pop(); Assert(false, "Exception was not thrown when stack is empty."); } catch (Exception) //use an existing exception like StackIsEmpty if available or create a custom one { Assert(true, "Exception was thrown correctly when stack is empty."); } myStack.push(1); myStack.push(2); myStack.push(3); Assert(3 == myStack.pop(), "last in first out"); myStack.push(3); myStack.push(4); myStack.push(5); try { myStack.push(6); Assert(false, "Exception was not thrown when stack is full."); } catch (StackOverflowException) { Assert(true, "Exception was thrown correctly when stack is full."); } } public static void Assert(bool result, string message) { Console.WriteLine("ASSERT: {0} - {1}", result.ToString(), message); } public class MyStack<T> { T[] _items = null; int _currentIndex = 0; public MyStack() : this(100){} public MyStack(int capacity) { _items = new T[capacity]; } public void push(T item) { if (_currentIndex >= _items.Length) { throw new StackOverflowException();} _items[_currentIndex] = item; _currentIndex++; } public T pop() { if (_currentIndex <= 0) { throw new Exception(); } _currentIndex--; Console.WriteLine("current index: {0}", _currentIndex); return _items[_currentIndex]; } } } }
run
|
edit
|
history
|
help
0
PYRAMIDPATTERN
code
Evo Ivana da budes fancy :D
Shalini_CSG_CodeTest
12
4
rekenmachine in c#
Fórum ➡ GroupJoin'ing Books and Orders ( with Zip'ped output ) ♦
minimal path for triangle
1.