数据结构 利用栈进行数制转换的实现

程序如下:(当程序执行后,比如数个1348,结果无限返回2,,是什么问题呢,,跪求大佬。)
#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int ElemType;
typedef int Status;
using namespace std;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
Status initStack(SqStack &S)
{
S.base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!S.base)
{
cout << “内存分配失败!” << endl;
exit(0);
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
//判断栈是否为空
}
Status push(SqStack &S, ElemType e)
{
if (S.top - S.base >= S.stacksize)
{
S.base = (ElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType));
if (!S.base)
{
exit(0);
}
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return 1;
}//插入元素e为栈顶元素
Status StackEmpty(SqStack S)
{
if (S.top == S.base)
return 1;
else
return 0;
}

Status pop(SqStack S, ElemType &e)
{
if (S.base == S.top)
{
cout << “栈为空!” << endl;
return 0;
}
e = *–S.top;
return 1;
}//删除栈顶元素,并返回
int main()
{
ElemType e;
SqStack S;
initStack(S);
int N;
cin >> N;
while (N)
{
push(S, N % 8);
N /= 8;
}
while (!StackEmpty(S))
{
pop(S, e);
cout << e;
}
cout << endl;
system(“pause”);
return 1;
}

猜你喜欢

转载自blog.csdn.net/qq_44713855/article/details/89287872