坚持就是胜利,加油吧自己
前期数据准备:
#define maxsize 100
typedef int elemtype;
typedef struct stack_type
{
elemtype stack[maxsize];
int top;
}stacktype;
使用函数:
//initial
void initial(stacktype *p)
{
p->top = -1;//Why is top equal to -1?
}
//Insert elem;
bool push(stacktype *p,elemtype a)
{
if (p->top >= maxsize - 1)
{
printf("This stack IS FULL! Please find another stack to finish your option!!!\n");
return false;
}
else
{
p->top++;
p->stack[p->top] = a;
return true;
}
}
//Let the top elem get out of the stack
elemtype pop(stacktype *p)
{
if (p->top < 0)
{
printf("This stack IS EMPTY!Please find another stack to finish your option!!!\n");
return(NULL);
}
else
{
p->top--;
return(p->stack[p->top + 1]);
}
}
使用该栈解决的问题:简单的数字进制转换
我的代码:
#include <cstdio>
#include <iostream>
using namespace std;
#define maxsize 100
typedef int elemtype;
typedef struct stack_type
{
elemtype stack[maxsize];
int top;
}stacktype;
//
void initial(stacktype *p)
{
p->top = -1;//Why is top equal to -1?
}
//Insert elem;
bool push(stacktype *p,elemtype a)
{
if (p->top >= maxsize - 1)
{
printf("This stack IS FULL! Please find another stack to finish your option!!!\n");
return false;
}
else
{
p->top++;
p->stack[p->top] = a;
return true;
}
}
//Let the top elem get out of the stack
elemtype pop(stacktype *p)
{
if (p->top < 0)
{
printf("This stack IS EMPTY!Please find another stack to finish your option!!!\n");
return(NULL);
}
else
{
p->top--;
return(p->stack[p->top + 1]);
}
}
int main(void)
{
stacktype s,*p;
p = &s;
//Now here is a simple question ;
elemtype num = 12;
int choice; bool flag;
scanf_s("%d",&choice);
initial(p);
do
{
flag = push(p, (num%choice));
} while ((num/=choice)&&flag);
printf("\nNow the ans is ");
while (p->top >= 0)
{
printf("%d",pop(p));
}
cout << endl;
return 0;
}
输出结果