注册 登录
编程论坛 数据结构与算法

自己学数据结构,单链表 c 实现一直出错,求大神修改

lamsosad 发布于 2014-10-15 13:50, 569 次点击
#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 编辑 ]
2 回复
#2
dcl20142014-10-16 09:44
//改了一下 现在没错了
#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;
}
#3
lamsosad2014-10-16 14:06
回复 2 楼 dcl2014
=V= 感谢     ~
程序正常进行了...

求原因.......
1