数据结构栈的程序.docx
- 文档编号:29917469
- 上传时间:2023-08-03
- 格式:DOCX
- 页数:15
- 大小:16.60KB
数据结构栈的程序.docx
《数据结构栈的程序.docx》由会员分享,可在线阅读,更多相关《数据结构栈的程序.docx(15页珍藏版)》请在冰豆网上搜索。
数据结构栈的程序
#include
#include
#defineinitsize20
#defineincrement10
typedefcharelemtype;
typedefstructstack{
elemtype*base;
elemtype*top;
intstacksize;
}stack;
voidinitstack(stack&s)//初始化栈
{s.base=(elemtype*)malloc(initsize*sizeof(elemtype));
if(!
s.base)
exit(0);
s.stacksize=initsize;
s.top=s.base;}
voidpush(stack&s,elemtypee)//入栈,栈满则追加栈空间
{if(s.top-s.base>=initsize)
s.base=(elemtype*)realloc(s.base,(s.stacksize+increment)*sizeof(elemtype));
if(!
s.base)
exit(0);
s.stacksize+=increment;
*s.top=e;
s.top++;
}
voidpop(stack&s,elemtype&e)//出栈
{if(s.base==s.top)
printf("有错误");
else
e=*--s.top;
}
intempty(stack&s)//判栈空
{if(s.base==s.top)
return1;
elsereturn0;
}
voidclear(stack&s)
{if(s.base==s.top)
return;
s.base=s.top;
}
voidconversion(intn,intN)//进制转换
{intc;
chare;
stacks;
initstack(s);
while(n!
=0)
{c=n%N;
n=n/N;
if(c<10)
*s.top++='0'+c;
else
{*s.top++='A'+c-10;
}
}
while(!
empty(s))
{pop(s,e);
printf("%c",e);
}
clear(s);
}
voidmain()
{
intn,N;
printf("请输入十进制数:
");
scanf("%d",&n);
printf("请输入需要转化进制:
");
scanf("%d",&N);
printf("转化后的数是:
");
conversion(n,N);
}
#include
usingnamespacestd;
#defineSTACK_INIT_SIZE1000
#defineSTACKINCREMENT100
typedefstruct
{
int*base;
int*top;
intstacksize;
}SqStack;
SqStackS;
voidInitStack(SqStack&S)
{
S.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!
S.base)cout<<"OVERFLOW"< S.top=S.base; S.stacksize=STACK_INIT_SIZE; }//初始化 voidPush(SqStack&S,inte) { if(S.top-S.base>=S.stacksize) { S.base=(int*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int)); if(! S.base)cout<<"error"< S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; *S.top=e; S.top=S.top+1; }//入栈 voidPop(SqStack&S,inte) { if(S.top==S.base)cout<<"ERROR"< e=*--S.top; S.top=S.top-1; e=*S.top; cout< }//出栈 intStackEmpty(SqStack&S) { if(S.base==S.top)return1; elsereturn0; }//栈空定义 voidconversion() { intN,e; InitStack(S); cout<<"请输入一个十进制数: "; cin>>N; while(N) { Push(S,N%16); N=N/16; } while(! StackEmpty(S)) { Pop(S,e); if(e>9)printf("%d",e+55); elseprintf("%d",e+48); } cout< }//进制转换 intmain() { conversion(); return0; } conversion()里面的变量e没有初始化 把这几个地方改下: //出栈 voidPop(SqStack&S,int&e) { if(S.top==S.base)cout<<"ERROR"< e=*--S.top; S.top=S.top-1; e=*S.top; //cout< } //主函数里面控制格式输出的那个地方 %d改成%c //所以最好在主函数里面将e初始化为0.这样即使你的e在后面没有初始化,程序运行的结果你也可以预知...这是个好习惯 #include #defineMAXSIZE1024栈可能达到的最大容量 typedefintDataType; typedefstruct {DataTypedata[MAXSIZE]; inttop; }SeqStack; SeqStack*initSeqStack()栈初始化 {SeqStack*s; s=(SeqStack*)malloc(sizeof(SeqStack)); s->top=-1; returns; } intempty(SeqStack*s)判栈空 {if(s->top==-1) return1; else return0; } intpush(SeqStack*s,DataTypex)入栈 {if(s->top==MAXSIZE-1) {printf("overflow"); return0; } s->top++; s->data[s->top]=x; return1; } voidpop(SeqStack*s)出栈 {s->top--; } DataTypetop(SeqStack*s)读栈 {return(s->data[s->top]); } voidconvert(intN,intr,intm)转换进制运算 {SeqStack*s; intx; charc; s=ininSeqStack(); while(N) {push(s,N%r); N=N/r; } while(! empty(s)) {x=top(s); pop(s); printf("%d",x); } s=top; free(s); returntop; while(N) {push(s,N%m); N=N/m; } while(! empty(s)) {x=top(s); pop(s); if(x<10) c=y+48; else c=y-10+'A'; printf("%c",c); } } intmain() {SeqStack*SeqS; intN,r,m; intx; printf("PleaseEnteranumber: "); scanf("%d",&N); r=8; m=16; SeqS=initSeqStack(); empty(SeqS); x=10; pop(SeqS); top(SeqS); pop(SeqS); convert(N,r,m); getch(); } #include #include #definemaxsize50 typedefintelemtype; typedefstruct { elemtypedata[50]; inttop; }sqstack;/*顺序栈类型定义*/ voidinitstack(sqstack*s)/*建立空栈*/ { s=(sqstack*)malloc(sizeof(sqstack)); s->top=-1; } intstacklength(sqstack*s)/*求栈长*/ { return(s->top+1); } elemtypegettop(sqstack*s)/*取栈顶元素*/ { elemtypex; if(s->top==-1)return0; x=s->data[s->top]; returnx; } intpush(sqstack*s,elemtypex)/*在栈顶插入新元素*/ { s->top++; s->data[s->top]=x; return1; } intpop(sqstack*s)/*删除栈顶元素*/ { if(s->top==-1)return0; elses->top--; return1; } intstackempty(sqstack*s)/*判断栈是否为空*/ { if(s->top==-1)return1; elsereturn0; } voidvisit(sqstack*s) { inti; elemtypex; i=s->top; while(i! =-1) {x=s->data[i]; printf("%3d",x); i--; } printf("\n"); } voidclearstack(sqstack*s) { s->top=-1; } voidconvert(intn,inte)/*将十进制数n转换成e进制数并输出*/ { inti; sqstack*s; initstack(s); while(n) { i=n%e; n=n/e; push(s,i); } printf("thenumberafterconversion: \n"); while(! stackempty(s)) { printf("%d",gettop(s)); pop(s); } } voidmain() { inti,j; clrscr(); printf("Enterthenumberyouwanttoconvert: "); scanf("%d",&i); printf("Enterthehexadecimal: "); scanf("%d",&j); convert(i,j); getch(); } int*Init() { int*top,*base; base=(int*)malloc(sizeof(int)*50); if(! base){printf("Error! ");exit();} top=base; returntop; } int*push(int*top,intn) { *top=n; top++; returntop; } intpop(int*top) { inte; top--; e=*top; returne; } voidconvs() { int*top,*base; inte,N; inti; top=Init(); base=top; printf("Inputthenumber: \n"); scanf("%d",&N); while(N! =0) { top=push(top,N%8); N=N/8; } printf("Afterchange,thenumberis: \n"); while(top! =base) { e=pop(top); top--; printf("%d",e); } printf("\n"); } main() { convs(); getch(); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序