大神帮忙看一下这个动态链表插入程序,崩溃了……
本帖最后由 veer786 于 2013-10-22 20:37 编辑本帖最后由 veer786 于 2013-10-22 19:55 编辑
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)//结构体的长度
int n;//全局变量,用来记录学生数
struct Student *creat();//创建链表
void print(struct Student *);//打印链表
struct Student* insert_link(struct Student *head,int num);//插入节点
struct Student //创建学生
{
int num;
int score;
struct Student *next;
};
int main()
{
struct Student *stu;
int num = 0;
stu = creat();
print(stu);
printf("请选择插入点\n");
scanf("%d",&num);
printf("在%d学生之后插入\n",num);
print(insert_link(stu,num));
return 0;
}
struct Student* creat()
{
struct Student *head = NULL;
struct Student *p1,*p2;
p1 = p2 = (struct Student *)malloc(LEN);
printf("请输入学号:\n");
while(scanf("%d",&p1->num) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入学号!\n");
getchar();
}
printf("请输入分数:\n");
while(scanf("%d",&p1->score) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入分数!\n");
getchar();
}
while(p1->num != 0)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct Student *)malloc(LEN);
printf("请输入学号:\n");
while(scanf("%d",&p1->num) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入学号!\n");
getchar();
}
printf("请输入分数:\n");
while(scanf("%d",&p1->score) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入分数!\n");
getchar();
}
}
p2->next = NULL;
return head;
}
void print(struct Student *head)
{
printf("共有%d条记录\n",n);
while(head)
{
printf("学号为:%d\t分数为:%d\n",head->num,head->score);
head =head->next;
}
}
struct Student*insert_link(struct Student *head,int num)
{
struct Student *p1,*p2;
if(head == NULL)//如果链表为空,插入新记录
{
struct Student stu;
head = &stu;
printf("该表为空表,添加记录!\n");
printf("请输入学号:\n");
while(scanf("%d",&stu.num) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入学号!\n");
getchar();
}
printf("请输入分数:\n");
while(scanf("%d",&stu.score) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入分数!\n");
getchar();
}
stu.next = NULL;
p1 = NULL;
p2 = NULL;
return head;
}
else//链表不为空
{
p1 = head;
while(p1->num!=num&&p1->next != NULL)//遍历链表
{
p2 = p1;
p1 = p1->next;
}
if(p2->num == num)//输入学号得到匹配,在此学号之后进行插入
{
struct Student stu;
printf("请输入学号:\n");
while(scanf("%d",&stu.num) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入学号!\n");
getchar();
}
printf("请输入分数:\n");
while(scanf("%d",&stu.score) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入分数!\n");
getchar();
}
p2->next = &stu;
stu.next = p1;
}
else//无匹配学号,在链表结尾插入
{
struct Student stu;
printf("请输入学号:\n");
while(scanf("%d",&stu.num) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入学号!\n");
getchar();
}
printf("请输入分数:\n");
while(scanf("%d",&stu.score) != 1)//用来检测错误输入
{
printf("输入有误,请重新输入分数!\n");
getchar();
}
p1->next = &stu;
stu.next = NULL;
}
}
return head;
}
悲剧了,搞不定了……求大神指导啊{:2_30:} {:7_162:}
路过看一看= =! 输出链表函数应在里面重新定义一个指针p用来存放head而不是直接使用head。这样的结果会使head指向NULL。
插入函数中,你把stu定义在函数里面,可是退出函数后,它的内存就被释放了,那还拿什么来输出啊 好厉害的说,我什么时候也能写出这么多东西出来 我已经替你编译好了:lol: 研究一下你的代码吧
.file "std.c"
.comm n,4,4
.section .rodata
.LC0:
.string "\350\257\267\351\200\211\346\213\251\346\217\222\345\205\245\347\202\271"
.LC1:
.string "%d"
.LC2:
.string "\345\234\250%d\345\255\246\347\224\237\344\271\213\345\220\216\346\217\222\345\205\245\n"
.text
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $32, %esp
movl $0, 24(%esp)
call creat
movl %eax, 28(%esp)
movl 28(%esp), %eax
movl %eax, (%esp)
call print
movl $.LC0, (%esp)
call puts
leal 24(%esp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
movl 24(%esp), %eax
movl %eax, 4(%esp)
movl $.LC2, (%esp)
call printf
movl 24(%esp), %eax
movl %eax, 4(%esp)
movl 28(%esp), %eax
movl %eax, (%esp)
call insert_link
movl %eax, (%esp)
call print
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE2:
.size main, .-main
.section .rodata
.LC3:
.string "\350\257\267\350\276\223\345\205\245\345\255\246\345\217\267\357\274\232"
.align 4
.LC4:
.string "\350\276\223\345\205\245\346\234\211\350\257\257\357\274\214\350\257\267\351\207\215\346\226\260\350\276\223\345\205\245\345\255\246\345\217\267\357\274\201"
.LC5:
.string "\350\257\267\350\276\223\345\205\245\345\210\206\346\225\260\357\274\232"
.align 4
.LC6:
.string "\350\276\223\345\205\245\346\234\211\350\257\257\357\274\214\350\257\267\351\207\215\346\226\260\350\276\223\345\205\245\345\210\206\346\225\260\357\274\201"
.text
.globl creat
.type creat, @function
creat:
.LFB3:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $40, %esp
movl $0, -12(%ebp)
movl $12, (%esp)
call malloc
movl %eax, -20(%ebp)
movl -20(%ebp), %eax
movl %eax, -16(%ebp)
movl $.LC3, (%esp)
call puts
jmp .L4
.L5:
movl $.LC4, (%esp)
call puts
call getchar
.L4:
movl -16(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L5
movl $.LC5, (%esp)
call puts
jmp .L6
.L7:
movl $.LC6, (%esp)
call puts
call getchar
.L6:
movl -16(%ebp), %eax
addl $4, %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L7
jmp .L8
.L15:
movl n, %eax
addl $1, %eax
movl %eax, n
movl n, %eax
cmpl $1, %eax
jne .L9
movl -16(%ebp), %eax
movl %eax, -12(%ebp)
jmp .L10
.L9:
movl -20(%ebp), %eax
movl -16(%ebp), %edx
movl %edx, 8(%eax)
.L10:
movl -16(%ebp), %eax
movl %eax, -20(%ebp)
movl $12, (%esp)
call malloc
movl %eax, -16(%ebp)
movl $.LC3, (%esp)
call puts
jmp .L11
.L12:
movl $.LC4, (%esp)
call puts
call getchar
.L11:
movl -16(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L12
movl $.LC5, (%esp)
call puts
jmp .L13
.L14:
movl $.LC6, (%esp)
call puts
call getchar
.L13:
movl -16(%ebp), %eax
addl $4, %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L14
.L8:
movl -16(%ebp), %eax
movl (%eax), %eax
testl %eax, %eax
jne .L15
movl -20(%ebp), %eax
movl $0, 8(%eax)
movl -12(%ebp), %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE3:
.size creat, .-creat
.section .rodata
.LC7:
.string "\345\205\261\346\234\211%d\346\235\241\350\256\260\345\275\225\n"
.align 4
.LC8:
.string "\345\255\246\345\217\267\344\270\272\357\274\232%d\t\345\210\206\346\225\260\344\270\272\357\274\232%d\n"
.text
.globl print
.type print, @function
print:
.LFB4:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
movl n, %eax
movl %eax, 4(%esp)
movl $.LC7, (%esp)
call printf
jmp .L18
.L19:
movl 8(%ebp), %eax
movl 4(%eax), %edx
movl 8(%ebp), %eax
movl (%eax), %eax
movl %edx, 8(%esp)
movl %eax, 4(%esp)
movl $.LC8, (%esp)
call printf
movl 8(%ebp), %eax
movl 8(%eax), %eax
movl %eax, 8(%ebp)
.L18:
cmpl $0, 8(%ebp)
jne .L19
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE4:
.size print, .-print
.section .rodata
.align 4
.LC9:
.string "\350\257\245\350\241\250\344\270\272\347\251\272\350\241\250\357\274\214\346\267\273\345\212\240\350\256\260\345\275\225\357\274\201"
.text
.globl insert_link
.type insert_link, @function
insert_link:
.LFB5:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $72, %esp
cmpl $0, 8(%ebp)
jne .L21
leal -28(%ebp), %eax
movl %eax, 8(%ebp)
movl $.LC9, (%esp)
call puts
movl $.LC3, (%esp)
call puts
jmp .L22
.L23:
movl $.LC4, (%esp)
call puts
call getchar
.L22:
leal -28(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L23
movl $.LC5, (%esp)
call puts
jmp .L24
.L25:
movl $.LC6, (%esp)
call puts
call getchar
.L24:
leal -28(%ebp), %eax
addl $4, %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L25
movl $0, -20(%ebp)
movl $0, -12(%ebp)
movl $0, -16(%ebp)
movl 8(%ebp), %eax
jmp .L26
.L21:
movl 8(%ebp), %eax
movl %eax, -12(%ebp)
jmp .L27
.L29:
movl -12(%ebp), %eax
movl %eax, -16(%ebp)
movl -12(%ebp), %eax
movl 8(%eax), %eax
movl %eax, -12(%ebp)
.L27:
movl -12(%ebp), %eax
movl (%eax), %eax
cmpl 12(%ebp), %eax
je .L28
movl -12(%ebp), %eax
movl 8(%eax), %eax
testl %eax, %eax
jne .L29
.L28:
movl -16(%ebp), %eax
movl (%eax), %eax
cmpl 12(%ebp), %eax
jne .L30
movl $.LC3, (%esp)
call puts
jmp .L31
.L32:
movl $.LC4, (%esp)
call puts
call getchar
.L31:
leal -40(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L32
movl $.LC5, (%esp)
call puts
jmp .L33
.L34:
movl $.LC6, (%esp)
call puts
call getchar
.L33:
leal -40(%ebp), %eax
addl $4, %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L34
movl -16(%ebp), %eax
leal -40(%ebp), %edx
movl %edx, 8(%eax)
movl -12(%ebp), %eax
movl %eax, -32(%ebp)
jmp .L35
.L30:
movl $.LC3, (%esp)
call puts
jmp .L36
.L37:
movl $.LC4, (%esp)
call puts
call getchar
.L36:
leal -52(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L37
movl $.LC5, (%esp)
call puts
jmp .L38
.L39:
movl $.LC6, (%esp)
call puts
call getchar
.L38:
leal -52(%ebp), %eax
addl $4, %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call __isoc99_scanf
cmpl $1, %eax
jne .L39
movl -12(%ebp), %eax
leal -52(%ebp), %edx
movl %edx, 8(%eax)
movl $0, -44(%ebp)
.L35:
movl 8(%ebp), %eax
.L26:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE5:
.size insert_link, .-insert_link
.ident "GCC: (GNU) 4.8.2 20131212 (Red Hat 4.8.2-7)"
.section .note.GNU-stack,"",@progbits
页:
[1]