#include<bits/stdc++.h>
#define MaxSize 100
typedef struct SNode* Stack;
typedef int elementtype;
struct SNode {
elementtype data;
struct SNode* next;
};
Stack createstack()
{
Stack p;
p = (Stack)malloc(sizeof(struct SNode));
p->next = NULL;
return p;
}
int IsEmpty(Stack p)
{
return (p->next == NULL);
}
void push(elementtype item,Stack p) {
struct SNode* t;
t= (Stack)malloc(sizeof(struct SNode));
t->data = item;
t->next = p->next;
p->next = t;
}
elementtype pop(Stack p)
{
struct SNode* f;
elementtype te;
if (IsEmpty(p))
{
printf("堆栈空");
return NULL;
}
else {
f = p->next;
te = f->data;
p->next = f->next;
free(f);
return te;
}
}
int main()
{
int m, n;
struct SNode* h;
scanf("%d%d", &m, &n);
if (m == 0)
printf("%d\n", m);
else
{
Stack cs = createstack();
while (m)
{
int s = m % n;
push(s, cs);
m = m / n;
}
for (h = cs->next; h != NULL; h = h->next)
{
printf("%d", h->data);
}
printf("\n");
}
return 0;
}
数据结构实验之栈与队列一:进制转换
最新推荐文章于 2024-08-19 23:56:23 发布