用线性表存放二进制数,每个节点存放一个二进制位,最后实现二进制数加一
#include<stdio.h>#include<stdlib.h>
typedef struct aa
{
int data;
struct aa *prior,*next;
}LinkList;
LinkList *Int()
{
LinkList *head,*s,*r;
head=(LinkList*)malloc(sizeof(LinkList));
r=head;
int a;
scanf("%d",&a);
while(a!=2)
{
s=(LinkList*)malloc(sizeof(LinkList));
s->data=a;
r->next=s;
s->prior=r;
r=s;
scanf("%d",&a);
}
r->next=head;
head->prior=r;
return head;
}
LinkList *sum1(LinkList *L)
{
LinkList *p,*r;
r=L;
p=L->prior;
int sum;
sum=p->data+1;
while(sum==2)
{
if(p!=L)
{
p->data=0;
p=p->prior;
}
sum=p->data+1;
}
return r;
}
int main()
{
LinkList *H,*G,*p;
H=Int();
G=sum1(H);
p=H->next;
for(;p!=G;p=p->next)
printf("%d",p->data);
printf("\n");
return 0;
}
//我输入一个二进制数后,不知道为什么显示不出结果,求大神指点错误