代码:seqstack.h
#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__
#define MAX 20
typedef int datatype;
typedef struct
{
datatype data[MAX]; //存储栈的数组
int top; //记录栈顶元素的下标
}seqStack;
//创建
seqStack * stack_create();
//判空
int stack_empty(seqStack *S);
//判满
int stack_full(seqStack *S);
//入栈,压栈,进栈
void stack_push(seqStack *S, datatype e);
//遍历
void stack_show(seqStack *S);
//出栈,弹栈
void stack_pop(seqStack *S);
#endif
seqstack.c
#include"seqstack.h"
#include<stdlib.h>
#include<stdio.h>
//创建
seqStack * stack_create()
{
seqStack *S = (seqStack *)malloc(sizeof(seqStack));
if(NULL==S)
{
printf("创建失败\n");
return NULL;
}
//初始化
S->top = -1;
printf("创建成功\n");
return S;
}
//判空
int stack_empty(seqStack *S)
{
return S->top == -1; //1代表空,0代表非空
}
//判满
int stack_full(seqStack *S)
{
return S->top == MAX-1; //1代表满,0代表不满
}
//入栈,压栈,进栈
void stack_push(seqStack *S, datatype e)
{
//判断逻辑
if(NULL==S || stack_full(S))
{
printf("入栈失败\n");
return ;
}
//入栈逻辑
S->top++;
S->data[S->top] = e;
printf("入栈成功\n");
}
//遍历
void stack_show(seqStack *S)
{
//判断逻辑
if(NULL==S || stack_empty(S))
{
printf("便利失败\n");
return ;
}
//遍历逻辑
printf("从栈顶元素到栈底元素分别是:");
for(int i=S->top; i>=0; i--)
{
printf("%d\t",S->data[i]);
}
printf("\n");
}
main.c
#include"seqstack.h"
#include<stdlib.h>
#include<stdio.h>
int main(int argc, const char *argv[])
{
seqStack *S = stack_create();
if(NULL==S)
{
return -1;
}
int num, s;
printf("请输入要转化的整数:");
scanf("%d", &num);
getchar();
printf("请输入要转化为几进制:");
scanf("%d", &s);
while(num >= s)
{
datatype e = num%s;
num = num/s;
stack_push(S, e);
}
stack_push(S, num);
stack_show(S);
return 0;
}
结果: