#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
const int maxn = 1e5 + 10;
typedef struct SNode* Stack;
struct SNode {
int data;
struct SNode* next;
};
Stack CreatStack() {
Stack ptrl;
ptrl = (Stack)malloc(sizeof(struct SNode));
ptrl->next = NULL;
return ptrl;
}
void push(Stack p, int item) {
Stack ptrl;
ptrl = (Stack)malloc(sizeof(struct SNode));
ptrl->data = item;
ptrl->next = p->next;
p->next = ptrl;
}
int pop(Stack p) {
Stack ptrl;
int item;
if (p->next == NULL) {
printf("堆栈空");
return NULL;
}
else {
ptrl = p->next;
p->next = ptrl->next;
item = ptrl->data;
free(ptrl);
return item;
}
}
int main() {
/*freopen("E:/c++/input.txt", "r", stdin);
freopen("E:/c++/output.txt", "w", stdout);*/
int n;
while (scanf("%d", &n),n != -1) {
Stack stack = CreatStack();
int s = n;
while (n > 0) {
push(stack, n % 2);
n = n / 2;
}
printf("%d--->",s);
if (s == 0) {
printf("0");
}
while (stack->next != NULL) {
printf("%d", pop(stack));
}
printf("\n");
}
//fclose(stdin);//关闭重定向输入
//fclose(stdout);//关闭重定向输出
return 0;
}
栈的建立
最新推荐文章于 2024-05-09 16:28:25 发布