首页 > 开发 > 综合 > 正文

数据结构与算法(C#实现)系列---演示篇(二)

2024-07-21 02:19:58
字体:
来源:转载
供稿:网友


数据结构与算法(c#实现)系列---演示篇(二)

heavenkiller(原创)

public static void showgeneraltree_travel()

{

ienumerator tmpienum;

tree.traversaltype traveltype=0;



//---------------------提示----------------------------

console.writeline("please choose a the no. of a item you want to travel:");

console.writeline("1.breadthfirst----- 广度遍历");

console.writeline("2.predepthfirst-----前序遍历");

console.writeline("3.indepthfirst----中序遍历");

console.writeline("4.postdepthfirst----后序遍历");

switch(console.readline())

{

case "1"://show stack

traveltype=tree.traversaltype.breadth;

console.writeline("广度遍历");

break;

case "2"://sortedlist

traveltype=tree.traversaltype.predepth;

console.writeline("前序遍历");

break;

case "3":

traveltype=tree.traversaltype.indepth;

console.writeline("中序遍历");

break;

case "4":







traveltype=tree.traversaltype.postdepth;

console.writeline("后序遍历");

break;



default:

break;

}

//构造一棵广义树 generaltree

generaltree a=new generaltree("a");

generaltree b=new generaltree("b");

generaltree c=new generaltree("c");

generaltree d=new generaltree("d");

generaltree e=new generaltree("e");

generaltree f=new generaltree("f");

a.attacksubtree(b);

a.attacksubtree(c);

b.attacksubtree(d);

b.attacksubtree(e);

a.attacksubtree(f);

//show the operation

console.writeline("a.attacksubtree(b)");

console.writeline("a.attacksubtree(c)");

console.writeline("b.attacksubtree(d)");

console.writeline("b.attacksubtree(e)");

console.writeline("a.attacksubtree(f)");

//--------------------------------------------------------



a.settraversaltype(traveltype);//设置遍历类型

tmpienum=a.getenumerator();



//console.writeline("begin to depthfist travel:");



while(tmpienum.movenext())

{

console.writeline(tmpienum.current.tostring());

}





}



public static void showstack_rpncalculator()

{

//read a expression string and push every character into the stack in queue.

console.writeline("this is performance for stack,you can input a string like this '123*+',then this subprogramme can compute it and get the result '7',this is rpn calculator. ");

console.writeline("please input a expression string:");

string strexpression=console.readline();

char [] tmpchars=strexpression.tochararray(0,strexpression.length);

stack stackrpn=new stack();







int numa,numb;

foreach(char tmp in tmpchars)

{

switch (tmp)

{



case '*':

numa=(int)stackrpn.pop();

numb=(int)stackrpn.pop();

stackrpn.push(numa*numb);

break;

case '+':

numa=(int)stackrpn.pop();

numb=(int)stackrpn.pop();

stackrpn.push(numa+numb);

break;

default:

stackrpn.push(int32.parse(tmp.tostring()));

break;



}







}

console.writeline("the result is:{0}",stackrpn.pop().tostring());





}





发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表