HizoDream 2017-02-21 04:44 采纳率: 50%
浏览 921
已采纳

c语言链表建立的问题。。。求人来解答

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct student
{int number;
struct student *next;
} NODE;
typedef struct student node;
typedef node *link;
link creatlist()
{
    link head,p,q;
    int a;
    char ch;
    head=(link)malloc(sizeof(struct student));
    p=head;
    printf("请输入相关学号:(以?结尾)/n"); 
    while(ch!='?')
    {q=(link)malloc(sizeof(struct student));
    scanf("%d",&a);
    q->number=a;
    p->next=q;
    p=q;
    ch=getchar();
    }
    p->next=NULL; 
    return(head);
}
link add(link head,int a)
{int i,m; char ch; link ptr;
for(i=0;i<a;i++)
head=head->next;
printf("请输入要插入的学号(以?结尾):");
while(ch!='?')
{scanf("%d",&m);
ptr=(link)malloc(sizeof(struct student));
ptr->number=m;
ptr->next=head->next;
head->next=ptr;
head=ptr; 
ch=getchar();
return 0;
 } 
 } 
 void print(link head)
 {head=head->next;
 while(head!=NULL)
 {printf("%5d",head->number);
 head=head->next;
  } 
  printf("\n") ;
 }
int main()
{link a;  int n;
a=creatlist();
print(a);
printf("请输入要插到第几项的项数:");
scanf("%d",n);
add(a,n);
printf("ok!");
print(a);
return 0;
}

当调用add的函数时显示停止工作,这段代码是为了建立一个链表和插入链表的实现。。求大神解答。。
ps:觉得麻烦的话不用改正,告诉我为什么会出现这种情况就行了。
在此拜谢orz

  • 写回答

3条回答 默认 最新

  • 小灸舞 2017-02-21 05:17
    关注

    1.你的main函数里的scanf少了个&
    2.然后我接着看了你的creatlist函数,你这个输入必须是输入一个数按一下回车,最后输入的数还必须加上?再按回车才行
    图片说明

     #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    typedef struct student
    {
        int number;
        struct student *next;
    } NODE;
    typedef struct student node;
    typedef node *link;
    link creatlist()
    {
        link head, p, q;
        int a;
        char ch = ' ';
        head = (link)malloc(sizeof(struct student));
        p = head;
        printf("请输入相关学号:(以?结尾)/n");
        while (ch != '?')
        {
            q = (link)malloc(sizeof(struct student));
            scanf("%d", &a);
            q->number = a;
            p->next = q;
            p = q;
            ch = getchar();
        }
        p->next = NULL;
        return(head);
    }
    link add(link head, int a)
    {
        int i, m; char ch = ' '; link ptr;
        for (i = 0; i<a; i++)
            head = head->next;
        printf("请输入要插入的学号(以?结尾):");
        while (ch != '?')
        {
            scanf("%d", &m);
            ptr = (link)malloc(sizeof(struct student));
            ptr->number = m;
            ptr->next = head->next;
            head->next = ptr;
            head = ptr;
            ch = getchar();
            return 0;
        }
    }
    void print(link head)
    {
        head = head->next;
        while (head != NULL)
        {
            printf("%5d", head->number);
            head = head->next;
        }
        printf("\n");
    }
    int main()
    {
        link a;  int n;
        a = creatlist();
        print(a);
        printf("请输入要插到第几项的项数:");
        scanf("%d", &n);
        add(a, n);
        printf("ok!");
        print(a);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站