#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct sinhvien{
string hoten,masv;
int ns,tongdiem;
};
struct node{
sinhvien sv;
node *next;
};
typedef node *tro;
///ham khoi tao danh sach rong
void initializer(tro &L ){
L=NULL;
}
///kiem tra danh sach rong
int Empty(tro L){
return L==NULL;
}
///Nhap thong tin cho mot sinhvien
void nhapSV(sinhvien &sv){
cout<<"nhap ma sv:";fflush(stdin);getline(cin,sv.masv);
if(sv.masv.compare("***")==0) return;
cout<<"nhap ho ten:";fflush(stdin);getline(cin,sv.hoten);
cout<<"nhap nam sinh:";cin>>sv.ns;
cout<<"nhap tong diem:";cin>>sv.tongdiem;
}
///Nhap danh sach sv
void nhapDS(tro &L){
initializer(L);
sinhvien x;
tro Q,P;
int i=1;
do{
cout<<"Nhap sv thu "<<i<<endl;
nhapSV(x);
if(x.masv.compare("***")==0) return;
///ket thuc khi ma nhap la "***"
P=new(node);
P->sv=x;
P->next=NULL;
if(L==NULL) L=P;
else Q->next=P; //line 44
Q=P; //line 45
++i;
}while(1);
}
void xuatDS(tro L){
if(L==NULL){
cout<<"DS rong\n";
return;
}
cout<<setw(5)<<"STT";
cout<<setw(10)<<"Ma sv";
cout<<setw(25)<<"Ho Ten";
cout<<setw(10)<<"Nam sinh";
cout<<setw(10)<<"Diem TK"<<endl;
tro Q =L;
int i=0;
while(Q!=NULL){
sinhvien x=Q->sv;
cout<<setw(10)<<i+1<<endl;
cout<<setw(5)<<x.masv;
cout<<setw(10)<<x.hoten;
cout<<setw(25)<<x.ns;
cout<<setw(10)<<x.tongdiem;
Q=Q->next;
}
}
int main()
{
tro L;
return 0;
}
Chả là em đang code lại bài danh sách móc nối đơn trong sách,nhưng đến line 44,45
thì e không hiểu “Tại sao con trỏ P lại gán cho Q->Next xong lại gán trỏ P cho Q ở line 45”, anh chị có thể diễn giải giúp e ko, e cảm ơn ạ