关于链表的删除问题
源码附上运行到输入删除的学生信息时,输入完程序就直接死掉了
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)//student结构的大小//
struct student *creat();//创建链表
void print(struct student *head);//打印链表//
struct student *del(struct student *head,int num);//删除链表//
struct student
{
int num;
int score;
struct student *next;
};//声明结构体的类型的同时定义变量//
int n;//定义为全局变量//
void main()
{
struct student *stu,*p;
int num;
stu=creat();
p=stu;
print(p);
printf("\n\n");
printf("请输入您要删除的学生信息:");
scanf("%d",num);
print(del(p,num));
system("pause");
}
struct student *creat()//创建链表同时在里面写出算法//
{
system("color 0C");
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);//分配一个结构体大小的内存块,为p1,p2创建类型为struct student *类型的,长度为len的动态内存//
printf("please enter the nuim:");
scanf("%d",&(*p1).num);
printf("please enter the score:");
scanf("%d",&(*p1).score);
head=NULL;
n=0;
while(p1->num)
{
n++;
if(1==n)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("please enter the num:");
scanf("%d",&p1->num);
printf("please enter the score:");
scanf("%d",&p1->score);
}
p2->next=NULL;
return (head);//返回头指针//
}
void print(struct student *head)
{
struct student *p;
printf("\nThere are %d records!\n\n",n);
p=head;
int i=0;
printf("共有%d个学生\n",n);
if(head)
{
do
{
printf("学号为%d,他的分数为%d\n",p->num,p->score);
p=p->next;
}
while(p);
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(NULL==head)//如果头结点指向NULL,这是一个空链表//
{
printf("您并没有输入学生信息");
goto END;
}
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(head=p1)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
n=n-1;
printf("删除成功\n");
}
else
{
printf("没有找到该学生的信息\n");
}
printf("succee\n,现在共有%d个学生的信息",n);
END:
printf("并没有创建学生信息");
return head;
}
楼主的代码很乱,读起来费劲,重写了大部分代码,供参考。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
typedef struct student
{
int num ;
int score ;
struct student * next ;
} node ;
int n ;
node * creat(void)
{
int num , score ;
node * head , * p1 , * p2 ;
head = NULL ;
n = 0 ;
system ("color 0C") ;
for(;;) {
printf("please enter the num : ") ;
scanf("%d" , & num) ;
if (! num) break ; // 输入 0 结束录入
printf("please enter the score : ") ;
scanf("%d" , & score) ;
p2 = (node *) malloc(sizeof(node)) ;
if(p2 != NULL) {
p2 -> num = num ;
p2 -> score = score ;
p2 -> next= NULL ;
if(! n) {
head = p2 ;
} else {
p1 -> next = p2 ;
}
p1 = p2 ;
n ++ ;
} else {
fprintf(stderr , "malloc() failure !\n") ;
break ;
}
}
return head ;
}
node * del(node * head , const int num)
{
node * p1 , * p2 ;
bool f ;
f = false ;
if (head != NULL) {
p1 = head ;
if (head -> num == num) {
head = head -> next ;
f = true ;
} else {
p2 = p1 -> next ;
while(p2 != NULL) {
if (p2 -> num == num) {
p1 -> next = p2 -> next ;
p1 = p2 ;
f = true ;
break ;
}
p1 = p2 ;
p2 = p1 -> next ;
}
}
if(f) {
free(p1) ;
n -- ;
printf("删除成功\n") ;
printf("succee , 现在共有 %d 个学生的信息\n", n) ;
} else {
printf("没有找到该学生的信息\n") ;
}
} else {
printf("链表为空!\n") ;
}
return head ;
}
void print(node * head)
{
node * p ;
printf("\n") ;
printf("There are %d records!\n" , n) ;
printf("共有%d个学生\n" , n) ;
p = head ;
if(head != NULL) {
while(p != NULL) {
printf("学号为%d,他的分数为%d\n" , p -> num , p -> score) ;
p = p -> next ;
}
} else {
printf("链表为空!\n") ;
}
}
main(void)
{
node * stu , * p ;
int num ;
stu = creat() ;
p = stu ;
print(p) ;
printf("\n\n") ;
printf("请输入您要删除的学生信息:") ;
scanf("%d", & num) ; // 注意,原来的代码这里有误
print(del(p , num)) ;
system("pause") ;
}
谢谢啦
页:
[1]