|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问各位大师 为什么我调试到执行unwanted函数里面 然后会出现非法访问地址 0xc0000005的提示呢?
然后如果我吧unwanted函数改成指针函数void *unwanted(struct student *head, int x); 吧函数里面的后两个判断里面的printf函数去掉,返回指针head, 然后在主函数里面改成print(unwanted(stu,x));就可以正常打印后两个判断的状况,但是如果是前两种判断的情况,仍然会有问题。
编译是对的。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student) // student结构的大小
struct student *creat(); //创建链表
void print(struct student *head); //打印链表
void unwanted(struct student *head, int x); //delete the node x in the struct student
struct student
{
int num;
float score;
struct student *next;
};
int n; //全局变量,用来记录存放了多少数据。
void main()
{
struct student *stu;
int x;
stu = creat();
print( stu );
system("pause");
printf("input the the number of unwanted student information:\n");
scanf("%d", &x );
system("pause");
unwanted(stu, x);
printf("\n\n");
system("pause");
}
struct student *creat() //定义create函数
{
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN); // LEN是student结构的大小
printf("Please enter the num :");
scanf("%d", &p1->num);
printf("Please enter the score :");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while( 0 != p1->num )
{
n++;
if( 1 == n )
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nPlease enter the num :");
scanf("%d", &p1->num);
if(p1->num != 0)
{
printf("Please enter the score :");
scanf("%f", &p1->score);
}
}
p2->next = NULL;
return head;
}
void print(struct student *head) //定义print函数
{
// struct student *p;
printf("\nThere are %d records!\n\n", n);
// p = head;
if( NULL != head )
{
do
{
printf("学号为 %d 的成绩是: %f\n", head->num, head->score);
head = head->next;
}while( NULL != head );
}
}
void unwanted(struct student *head, int x) //定义unwanted函数
{
struct student *u1, *u2;
if(head == NULL)
printf("It is empty!\n");
u1 = head;
while((u1->num != x) || u1 != NULL )
{
u2 = u1;
u1 = u1->next;
}
if(u1 == NULL)
{
printf("There is no %d student found!\n", x);
}
else if(u1 == head)
{
head = u1->next;
n = n-1;
printf("The modified edition is:\n");
print(head);
}
else
{
u2->next = u1->next;
n = n-1;
printf("The modified edition is:\n");
print(head);
}
}
|
|