欢迎光临
我们一直在努力

C# 堆栈实现

建站超值云服务器,限时71元/月

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/

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C# 堆栈实现
分享到: 更多 (0)