C4n 2021-03-22 14:37 采纳率: 80%
浏览 47
已结题

我的程序不知道为什么运行到排序这一步就奔溃了?很短的一段代码

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define New_node (stu*)malloc(sizeof(stu))
typedef struct student
{
	char Student_ID[13];
	int num;
	struct student*next;

}stu;
void swap(stu *,stu *);
void input(stu**);//输入学生信息 
void output(stu*);// 输出所有学生信息 
void sort(stu*);//学号排序学生信息 
main()
{
	stu *L=New_node;   
	L->next=NULL;     //建立头节点
	stu *R=L;         //尾指针指向头节点 
	int choice;
	do
	{
		printf("1.输入,2.输出3.排序\n");
	    scanf("%d",&choice);
	    fflush(stdin);
	    switch(choice)
	    {
	    	case 1:input(&R);break;
	    	case 2:output(L);break;
	    	case 3:sort(L->next);break;
		}
	}while(1);
 	return 0;
}
void input(stu**R) //输入学生信息
{
	stu*p=New_node; //p用来存新加学生的数据 
	printf("\n请输入三位数的学号:\n");
	scanf("%s",&p->Student_ID);
	printf("\n请输入数字:\n");
	scanf("%d",&p->num);
	p->next=NULL;
	(*R)->next=p;
	*R=p;  //尾指针指向了p 
	if (p == NULL)//若为新建节点申请内存失败,则退出程序 
	{
		printf("申请内存失败!!\n");
		exit(0);
	}
}
void output(stu *L)//输出全部学生数据 
{
	stu *p;
	p=L->next;
	while(p!=NULL)
	{
		printf("学号:%s\t",p->Student_ID);
		printf("数字:%d\n",p->num);
		printf("************************************************\n");
		p=p->next;
	}
	
}
void swap(stu *p1,stu *p2)//交换p1,p2数据域的内容 
{
	char xuehao[13];
	int num;
	strcpy(xuehao,p1->Student_ID);
	strcpy(p1->Student_ID,p2->Student_ID);
	strcpy(p2->Student_ID,xuehao);
	num=p1->num;
	p1->num=p2->num;
	p2->num=num;
 } 
void sort(stu *Lr)//排序,按num从小排到大 
{
	stu *p;
	int i,j,n;
	n=4;
	int t;
     for(i=1;i<n;i++)
	{	p=Lr;
		for(j=0;j<n-i;j++)
		{
			if((p->num)>(p->next->num))
			swap(p,p->next);
			p=p->next;
		}
	}
}

就sort函数执行不了,sort是用的冒泡排序法

  • 写回答

4条回答 默认 最新

  • C4n 2021-03-22 14:56
    关注

    alt

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 1月9日
  • 已采纳回答 1月1日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值