namespace stack{
using system;
public class stack
{
private node first = null;
private int count = 0;
/***********************************************property procedures do not accept any parameters. note the
diff in the function definition (no parenthesis)************************************************/
public bool empty
{/*******************************************property
getprocedure********************************************/
get
{
return (first == null);
}
}
public int count
{/*******************************************property get
procedure********************************************/
get
{ return count; } }
public object pop()
{
if (first == null)
{
throw new invalidoperationexception ("cant pop from an empty stack");
}
else
{
object temp = first.value;
first = first.next;
count–;
return temp;
}
}
public void push(object o)
{
first = new node(o, first);
count++;
}
class node
{
public node next;
public object value;
public node(object value) :
this(value, null) {}
public node(object value, node next)
{
next = next;
value = value;
}
}}
class stacktest{
static void main()
{ stack s = new stack();
if (s.empty)
console.writeline("stack is empty");
else
console.writeline("stack is not empty");
for (int i = 0; i < 5; i++)
s.push(i);
console.writeline("items in stack {0}", s.count);
for (int i = 0; i < 5; i++)
console.writeline("popped item is {0} and the count is {1}", s.pop(), s.count);
s = null;
}
}}//*********end of code
//asphouse http://asphouse.yeah.net/
