|
10鱼币
本帖最后由 大西洋1912 于 2013-8-4 10:56 编辑
//添加学生部分(相关代码:24~30,94~104,129~172三处代码行),单步调试没有问题,但是一运行就出现死循环,为什么呢?是哪儿导致的死循环?
- #include <stdio.h>
- #include <malloc.h>
- #include <windows.h>
- struct student{
- int num;
- char name[20];
- float score;
- struct student * next;//这是整个链表的核心,用于存放下一段空间的首地址
- }stu;
- int n=0; //结点个数
- int main(void){
- int number; //要删除的学号
- int bianhao=0; //操作选项编号
- struct student * head; //定义头指针
- struct student * creat(); //动态创建链表函数
- void print(struct student * head); //输出链表函数
- struct student * del(struct student * head, int number);//删除函数
- struct student * add(struct student * head);
- head = creat(); //创建链表
- if(head==NULL){
- printf("空表!!!\n");
- }else{
- print(head); //输出链表内容
- select:
- printf("请选择操作:\n\n1、添加\t2、删除\t3、返回\t4、退出系统\n\n请选择:");
- scanf("%d", &bianhao);
- if( 1==bianhao ){//添加学生
- head = add(head);
- print(head);
- goto select;
- }else if( 2==bianhao ){
- printf("请输入要删除的学号:");
- scanf("%d", &number);
- head = del(head, number); //删除用户
- if(head!=NULL){
- print(head);
- printf("\n删除成功!!!\n\n");
- }else{
- system("cls");
- printf("\n表已删除!!!\n\n");//当表中内容被全部删除以后
- }
- goto select;
- }else if( 3==bianhao ){//返回,直接显示当前结果
- print(head);
- goto select;
- }else if( 4==bianhao ){
- printf("系统已关闭!!!\n");
- }else{
- printf("输入错误!!!\n");
- goto select;
- }
- }
- return 0;
- }
- //动态创建空间
- struct student * creat(){
- struct student * p1, * p2;
- struct student * head;//定义头指针
- head = NULL;
- p1 = (struct student *)malloc(sizeof(struct student));
- p2 = p1;
- printf("\n-----成绩管理系统-----\n");
- printf("学号:");
- scanf("%d", &p1->num);
- if(p1->num!=0){
- printf("姓名:");
- scanf("%s", &p1->name);
-
- printf("分数:");
- scanf("%f", &p1->score);
- }
- while(p1->num!=0){
- n+=1;
- if(n==1){
- head = p1;//头指针head指向第一个结点(首节点)
- }else{
- p2->next = p1;//将下一段空间的地址保存到上一个结点中
- p2 = p1;
- }
- //p1指向开辟的下一段空间
- p1 = (struct student *)malloc(sizeof(struct student));
- printf("\n学号:");
- scanf("%d", &p1->num);
- if(p1->num!=0){
- printf("姓名:");
- scanf("%s", &p1->name);
- printf("分数:");
- scanf("%f", &p1->score);
- }
- }
- p2->next = NULL;
- return head;
- }
- //输出链表内容
- void print(struct student * head){
- system("cls");
- printf("学生成绩表\n\n", n);
- printf("学号\t姓名\t分数\n\n");
- do{
- printf("%d\t%s\t%5.1f\n\n", head->num, head->name, head->score);
- head = head->next;
- }while(head!=NULL);
- printf("目前共有%d条记录!!!\n\n", n);
- }
- //删除指定的项目
- struct student * del(struct student * head, int number){
- struct student * p1, * p2;
- if(head==NULL){
- printf("对不起,不能删除空表!!!\n");
- return head;
- }
- p1 = head;
- while( number!=p1->num && p1->next!=NULL){
- p2 = p1;
- p1 = p1->next;
- }
- if(p1->num==number){
- if(head==p1){
- head = p1->next;
- }else{
- p2->next = p1->next;
- }
- n--;
- }else{
- printf("对不起没有这个学号!!!\n");
- }
- return head;
- }
- //添加内容的函数
- struct student * add(struct student * head){
- struct student * p0, * p1, *p2;
- p2 = p1 = head;
- p0 = &stu;
-
- //下面为用户输入添加的数据
- printf("请输入需要插入的数据:\n");
- printf("学号:");
- scanf("%d", &p0->num);
-
- if(p0->num<=0){
- printf("学号输入错误!!!\n数据添加失败!!!\n");
- }else{
- printf("姓名:");
- scanf("%s", &p0->name);
- printf("成绩:");
- scanf("%f", &p0->score);
- printf("\n您添加的数据为:\n\n学号:%d\n姓名:%s\n成绩:%-5.2f\n\n", p0->num, p0->name, p0->score);
-
- if( head==NULL ){
- head = p0;
- p0->next = NULL;
- }else{
- while( p0->num>p1->num && p1->next!=NULL ){
- p2 = p1;
- p1 = p1->next;
- }
- if(p0->num<=p1->num){
- if(p1==head){ //头结点
- head = p0;
- }else{
- p2->next = p0;
- }
- p0->next = p1;
- }else{
- p1->next = p0;
- p0->next = NULL;
- }
- }
- n++;
- }
- return head;
- }
复制代码
|
|