| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 569 人关注过本帖
标题:自己学数据结构,单链表 c 实现一直出错,求大神修改
只看楼主 加入收藏
lamsosad
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-10-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
自己学数据结构,单链表 c 实现一直出错,求大神修改
#include <stdio.h>

#include <malloc.h>

#define ERROR 0;
#define OK 1;

typedef struct num{
    int data;
    struct num *next;
}*linklist;

struct num *create()            
{
    struct num *head,*current,*next;   
    int copy;
    char judge;
    printf("please input number:\n");
    scanf("%d",&copy);
    getchar();
   
    head=(struct num*)malloc(sizeof(struct num));
    head->data=copy;
    current=head;
    printf("是否继续输入:");
    scanf("%c",&judge);
  
    while(judge!='N')
    {
        printf("please input num:\n");
        scanf("%d",&copy);
        getchar();
        next=(struct num*)malloc(sizeof(struct num));
        next->data=copy;
        current->next=next;
        current=next;
        printf("是否继续输入:");
        scanf("%c",&judge);
    }
    current->next=NULL;
    return head;        
}

void list(struct num *p)
{
    while(1)
    {
        printf("%s\n",p->data);
        if(p->next!=NULL)
         p=p->next;
         
         else
         break;
     }
   
}

Insert(linklist p,int i,int e)   
{
   
    linklist q,s;
    int j=1;
    while(q&&j<i-1)
    {
        q=q->next;
        ++j;
    }
    if(!q||j>i)
    return ERROR;
    s=(struct num*)malloc(sizeof (struct num));
    s->data=e;
    s->next=q->next;
    q->next=s;
    return OK;
}


int main()
{
    struct num *p;
    p=create();
    list (p);
    int a,e;
    printf("请输入要插入数字的位置:\n");
    scanf("%d",&a);
    printf("请输入数字:\n");
    scanf("%d",&e);
        Insert(p,a,e) ;
    list(p) ;

    return 0;
}

问题是我不再想输入数字时,输入了N,程序就停止进行了  T U T
求大神指点  万分感谢

[ 本帖最后由 lamsosad 于 2014-10-15 17:11 编辑 ]
搜索更多相关主题的帖子: current include please number create 
2014-10-15 13:50
dcl2014
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:273
注 册:2014-9-20
收藏
得分:20 
//改了一下 现在没错了
#include <stdio.h>

#include <malloc.h>

#define ERROR 0;
#define OK 1;

typedef struct num{
    int data;
    struct num *next;
}*linklist;

struct num *create()            
{
    struct num *head,*current,*pnew;   
    int copy;
    char judge;
    printf("please input number:\n");
    scanf("%d",&copy);
    getchar();
   
    head=(struct num*)malloc(sizeof(struct num));
    head->data=copy;
    head->next=NULL;
    current=head;
    printf("是否继续输入:");
    scanf("%c",&judge);
  
    while(judge!='N')
    {
        printf("please input num:\n");
        scanf("%d",&copy);
        getchar();
        pnew=(struct num*)malloc(sizeof(struct num));
        pnew->data=copy;
        
        current->next=pnew;
        current=pnew;
        printf("是否继续输入:");
        scanf("%c",&judge);
    }
    pnew->next=NULL;
    return head;        
}

void list(struct num *p)
{
    while(1)
    {
        printf("%d\n",p->data);
        if(p->next!=NULL)
         p=p->next;
         
         else
         break;
     }
   
}

Insert(linklist p,int i,int e)   
{
   
    linklist q=p,s;
    int j=1;
    while(q&&j<i-1)
    {
        q=q->next;
        ++j;
    }
    if(!q||j>i)
    return ERROR;
    s=(struct num*)malloc(sizeof (struct num));
    s->data=e;
    s->next=q->next;
    q->next=s;
    return OK;
}


int main()
{
    struct num *p=NULL;
    p=create();
    list (p);
    int a,e;
    printf("请输入要插入数字的位置:\n");
    scanf("%d",&a);
    printf("请输入数字:\n");
    scanf("%d",&e);
        Insert(p,a,e) ;
    list(p) ;


    return 0;
}
2014-10-16 09:44
lamsosad
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-10-15
收藏
得分:0 
回复 2 楼 dcl2014
=V= 感谢     ~
程序正常进行了...

求原因.......
2014-10-16 14:06
快速回复:自己学数据结构,单链表 c 实现一直出错,求大神修改
数据加载中...
 
   



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

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