level 1
hello_Puppy
楼主
题目:要求使用链表保存输入的二进制代码,然后对其进行加一操作,再输出!但是为什么输入1111后,却是10000,而输入101,也是10000!请大家帮忙看看到底是什么问题!谢谢了#include
#include
#define MAXSIZE 100typedef struct Node {int elem;struct Node *next;}Node,*BinList;BinList Init(BinList L){L=(BinList)malloc(sizeof(Node));L->next=NULL;return L;}BinList Create(BinList L){Node *p,*q;p=L;int num;while(scanf("%c",&num)!=EOF){q=(Node *)malloc(sizeof(Node));q->elem=num;q->next=NULL;p->next=q;p=p->next;}return L;}BinList Increase(BinList L) { Node *pos,*q,*t;q=L->next;pos=L;while(q){if(q->elem=='0')pos=q; /*使用pos记录最后一个0的位置*/q=q->next;}if(pos==L) /*没有找到0,二进制码中无0*/ {t=(BinList)malloc(sizeof(Node));t->elem='1';q=L->next;while(q){q->elem='0';q=q->next;}t->next=L->next;L->next=t;} /*采用头插的方法将1插入到第一个位置,将其后的1全部变成0*/else{ pos->elem='1'; q=pos->next;while(q) { q->elem='0';q=q->next; }} return L;} void print(BinList L){Node *p;p=L->next;while(p){printf("%c",p->elem);p=p->next;}}int main(){BinList L;L=Init(L);L=Create(L);L=Increase(L);print(L);getch();return 0;}
2008年09月18日 15点09分
1
#include
#define MAXSIZE 100typedef struct Node {int elem;struct Node *next;}Node,*BinList;BinList Init(BinList L){L=(BinList)malloc(sizeof(Node));L->next=NULL;return L;}BinList Create(BinList L){Node *p,*q;p=L;int num;while(scanf("%c",&num)!=EOF){q=(Node *)malloc(sizeof(Node));q->elem=num;q->next=NULL;p->next=q;p=p->next;}return L;}BinList Increase(BinList L) { Node *pos,*q,*t;q=L->next;pos=L;while(q){if(q->elem=='0')pos=q; /*使用pos记录最后一个0的位置*/q=q->next;}if(pos==L) /*没有找到0,二进制码中无0*/ {t=(BinList)malloc(sizeof(Node));t->elem='1';q=L->next;while(q){q->elem='0';q=q->next;}t->next=L->next;L->next=t;} /*采用头插的方法将1插入到第一个位置,将其后的1全部变成0*/else{ pos->elem='1'; q=pos->next;while(q) { q->elem='0';q=q->next; }} return L;} void print(BinList L){Node *p;p=L->next;while(p){printf("%c",p->elem);p=p->next;}}int main(){BinList L;L=Init(L);L=Create(L);L=Increase(L);print(L);getch();return 0;}