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条)

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统