#include<stdio.h>
#include<stdlib.h>
const int MAX_SIZE=1e5+10;
typedef struct SNode{
struct SNode *next;
int data;
}Stack;
Stack* creatStack(){
Stack *head;
head=(Stack*)malloc(sizeof(struct SNode));
head->next=NULL;
head->data=0;
}
int isEmpty(Stack *p){
if(p->next==NULL){
return 1;
}
return 0;
}
int isFull(Stack *head){
if(head->data==MAX_SIZE){
printf("The stack is full\n");
return 1;
}
return 0;
}
void push(Stack *head,int item){
if(isFull(head))return;
Stack* tmp=(Stack*)malloc(sizeof(struct SNode));
tmp->data=item;
tmp->next=head->next;
head->next=tmp;
head->data++;
}
int pop(Stack *head){
head->data--;
Stack *p=head->next;
int res=p->data;
head->next=p->next;
free(p);
return res;
}
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
while(~scanf("%d",&n),n!=-1){
Stack *ADT=creatStack();
printf("%d--->",n);
if(n==0){
printf("0\n");
return 0;
}
while(n>0){
push(ADT,n%2);
n/=2;
}
while(!isEmpty(ADT)){
printf("%d",pop(ADT));
}
printf("\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}
链式实现栈并用其解决十进制数转化为二进制数
最新推荐文章于 2023-03-24 09:41:19 发布