#include<stdio.h>
#include<malloc.h>
#define ElemType int
typedef struct LinkStack{//定义链栈结构 ,其实质是一个受限的单链表
ElemType data;
struct LinkStack *next;
}LinkStack;
LinkStack *Init(LinkStack *top)
{//初始化一个带有头结点的链栈 top=(LinkStack *)malloc(sizeof(LinkStack));
top->next=NULL; return top;
}
LinkStack *Push(LinkStack *top,ElemType &e)
{//入栈操作 LinkStack *p;
p=(LinkStack *)malloc(sizeof(LinkStack));
if(p==NULL){ printf("栈满"); }
else{ p->data=e; p->next=top->next; top->next=p; }
return top;
}
LinkStack *Pop(LinkStack *top)
{//出栈操作 LinkStack *p;
ElemType e;
p=top->next;
if(p==NULL){ printf("栈空"); }
else{
e=p->data;
printf("%d",e);
top->next=p->next;
free(p);
}
return top;
}
LinkStack *Get(LinkStack *top)
{//获取栈顶元素,元素仍在栈内
ElemType e;
LinkStack *p;
p=top->next;
if(p==NULL){ printf("空栈");
}else{ printf(" 取栈顶元素:");
e=p->data;
printf("%d ",e); }
}
void *Coversion(ElemType dec)
{//十进制转换成八进制 ElemType e;
LinkStack *top,*p; top=Init(top);
for(;dec!=0;dec=dec/8)
{
e=dec%8; top=Push(top,e);
}
for(p=top->next;p;p=top->next)
{
top=Pop(top); }
}
int main()
{
LinkStack *top;
ElemType dec;
printf("输入十进制数: ");
scanf("%d",&dec);
//top=Init(top);
/*printf("请输入入栈元素:");
scanf("%d",&e);
while(e!=-1){ top=Push(top,e);
scanf("%d",&e); }
Get(top);
top=Pop(top);
top=Pop(top);
//Get_top(top);*/
printf("对的八进制是:");
Coversion(dec);
return 0; }