Lỗi Danh Sách Liên kết (Khi Xuất Ra )?

Mọi người thông cảm do em tìm mãi lỗi ở chỗ xuất ra không được nên lên đây mạn phép post len

#include <conio.h>
#include <stdio.h>
#include <malloc.h>
// tạo đối tượng Sinh Viên ( 1 NOTE )
struct SinhVien {
	char FullName[1000];
	char Old[3]; 
	char SEX[5];
	float Toan,Ly,Hoa;
};
typedef struct SinhVien SV;
// tao Danh sách để quản lý .
struct NhaTruong {
	SV Person;
	NhaTruong *next; // con trỏ next .
};
typedef struct NhaTruong Home;
void Produce(Home *&Node){
	Node = (Home *)malloc(sizeof(Home));
}
void main() {
	int chon;
	Home *dssv; // khai báo 1 danh sach sinh vien
	Produce(dssv);
	Home *HEAD = NULL;
	Home *Tail = NULL;
	HEAD = dssv;
	while(1)
	{
		printf("Enter your FullName: "); gets(dssv->Person.FullName);
		printf("Enter your Old: "); gets(dssv->Person.Old);
		printf("Enter your SEX: "); gets(dssv->Person.SEX);
		fflush(stdin);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Toan);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Ly);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Hoa);
		Produce(dssv->next); // cấp phát cho con NODE tiếp theo
		dssv = dssv->next; // cho NODE 1 >> nối NODE 2.
		printf("You want Enter Information!");scanf("%d", &chon);
		fflush(stdin);
		if(chon == 0)
		{
			dssv = NULL;
			break;
		}
	}
	dssv = HEAD;
	while( dssv != NULL)
	{
		printf("\nHo Va Ten: %s", dssv->Person.FullName);
		dssv = dssv->next;
	}
	getch();
}

Mong mọi người giúp em @@

bạn trả lời đi rồi tính tiếp :slight_smile:

#include <conio.h>
#include <stdio.h>
#include <malloc.h>
struct SinhVien {
	char FullName[1000];
	char Old[3]; 
	char SEX[5];
	float Toan,Ly,Hoa;
};
typedef struct SinhVien SV;
struct NhaTruong {
	SV Person;
	NhaTruong *next; 
};
typedef struct NhaTruong Home;
void Produce(Home *&Node){
	Node = (Home *)malloc(sizeof(Home));
}
int main() {
	int chon;
	Home *dssv; 
	Produce(dssv);
	Home *HEAD = NULL;
	Home *Tail = NULL;
	HEAD = dssv;
	while(1)
	{
		printf("Enter your FullName: "); gets(dssv->Person.FullName);
		printf("Enter your Old: "); gets(dssv->Person.Old);
		printf("Enter your SEX: "); gets(dssv->Person.SEX);
		fflush(stdin);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Toan);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Ly);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Hoa);
		
		printf("You want Enter Information!");scanf("%d", &chon);
		fflush(stdin);
		if(chon == 0)
		{
			dssv = NULL;
			break;
		}
		Produce(dssv->next); 
		dssv = dssv->next; 
	}
	dssv = HEAD;
	while( dssv != NULL)
	{
		printf("\nHo Va Ten: %s", dssv->Person.FullName);
		dssv = dssv->next;
	}
	return 0;}

hỏi người dùng trước khi cấp phát chứ :smile:

hỏi gì ? mình muốn chừng nào ko nhập nữa thì thoát rồi xuất luôn

if( chon == 0 ) để thoát cái vòng nhập thông tin còn cái
dssv = NULL; là để node cuối hiện tại trỏ tới NULL
1 >> 2 >> 3 >> NULL ý

Vậy thì chương trình bị lỗi gì? Chụp lỗi xem… Mình chỉ thấy nó bị thừa 1 node cuối khi xuất thôi, cách fix thì mình post code ở trên rồi

không bị lỗi mà bị như cái kiểu nhập 3 cái mà nó xuất luôn cái NULL

Đó là bị dư cái node cuối đấy, code của mình post ở trên đã fix rồi đó. Lỗi là do Học cấp phát sẵn rồi sau đó hỏi người dùng có nhập tiếp không, nếu người dùng không nhập tiếp thì node cuối đó bị thừa mà chưa gán được giá trị nào vào.

1 Like

cảm ơn Hoàng nhé thanks

Cho Học hỏi trước hàm while Học có cấp phát 1 vùng nhớ cho nó rồi vùng nhớ đó để nhập các dữ liệu nếu người dùng ko muốn thì người dùng bấm ‘0’ . nhưng cái node chung ta vừa nhập tại sao lại không trở thành NULL ? trong khi chúng ta ko hề cấp phát cho nó .

mình vừa tìm ra bug nữa có liên quan tới câu hỏi :smile: lỗi này là khi mình chỉ nhập một sinh viên thôi và không muốn nhập nữa, khi in ra sẽ báo lỗi. Code mình fix bên dưới.

Về cái này thì do Học làm sai chỗ

dssv=NULL;

Làm như thế này thì thằng dssv không còn giữ một địa chỉ nào thôi (biến dssv chỉ là một biến tạm để chúng ta làm việc với dslk thôi), không làm ảnh hưởng đến danh sách liên kết đâu, nên node cuối cùng chúng ta nhập sẽ không bị gì cả
Cách làm đúng là : dssv->next=NULL.
Và khi đó tail chính là thằng dssv luôn. (tại thấy học khai báo tail mà chưa làm gì cả)

#include <conio.h>
#include <stdio.h>
#include <malloc.h>
struct SinhVien {
	char FullName[1000];
	char Old[3]; 
	char SEX[5];
	float Toan,Ly,Hoa;
};
typedef struct SinhVien SV;
struct NhaTruong {
	SV Person;
	NhaTruong *next; 
};
typedef struct NhaTruong Home;
void Produce(Home *&Node){
	Node = (Home *)malloc(sizeof(Home));
}
int main() {
	int chon;
	Home *dssv; 
	Produce(dssv);
	Home *HEAD = NULL;
	Home *Tail = NULL;
	HEAD = dssv;
	while(1)
	{
		printf("Enter your FullName: "); gets(dssv->Person.FullName);
		printf("Enter your Old: "); gets(dssv->Person.Old);
		printf("Enter your SEX: "); gets(dssv->Person.SEX);
		fflush(stdin);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Toan);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Ly);
		printf("Enter your point: "); scanf("%d", &dssv->Person.Hoa);
		
		printf("You want Enter Information!");scanf("%d", &chon);
		//printf("%p",dssv);
		fflush(stdin);
		if(chon == 0)
		{
			dssv->next = NULL;
			break;
		}
		Produce(dssv->next); 
		dssv = dssv->next; 
	}
	dssv = HEAD;
	while( dssv != NULL)
	{
		printf("\nHo Va Ten: %s", dssv->Person.FullName);
		dssv = dssv->next;
	}
	return 0;
} 

haha Học biết mà hèn chi cái khúc dssv = NULL ; thấy ngộ ngộ

mà Cho Học hỏi nếu sửa lại dssv->next = NULL cũng như trên của học vậy sao cái trên của Hoc chạy ko ra nhỉ

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