xuannfann 2018-10-10 01:43 采纳率: 100%
浏览 454
已采纳

c语言 基础链表 意外中断。谢谢

(在输出位置意外中断,程序功能是输入姓名和学号,然后在首位再插入一个学生,然后输出整个链表,中断位置在输出函数的第一次姓名输出)

#include
#include

struct Student
{
char cName;
int iNumber;
struct Student *pNext;
};

int iCount;

struct Student *Create()
{
struct Student *pHead=NULL;
struct Student *pEnd,*pNew;
printf("Plase enter Name first,then Number:\n");
pEnd=pNew=malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
while(pNew->iNumber!=0)
{
iCount++;
if(iCount==1)
{
pNew->pNext=NULL;
pEnd=pNew;
pHead=pNew;
}
else
{
pNew->pNext=NULL;
pEnd->pNext=pNew;
pEnd=pNew;
}
pNew=malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
}
free(pNew);
return pHead;
};

void Print(struct Student *pHead)
{
struct Student *pTemp;
int iIndex=1;
printf("the list has %d members\n\n",iCount);

pTemp=pHead;

while(pTemp!=NULL)
{
printf("No.%d student:\n",iIndex);
printf("accomplished");
printf("Name:%s",pHead->cName);
printf("Number:%d\n",pTemp->iNumber);
pTemp=pTemp->pNext;
iIndex++;
}
};

struct Student *Insert(struct Student *pHead)
{
struct Student *pInsert;
printf("Insert member at first\n");
pInsert=malloc(sizeof(struct Student));
scanf("%s",&pInsert->cName);
scanf("%d",&pInsert->iNumber);
pInsert->pNext=pHead->pNext;
pHead->pNext=pInsert;
iCount++;
return pHead;

};

main()
{
struct Student *pHead;

printf("readng...\n");
pHead=Create();
printf("Insert readying\n");
pHead=Insert(pHead);
printf("\nInsert accomplished\n");
Print(pHead);

return 0;
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-10-10 02:04
    关注

    char cName;
    ->
    char cName[100];

    你只有1个字符,存不下name

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)