Created
August 29, 2010 08:57
-
-
Save hoheinzollern/556119 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Stack | |
{ | |
public class StackEmpty : Exception { } | |
public class Stack | |
{ | |
private object[] arr; | |
private int nextFree; | |
public Stack(int len) | |
{ | |
if (len > 0) | |
arr = new object[len]; | |
else | |
arr = null; | |
nextFree = 0; | |
} | |
public bool IsEmpty | |
{ | |
get | |
{ | |
return nextFree == 0; | |
} | |
} | |
public void Push(object x) | |
{ | |
if (arr == null) | |
arr = new object[10]; | |
if (nextFree >= arr.Length) | |
{ | |
Array.Resize<object>(ref arr, arr.Length * 2); | |
} | |
arr[nextFree] = x; | |
nextFree = nextFree + 1; | |
} | |
public object Pop() | |
{ | |
if (nextFree > 0) | |
{ | |
nextFree = nextFree - 1; | |
object x = arr[nextFree]; | |
arr[nextFree] = null; | |
return x; | |
} | |
else | |
{ | |
throw new StackEmpty(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment