首页 > 其他 > 详细

数据结构实验二

时间:2019-11-10 14:51:48      阅读:69      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <stdlib.h>
int STACK_INIT_SIZE=100;
int STACKINCREMENT=10;
typedef struct{
 int *base;
 int *top;
 int stacksize;
}sqstack;
void initstack(sqstack &S){
 S.base=(int * )malloc(STACK_INIT_SIZE*sizeof(int));
 if(!S.base)exit(1);
 S.top=S.base;
 S.stacksize=STACK_INIT_SIZE;
}
int judge(sqstack S){
 if(S.top==S.base)
  return 0;
 else
  return 1;
}
void push(sqstack &S,int e){
 if(S.top-S.base>=S.stacksize){
  S.base=(int*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
  if(!S.base)exit(1);
  S.top=S.base+S.stacksize;
  S.stacksize+=STACKINCREMENT;
 }
 *S.top++=e;
}
void pop(sqstack &S,int &e){
 if(S.top==S.base)
  printf("error");
 e=*--S.top;
}
void main(){
 printf("数据结构实验二数制转换\n");
 sqstack s;
 int n;
 initstack(s);
 printf("输入一个十进制数\n");
 scanf("%d",&n);
 while(n){
  push(s,n%8);
  n/=8;
 }
 while(judge(s)){
  pop(s,n);
  printf("%d",n);
 }
}
 
技术分享图片

 

数据结构实验二

原文:https://www.cnblogs.com/P201821430020/p/11829703.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!