并写出出栈和入栈算法2创建一个循环?
这是我用链表结构实现的栈,以下是算法,顺序表部分没写,最近没什么时间,不好意思啦。。
#include
#include
#include
#define Max_stack_size 20
#define Addersize 10
typedef char Elemtype;
typedef struct{
Elemtype *base;
Elemtype *top;
int stacksize;
}sqStack;
void initStack (sqStack *s){ //初始化一个空栈
s->base=(Elemtype *)malloc(Max_stack_size*sizeof(Elemtype));
if(!s->base) exit(0);
s->top=s->base;
s->stacksize=Max_stack_size;
}
void pushStack(sqStack *s,Elemtype e){ //入栈操作
if(s->top-s->base>=s->stacksize)
{s->base=(Elemtype *)realloc(s->base,(s->stacksize+Addersize)*sizeof(Elemtype));
if(!s->base) exit(0);
s->top=s->base+s->stacksize;
s->stacksize=s->stacksize+Addersize;}
*(s->top)=e;
s->top++;
}
void popStack(sqStack *s,Elemtype *e){ //出栈操作
if(s->top==s->base) return;
*e=*--(s->top);
}
void clearStack(sqStack *s) //清空栈
{
s->top=s->base;
}
void destroyStack(sqStack *s){ //销毁栈
int i;
int len;
len=s->stacksize;
for(i=0;i {free(s->base); s->base++; } s->base=NULL; s->top=s->base; s->stacksize=0; } int counterStack(sqStack s) {return (s.top-s.base);} void main() { sqStack p; Elemtype c; int i,sum,length; sum=0; printf("initial:\n"); initStack(&p); printf("push the 8 scale:\n"); scanf("%c",&c); //输入数据时不能隔开,否则答案错误,空格也算字符 while(c!='#') {pushStack(&p,c); scanf("%c",&c); }getchar(); length=counterStack(p); //有错时更容易检测到。。 printf("numbers'length:%d\n",length); for(i=0;i {popStack(&p,&c); {sum=sum+(c-48)*pow(8,i);} //二进制pow(8,i)改为pow(2,i),十六进制用if else如/**/中所示 } /* if('0' else if('A' else if('a' printf("the answer is:%d\n",sum); }