#include<stdio.h> #include<stdlib.h> #define OVERFLOW -1 #define ERROR 0 #define FALSE 0 #define TRUE 1 #define OK 1 typedef int ElemType; typedef int Status;
typedef struct{
ElemType *elem; //存储空间的基址
int top; //栈顶元素的下一个元素,简称栈顶位标
int size; //当前分配的存储容量,作用看入栈操作就可以知道
int increment; //扩容时,增加的存储容量,作用看入栈操作就可以知道
} SqStack; //顺序栈名称
Status InitStack_Sq(SqStack &S, int size, int inc){ //接受3个参数,&S是对结构体的引用
S.elem = (ElemType*)malloc(size*sizeof(ElemType)); //分配存储空间
if(S.elem == NULL) return OVERFLOW; //判断上一步分配存储空间是否成功
S.top = 0; //置S为空栈,S.top为0即表示栈为空栈
S.size = size; //栈的空间初始容量值
S.increment = inc; //栈的空间初始增量值(如果需要扩容)
return OK; //上面的执行正常,返回OK
}
Status StackEmpty_Sq(SqStack S){
if(S.top == 0)
return TRUE;
else
return FALSE;
}
//空栈的决断是,如果栈为空就返回1,否则就返回0,当然可以不这样规定;
//至于为什么要做空栈的判断,自然是有原因的,下面再看程序的代码时就可以知道了。
Status Push_Sq(SqStack &S, ElemType e){ //将元素e压入栈,这里e只是一个形参而已
ElemType *newbase; //定义中间变量
if(S.top>= S.size){ //S.top如果指向最后一个不存储元素的地址时,即S.top大于
newbase = (ElemType*)realloc(S.elem, //等于S.size时,就表示栈满了
(S.size + S.increment)*sizeof(ElemType)); //通过realloc动态扩容
if(NULL == newbase) return OVERFLOW; //判断扩容是否成功
S.elem = newbase; //扩容成功后才将中间变量的值指向S.elem,防止扩容失败时,
S.size = S.size + S.increment; //S.elem指向一个不是原来的位置
}
S.elem[S.top] = e; //将e元素入栈
S.top++; //使S.top加1,表示指向的是栈顶位标
return OK; //上面操作正常后返回1
}
Status Pop_Sq(SqStack &S, ElemType &e){ //栈顶元素出栈,赋给元素e
if(0 == S.top) return ERROR;
e = S.elem[--S.top]; //e出栈,并将S.top减1
return OK;
}
void Converstion(int N){
SqStack S;
ElemType e;
InitStack_Sq(S, 10, 5); //栈S的初始容量置为10,每次扩容容量为5
while(N != 0){
Push_Sq(S, N%8); //将N除以8的余数入栈
N /= 8; //N取值为其除以8的商
} //理论基础为除8取余法
while(StackEmpty_Sq(S) == FALSE){
Pop_Sq(S, e); //依次输出栈中的余数,并赋给元素e
printf("%d", e); //打印元素
}
int main(void){
printf("Enter a number:");scanf("%d",&num);
Converstion(num);
printf("\n");
}
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -1
#define ERROR 0
#define FALSE 0
#define TRUE 1
#define OK 1
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType *elem;
int top;
int size;
int increment;
} SqStack;
Status InitStack_Sq(SqStack &S, int size, int inc){
S.elem = (ElemType*)malloc(size*sizeof(ElemType));
if(S.elem == NULL) return OVERFLOW;
S.top = 0;
S.size = size;
S.increment = inc;
return OK;
}
Status StackEmpty_Sq(SqStack S){
if(S.top == 0)
return TRUE;
else
return FALSE;
}
Status Push_Sq(SqStack &S, ElemType e){
ElemType *newbase;
if(S.top>= S.size){
newbase = (ElemType*)realloc(S.elem,
(S.size + S.increment)*sizeof(ElemType));
if(NULL == newbase) return OVERFLOW;
S.elem = newbase;
S.size = S.size + S.increment;
}
S.elem[S.top] = e;
S.top++;
return OK;
}
Status Pop_Sq(SqStack &S, ElemType &e){
if(0 == S.top) return ERROR;
e = S.elem[--S.top];
return OK;
}
void Converstion(int N){
SqStack S;
ElemType e;
InitStack_Sq(S, 10, 5);
while(N != 0){
Push_Sq(S, N%8);
N /= 8;
}
while(StackEmpty_Sq(S) == FALSE){
Pop_Sq(S, e);
printf("%d", e);
}
}
int main(void){
int num;
printf("Enter a number:");scanf("%d",&num);
Converstion(num);
printf("\n");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有