目录
seqstack.h
seqstack.c
main.c
seqstack.h
#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__
#define MAX 10
typedef int datatype;
typedef struct
{
datatype data[MAX]; //存放栈的数组
int top; //记录栈顶元素下标
}seqStack;
//创建
seqStack *create();
//判空
int empty(seqStack *S);
//判满
int full(seqStack *S);
//入栈(进栈、压栈)
int push(seqStack *S,datatype e);
//出栈,弹栈
int pop(seqStack *S);
//遍历栈
void show(seqStack *S);
//销毁栈
void destroy(seqStack *S);
#endif
seqstack.c
#include
#include
#include"seqstack.h"
//创建
seqStack *create()
{
seqStack *S=(seqStack*)malloc(sizeof(seqStack));
if(NULL==S)
{
printf("创建失败n");
return NULL;
}
//初始化
S->top=-1; //将栈顶设置为-1
printf("创建成功n");
return S;
}
//判空
int empty(seqStack *S)
{
return S->top==-1?1:0; //1空 0非空
}
//判满
int full(seqStack *S)
{
return S->top==(MAX-1)?1:0; //1满 0非满
}
//入栈(进栈、压栈)
int push(seqStack *S,datatype e)
{
//判断逻辑
if (NULL==S||full(S))
{
printf("入栈失败n");
return -1;
}
//入栈操作 先加后压
S->top++;
S->data[S->top]=e;
printf("入栈成功n");
return 0;
}
//出栈,弹栈
int pop(seqStack *S)
{
// 判断逻辑
if (NULL==S || empty(S))
{
printf("出栈失败n");
return -1;
}
//出栈逻辑
//printf("%d出栈成功n",S->data[S->top]);
//printf("%dt",S->data[S->top]);
if(S->data[S->top]==10)
printf("A");
else if(S->data[S->top]==11)
printf("B");
else if(S->data[S->top]==12)
printf("C");
else if(S->data[S->top]==13)
printf("D");
else if(S->data[S->top]==14)
printf("E");
else if(S->data[S->top]==15)
printf("F");
else printf("%d",S->data[S->top]);
S->top--;
//printf("出栈n");
return 0;
}
//遍历栈
void show(seqStack *S)
{
//判断逻辑
if (NULL==S||empty(S))
{
printf("遍历失败n");
return ;
}
//遍历
printf("从栈顶到栈底所有元素分别是:");
for (int i=S->top;i>=0; i--)
{
printf("%dt",S->data[i]);
}
printf("n");
}
//销毁栈
void destroy(seqStack *S)
{
if (NULL!=S )
{
free(S);
S=NULL;
}
printf("释放成功n");
}
main.c
#include
#include"seqstack.h"
#include
int main(int argc, const char *argv[])
{
seqStack *S=create();
if (NULL==S)
{
return -1;
}
int m,n;
datatype e;
printf("请示入一个十进制数:");
scanf("%d",&m);
printf("请输入要转换的进制:");
scanf("%d",&n);
while (m!=0)
{
e=m%n;
push(S,e);
m=m/n;
}
show(S);
printf("进制转换后为n");
while (S->top!=-1)
{
pop(S);
}printf("n");
return 0;
}
#include#include #include"seqstack.h" //创建 seqStack *create() { seqStack *S=(seqStack*)malloc(sizeof(seqStack)); if(NULL==S) { printf("创建失败n"); return NULL; } //初始化 S->top=-1; //将栈顶设置为-1 printf("创建成功n"); return S; } //判空 int empty(seqStack *S) { return S->top==-1?1:0; //1空 0非空 } //判满 int full(seqStack *S) { return S->top==(MAX-1)?1:0; //1满 0非满 } //入栈(进栈、压栈) int push(seqStack *S,datatype e) { //判断逻辑 if (NULL==S||full(S)) { printf("入栈失败n"); return -1; } //入栈操作 先加后压 S->top++; S->data[S->top]=e; printf("入栈成功n"); return 0; } //出栈,弹栈 int pop(seqStack *S) { // 判断逻辑 if (NULL==S || empty(S)) { printf("出栈失败n"); return -1; } //出栈逻辑 //printf("%d出栈成功n",S->data[S->top]); //printf("%dt",S->data[S->top]); if(S->data[S->top]==10) printf("A"); else if(S->data[S->top]==11) printf("B"); else if(S->data[S->top]==12) printf("C"); else if(S->data[S->top]==13) printf("D"); else if(S->data[S->top]==14) printf("E"); else if(S->data[S->top]==15) printf("F"); else printf("%d",S->data[S->top]); S->top--; //printf("出栈n"); return 0; } //遍历栈 void show(seqStack *S) { //判断逻辑 if (NULL==S||empty(S)) { printf("遍历失败n"); return ; } //遍历 printf("从栈顶到栈底所有元素分别是:"); for (int i=S->top;i>=0; i--) { printf("%dt",S->data[i]); } printf("n"); } //销毁栈 void destroy(seqStack *S) { if (NULL!=S ) { free(S); S=NULL; } printf("释放成功n"); }



