#include<bits/stdc++.h>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef int SElemType;
typedef struct SNode{
SElemType data;
struct SNode *next;
}SNode,*LinkStack;
Status InitStack (LinkStack &S){
S=NULL;
return OK;
}
Status Push(LinkStack &S,SElemType e){
LinkStack p = new SNode;
p->data=e;
p->next=S;
S=p;
return OK;
}
Status Pop(LinkStack &S,SElemType &e){
if(S==NULL) return ERROR;
LinkStack p;
p=S;
e=p->data;
S=S->next;
delete p;
return OK;
}
void TraverseStack(LinkStack S){
if(S){
cout<<S->data<<" ";
TraverseStack(S->next);
}
}
int main(){
LinkStack S;
InitStack(S);
int n,m;
cin>>n>>m;
while(n){//n不等于0时
Push(S,n%m);
n=n/m;
}
TraverseStack(S);
}
数据结构:链栈——数值的转换
于 2023-11-30 17:01:08 首次发布