user-0 提问: 关于insert函数中在头指针前插入结点的求助

回复:
user: user-0
content: 非常谢谢，我能问下为什么是这么操作呢？新的指针指向为什么不能赋给头指针？  引用 1 楼 sghcpt 的回复:楼主：你应该在insert函数中的最后*stu = *head这行代码用错了，下面代码可参考一下： #include <stdio.h> #include <stdlib.h> struct Student{ 	int num; 	float score; 	Student *prev; 	Student *next; };  Student *input(); void print(Student *stu); void del(Student *stu, float n); void insert(Student **stu, int m, char direc, Student *ins);  int main() { 	Student *stud, in, *inser; 	float del_score; 	int insert_num; 	char dir; 	stud = input(); 	print(stud);  	printf("Please enter insert student info: "); 	scanf("%d %f", &in.num, &in.score); 	getchar();  	inser = ∈ 	printf("Please enter insert num: "); 	scanf("%d", &insert_num); 	getchar();  	printf("Please enter F or B: ");                         //F是前插，B是后插。 	scanf("%c", &dir); 	getchar();  	insert(&stud, insert_num, dir, inser); 	print(stud);  	system("pause"); 	return 0; }  Student *input() { 	Student *p1, *p2, *head; 	int n = 0; 	p1 = p2 = (Student*)malloc(sizeof(Student)); 	printf("Please enter No.%d student info: ", n + 1); 	scanf("%d %f", &p1->num, &p1->score); 	getchar(); 	head = NULL; 	while (p1->num != 0){ 		n++; 		if (n == 1){ 			head = p1; 			p1->prev = NULL; 		} 		else{ 			p2->next = p1; 			p1->prev = p2; 		} 		p2 = p1; 		p1 = (Student*)malloc(sizeof(Student)); 		printf("Please enter No.%d student info: ", n + 1); 		scanf("%d %f", &p1->num, &p1->score); 		getchar(); 	} 	p2->next = NULL; 	return head; }  void print(Student *stu) { 	Student *p; 	for (p = stu; p != NULL; p = p->next) 		printf("%d %.2f\n", p->num, p->score); }  void del(Student *stu, float n) { 	Student *p, *head; 	for (p = stu, head = p; p != NULL; p = p->next) 	if (p->score == n){ 		if (p == head){ 			head = p->next; 			p->next->prev = NULL; 			head->next = p->next->next; 		} 		else if (p->next == NULL) 			p->prev->next = NULL; 		else{ 			p->prev->next = p->next; 			p->next->prev = p->prev; 		} 	} 	*stu = *head; }  void insert(Student **stu, int m, char direc, Student *ins) { 	Student *p, *head; 	for (p = *stu, head = p; p != NULL; p = p->next){ 		if (p->num == m){ 			if (direc == 'F'){ 				if (p == head){ 					ins->next = p; 					ins->prev = NULL; 					head = ins; 				} 				else{ 					ins->prev = p->prev; 					p->prev->next = ins; 					ins->next = p; 					p->prev = ins; 				} 			} 			else if (direc == 'B'){ 				if (p->next == NULL){ 					p->next = ins; 					ins->next = NULL; 					ins->prev = p; 				} 				else{ 					ins->next = p->next; 					p->next = ins; 					ins->prev = p; 				} 			} 		} 	} 	*stu = head; }

user: user-1
content: 楼主：你应该在insert函数中的最后*stu = *head这行代码用错了，下面代码可参考一下： #include <stdio.h> #include <stdlib.h> struct Student{ 	int num; 	float score; 	Student *prev; 	Student *next; };  Student *input(); void print(Student *stu); void del(Student *stu, float n); void insert(Student **stu, int m, char direc, Student *ins);  int main() { 	Student *stud, in, *inser; 	float del_score; 	int insert_num; 	char dir; 	stud = input(); 	print(stud);  	printf("Please enter insert student info: "); 	scanf("%d %f", &in.num, &in.score); 	getchar();  	inser = ∈ 	printf("Please enter insert num: "); 	scanf("%d", &insert_num); 	getchar();  	printf("Please enter F or B: ");                         //F是前插，B是后插。 	scanf("%c", &dir); 	getchar();  	insert(&stud, insert_num, dir, inser); 	print(stud);  	system("pause"); 	return 0; }  Student *input() { 	Student *p1, *p2, *head; 	int n = 0; 	p1 = p2 = (Student*)malloc(sizeof(Student)); 	printf("Please enter No.%d student info: ", n + 1); 	scanf("%d %f", &p1->num, &p1->score); 	getchar(); 	head = NULL; 	while (p1->num != 0){ 		n++; 		if (n == 1){ 			head = p1; 			p1->prev = NULL; 		} 		else{ 			p2->next = p1; 			p1->prev = p2; 		} 		p2 = p1; 		p1 = (Student*)malloc(sizeof(Student)); 		printf("Please enter No.%d student info: ", n + 1); 		scanf("%d %f", &p1->num, &p1->score); 		getchar(); 	} 	p2->next = NULL; 	return head; }  void print(Student *stu) { 	Student *p; 	for (p = stu; p != NULL; p = p->next) 		printf("%d %.2f\n", p->num, p->score); }  void del(Student *stu, float n) { 	Student *p, *head; 	for (p = stu, head = p; p != NULL; p = p->next) 	if (p->score == n){ 		if (p == head){ 			head = p->next; 			p->next->prev = NULL; 			head->next = p->next->next; 		} 		else if (p->next == NULL) 			p->prev->next = NULL; 		else{ 			p->prev->next = p->next; 			p->next->prev = p->prev; 		} 	} 	*stu = *head; }  void insert(Student **stu, int m, char direc, Student *ins) { 	Student *p, *head; 	for (p = *stu, head = p; p != NULL; p = p->next){ 		if (p->num == m){ 			if (direc == 'F'){ 				if (p == head){ 					ins->next = p; 					ins->prev = NULL; 					head = ins; 				} 				else{ 					ins->prev = p->prev; 					p->prev->next = ins; 					ins->next = p; 					p->prev = ins; 				} 			} 			else if (direc == 'B'){ 				if (p->next == NULL){ 					p->next = ins; 					ins->next = NULL; 					ins->prev = p; 				} 				else{ 					ins->next = p->next; 					p->next = ins; 					ins->prev = p; 				} 			} 		} 	} 	*stu = head; }

user: user-2
content: 数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633