Skip to content

Instantly share code, notes, and snippets.

@hoheinzollern
Created August 29, 2010 08:57
Show Gist options
  • Save hoheinzollern/556119 to your computer and use it in GitHub Desktop.
Save hoheinzollern/556119 to your computer and use it in GitHub Desktop.
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