中北大学 算法与数据结构实验报告(6)

2019-04-23 22:43

{ cout<>S.flag; if ((S.flag!=1)&&(S.flag!=2)) //flag == 1 or 2 ? otherwise return 0 {cout<

int DuSqStackPop(DuSqStack &S,SElemType &x) { //S is a shared stack

//this function will pop the top element //from S.top1 or S.top2 by x

cout<>S.flag; if((S.flag!=1)&&(S.flag!=2)) //if S.flag!=1,2 then ERROR { cout<1) //if S1 is not empty then operate S1 {S.top1--; //modify stack top pointer S.top1 x=S.data[S.top1];//pop x

25

into DuSqStack }//end of if else {cout<<\ return(0); } break; case 2: if(S.top2

}//end of DuSqStackPop() function

void main() //main() function {

DuSqStack S; SElemType x=2000; //push x into DuSqStack S

cout<<\ InitDuSqStack(S); //To Initilaize DuSqStack

DuSqStackPush(S,x); //Test DuSqStackPush function twice

DuSqStackPush(S,x); //for example to push two element into DuSqStack DuSqStackPop(S,x); //Test DuSqStackPop function //and return by x getch(); }

26

10、实现由一定通用性的程序,实现一个四则运算表达式的求解

#include #include #include #include

#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int SElemType ; typedef struct SqStack { SElemType *base; SElemType *top; int stacksize; }SqStack;

int InitStack(SqStack &S) //InitStack() subfunction { //constructe a empty stack

S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if(!S.base) { cout<<\ return (0); } S.top =S.base ; S.stacksize =STACK_INIT_SIZE; cout<<\ return (1);

}//end of InitStack() function

int Push(SqStack &S,SElemType e) //Push() subfunction { //Push element to SqStack if(S.top-S.base >S.stacksize) //Stack == full?

27

{ S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT*sizeof(SElemType))); if(!S.base) { cout<<\ return(0); } S.top=S.base+S.stacksize; //To modify pointer of Stack top S.stacksize+=STACKINCREMENT; //To modify the size of stack } //end of if *S.top++=e; //S.top++ and push e into stack S return (1);

}//end of Push() subfunction

int StackEmpty(SqStack &S) //StackEmpty() function { //judge empty stack ? if(S.top==S.base) return(1); //empty return 1 else return(0); }

int Pop(SqStack &S,SElemType &e)

{ //if empty(S) then Error,otherwise Delete the S.top element //and return it by variable e if(S.top==S.base) { cout<<\ return (0); } e=*--S.top; //S.top-- and assign the values to e return(1); }

void Conversion() //Conversion() function {

SqStack S; //S is a SqStack SElemType N,e; InitStack(S); //constructe a empty stack S printf(\ scanf(\ while(N) { Push(S,N%8); N=N/8; }

printf(\ while(!StackEmpty(S)) //no empty then output { Pop(S,e);

28

printf(\ }

}//end of conversion() function

void main() //main() function {

cout<<\ Conversion();

cout<

}//end of main()

【实验结果】

1、写出实验的总结与体会,要简洁、真实、深刻;忌空话、套话

29


中北大学 算法与数据结构实验报告(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:考研数学历年真题(1987-2011)年数学一

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: