#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ET;
#define MAXSIZE 100
///栈/
//栈的存储结构
typedef struct stack
{
ET *bottom;//栈底指针
ET *top;//栈顶指针
int stacksize;//栈可用最大容量
}stack;
//栈的初始化
Status Initstack(stack *S)
{
S->bottom = (ET *)malloc(sizeof(ET)*MAXSIZE);
S->top = S->bottom;
S->stacksize = MAXSIZE;
return OK;
}
//入栈
Status Push(stack *S, ET e)
{
if(S->top - S->bottom == S->stacksize) return ERROR;
*(S->top++) = e;
return OK;
}
//出栈
Status Pop(stack *S, ET *e)
{
if(S->bottom==S->top) return ERROR;
*e = *(--S->top);
return OK;
}
//取栈顶元素
ET GEttop(stack S)
{
if(S.bottom!=S.top) return *(S.top-1);
}
//测试是否为空栈
Status Isempty(stack *S)
{
if(S->bottom == S->top) return ERROR;
else return OK;
}
//销毁栈
Status Destroystack(stack *S)
{
if(S->bottom == NULL) return ERROR;
//free(S->bottom);
S->bottom=S->top=NULL;
return OK;
}
//清空栈
Status Clearstack(stack *S)
{
if(S->bottom==NULL) return ERROR;
S->bottom = S->top;
return OK;
}
//遍历栈
void Browsestack(stack S)
{
int j = 0;
ET *p;
p = S.bottom;
printf("the stack is ");
while(p != S.top)
{
printf("%c ",*p);
p++;
}
printf("\n");
}
int main(void)
{
int num;
printf("please input a decimal number:");
scanf("%d",&num);
stack S;
Initstack(&S);
while(num>=8)
{
Push(&S,num%8);
num /= 8;
}
printf("%d",num);
while(Isempty(&S))
{
ET e;
Pop(&S,&e);
printf("%d",e);
}
system("pause");
return 0;
}
数据结构代码笔记:数值转换
最新推荐文章于 2023-01-31 20:04:42 发布