Hỏi về bài tập danh sách liên kết đơn

Mọi người cho em hỏi đoạn Code sai ở đâu ạ , đây là đề bài:

#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<string.h>
struct AllSchool
{
	char name[20];
	int official_count;
	int student_count;
	struct AllSchool *next;
};
typedef struct AllSchool *School;

School AddHead(School List,char ten[], int socanbo, int sosinhvien) {
	School p = (School)malloc(sizeof(struct AllSchool));
	strcpy(p->name, ten);
	p->official_count = socanbo;
	p->student_count = sosinhvien;
	p->next = List;
	List = p;
	return List;
}
//Nhap truong
void Input(School List) {
	char stop;
	char ten[20];
	int socanbo, sosinhvien, dem = 1;
	do {
		printf("Nhap thong tin truong thu %d : \n", dem );
		printf("Nhap ten truong : "); 
		fflush(stdin);
		gets(ten);
		printf("Nhap so can bo : "); scanf("%d", &socanbo);
		printf("Nhap so sinh vien : "); scanf("%d", &sosinhvien);
		AddHead(List,ten,socanbo,sosinhvien);
		dem++;
		printf("\nPress ESC to Stop >< AnyKey to Continue !\n");
		stop = getch();
	} while (stop != 27);
}
//
// void printlist(School List){
// 	for(School p=List;p!=NULL;p=p->next) printf("%s\t",p->name );
// }
//Truong nhieu sinh vien nhat
void MaxStudent(School List) {
	School max = List;
	School p=List;
	while(p->next!=NULL){
		if( (max->student_count)<(p->student_count)  ) max=p;
		p=p->next;
	}
	printf("Truong co nhieu sinh vien nhat la : \n");
	printf("Ten : %s ", max->name);
	printf("So can bo : %d", max->official_count);
	printf("So sinh vien : %d", max->student_count );
}
int main() {
	School List = NULL;
	Input(List);
	MaxStudent(List);
	return 0;
}

Em đã fix được bằng cách thêm & trước các tham số List của hàm , mà không hiểu sao trước giờ code có cần dùng đâu vẫn chạy mà nhỉ @@

Thấy conio thì chắc chắn là VS rồi :slight_smile: C không có cái này đâu.

Giờ bạn xem lại truyền tham biến và tham trị nhé.

4 Likes

Dạ thank bro ạ , em biết cái đó ạ :smiling_face_with_three_hearts:
Mới ngẫm lại và thấy ở những “ bài khác” đó em đều dùng biến con trỏ toàn cục , cho nên lúc thao tác với nó không cần dùng & nó vẫn chạy ạ kkk

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?