| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:数据结构的链表问题!谢谢了
只看楼主 加入收藏
ogiso_pest
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-6-26
收藏
 问题点数:0 回复次数:0 
数据结构的链表问题!谢谢了
题目:要求使用链表保存输入的二进制代码,然后对其进行加一操作,再输出!但是为什么输入1111后,却是10000,而输入101,也是10000!
请大家帮忙看看到底是什么问题!谢谢了




#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 100

typedef  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 23:04
快速回复:数据结构的链表问题!谢谢了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014033 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved