|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问为什么用while循序反复在链表里插入节点会让数据库爆掉呢?第一次循环时候不会,第二次就会,而且第二次调用添加节点的函数时候,并没有保存上一次添加的节点。
问题就出现我标成绿色的那个地方,本来如果不加循环的时候整个程序是正常运行的。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student) // LEN是结构体大小
struct student *create(); //申明create函数,作用是创建链表
void print(struct student *head); //申明print函数,作用是打印链表
struct student *unwanted(struct student *head, int x); //申明unwanted函数,作用是在链表里删除掉第x节点
struct student *add(struct student *head, struct student *stunew); //申明add函数,作用是添加一个节点
struct student //定义结构体
{
int num;
float score;
struct student *next;
};
int n; //作用是记录存放了多少数据。
void main()
{
struct student *stu;
int x;
int t1, t2;
stu = create();
print( stu );
system("pause");
printf("Is there a student to delete?(1/0):\n"); //询问是否需要删除节点,以1/0来判断,1是需要,0是不需要
scanf("%d",&t1);
if(t1 == 1) //如果输入1,就进行删除节点操作
{
printf("input the the number of unwanted student:\n");
scanf("%d", &x );
system("pause");
print(unwanted(stu, x));
}
system("pause");
while (t2) //这个地方我试着用while循环来反复执行插入操作,想只要不输入0就一直循环,但
第二次循环就爆掉了,就一直反复打印下面add函数里面我标成红色的那句
{ printf("Is there a new student to add?(1/0):\n");
scanf("%d",&t2);
if(t2 == 1)
{
struct student a;
printf("Plese input the number for the new student:");
scanf("%d",&a.num);
printf("Plese input the score for the new student:");
scanf("%f",&a.score);
print(add(stu, &a));
}
}
printf("\n\n");
system("pause");
}
struct student *create() //定义create函数
{
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN); //用malloc函数来分配一段大小为LEN的内存空间,返回的是这段内存空间的头指针
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 students.\n\n", n);
// p = head;
if( NULL != head )
{
do
{
printf("No. :%d Score: %f\n", head->num, head->score);
head = head->next;
}while( NULL != head );
}
struct student *unwanted(struct student *head, int x) //定义unwanted函数
{
struct student *u1, *u2;
if(head == NULL)
{
printf("It is empty!\n");
goto END;
}
u1 = head;
while((u1->num != x) && (u1->next != NULL) )
{
u2 = u1;
u1 = u1->next;
}
if(u1->num == x)
{
if(u1 == head)
{
head = u1->next;
n = n-1;
}
else
{
u2->next = u1->next;
n = n-1;
}
}
else
printf("There is no %d\n", x);
END:
return head;
}
struct student *add(struct student *head, struct student *stunew) //定义add函数
{
struct student *ap, *p1, *p2;
p1 = head;
ap = stunew;
if(head==NULL)
{
head = ap;
ap->next = NULL;
}
while((ap->num > p1->num) && (p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if(ap->num <= p1->num)
{
if(p1 == head)
{
head = ap;
ap->next = p1;
}
else
{
p2->next = ap;
ap->next = p1;
}
n = n+1;
}
else
{
p1->next = ap;
ap->next = NULL;
n = n+1;
}
printf("The new student's number is %d\n", ap->num);
return head;
}
|
|