设计算法,入栈和出栈的操作。具体实现要求: 1.写出栈的入栈和出栈算法。 2.写一个程序,将输入的十进
答案:2 悬赏:50 手机版
解决时间 2021-02-19 06:00
- 提问者网友:我的未来我做主
- 2021-02-18 16:19
设计算法,入栈和出栈的操作。具体实现要求: 1.写出栈的入栈和出栈算法。 2.写一个程序,将输入的十进
最佳答案
- 五星知识达人网友:举杯邀酒敬孤独
- 2021-02-18 16:45
楼主 ,这是我用链表结构实现的栈,以下是算法,顺序表部分没写,最近没什么时间,不好意思啦。。
#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如中所示
}
printf("the answer is:%d\n",sum);
}
#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
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
{sum=sum+(c-48)*pow(8,i);} //二进制pow(8,i)改为pow(2,i),十六进制用if else如中所示
}
printf("the answer is:%d\n",sum);
}
全部回答
- 1楼网友:空山清雨
- 2021-02-18 17:20
不好好学习......
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯