并写出出栈和入栈算法2创建一个循环

更新时间:01-24 教程 由 浅殇 分享

并写出出栈和入栈算法2创建一个循环?

这是我用链表结构实现的栈,以下是算法,顺序表部分没写,最近没什么时间,不好意思啦。。

#include

#include //包括malloc()和realloc()函数的头文件

#include //包括pow()函数的头文件

#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);

}

声明:关于《并写出出栈和入栈算法2创建一个循环》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/tutorial/14_2195385.html