• 【C语言】用单链表实现一个二进制数加1的运算。


    #include
    #include
    #include
    #include
    #include

    struct node
    {
        int data;
        struct node *next;
    };
    int main()
    {
        int count=0;
        char a[80];
        scanf("%s",a);
        struct node *p,*q,*h;
        h= (struct node *)malloc(sizeof(struct node));
        p= (struct node *)malloc(sizeof(struct node));
        q= (struct node *)malloc(sizeof(struct node));
        h->next = p;
        for(int i=strlen(a)-1;i>0;i--)//倒序读取二进制数并存入单链表
        {
            p->data = a[i] - '0';//将char类型转化为int
            p->next = q;
            p = p->next;
            q = (struct node *)malloc(sizeof(struct node));


        }
        p->data = a[0] - '0';//最后一个结点的特殊处理
        p->next = NULL;

        p = h->next;
        int j=0;
        while(j     {
            if(p->data == 0)
            {
                p->data = 1;
                break;
            }
            else {
                    if(p->next == NULL)
                    {
                        q = (struct node *)malloc(sizeof(struct node));
                        q->data = 1;
                        q->next = NULL;
                        p->next = q;
                        p->data = 0;
                        break;
                    }
                    else {
                            p->data = 0;
                            p=p->next;


                        }
            }
        }
        p = h->next;
        for(;p->next!=NULL;count++)//count计算单链表的长度
        {
            p = p->next;
        }
        p = h->next;
        for(int m=0;m<=count;m++)//倒序输出单链表的数据
        {
            for(int n=m;n         {
                p = p->next;
            }
            printf("%d",p->data);
            free(p);
            p = h->next;
        }
        return 0;

    }

  • 相关阅读:
    2022DAMA数据治理最佳培训机构奖
    服务网格和CI/CD集成:讨论服务网格在持续集成和持续交付中的应用。
    如何把Spring学精通了?
    元对象系统功能
    leetcode10--正则表达式
    matcher中find,matches,lookingAt匹配字符串的不同之处说明
    用JQuery操作元素的style属性
    【swagger】springboot项目中配置Swagger的两种方式以及swagger权限验证、安全控制
    勒索软件攻击防护中的6个常见错误
    亚马逊云科技 Build On -第二季学习心得
  • 原文地址:https://blog.csdn.net/Klay_thompson11/article/details/126600548