Em có đề bài yêu cầu thế này :
Dùng danh sách đặc để lưu 1 danh sách sinh viên với mỗi sinh viên có mã SV , họ tên , năm sinh
- Viết khai báo thích hợp
- Nhập một danh sách SV từ bàn phím và hiển thị danh sách SV lên màn hình
- Sắp xếp danh sách theo mã SV
Code của em :
Code:
#include<stdio.h>
#include<conio.h>
#define Maxlength 30
typedef int position ;
typedef struct {
char mssv[5];
char hoten[40];
int namsinh;
}ElementType;
typedef struct {
ElementType Element[Maxlength] ;
position Last ;
}List ;
void MakeNull_List( List *l )
{
l->Last = 0 ;
}
int Empty_List ( List l )
{
return (l.Last==0);
}
int Full_List ( List l )
{
return (l.Last==Maxlength);
}
position First( List l )
{
return 1;
}
position EndList ( List l )
{
return ( l.Last+1);
}
position Next(position p , List l )
{
return ( p+1 );
}
position Previous ( position p , List l )
{
return (p-1);
}
ElementType Retrive ( position p , List l )
{
return l.Element[p-1] ;
}
void Insert_List ( ElementType X , position p , List *l )
{ int i=0 ;
if (l->Last==Maxlength ) printf("\n Danh sach day ! Khong the them ");
else if ( (p<1) || (p>l->Last+1) ) printf("\n Vi tri khong hop le " );
else {
for(i=l->Last ;i>=p ;i--)
l->Element[i]=l->Element[i-1];
l->Last++;
l->Element[p-1]=X ;
}
position Locate(ElementType X,List l )
{
position p ;
int found=0 ;
p=First(l);
while( (p!=EndList(l)) && (found==0) )
{
if ( Retrive(p,l)==X )
found=1;
else p=Next(p,l);
}
return p ;
}
void Delete_List ( position p , List &l)
{
if ( (p<0) || (p>l->Last+1 ) )
printf("\n Vi tri khong hop le " );
else if ( Empty_List(&l) ) printf("\n Danh sach rong ! " );
else
{
position i;
for ( i=p ;i<l->Last;i++ )
{
l->Element[i-1]=l->Element[i];
}
l->Last--;
}
}
void Read_list(List &l)
{ ElementType x;
int n ;
MakeNull_List(l);
printf("\n Nhap vao so luong sinh vien : ");scanf("%d",&n);
int i;
for(i=1 ; i<=n ;i++ )
{
printf("Nhap Sinh vien thu %d :" ,i);
printf("\n Nhap ma so ");
fflush(stdin);
gets(x->mssv);
Insert_List(x->mssv,EndList(&l),l);
printf("\n Nhap ho ten");
fflush(stdin);
gets(x->hoten);
Insert_List(x->hoten,EndList(&l),l);
printf("\n Nhap nam sinh: ");
scanf("%d",x->namsinh);
Insert_List(x->namsinh,EndList(&l),l);
}
}
void Print_List ( List l )
{
position p;
p=First(l);
printf("\n Danh sach duoc in ra : ");
while (p!=EndList(l))
{
printf("\n Sinh vien thu %d :",p);
printf("\n Ma so SV : %s ",Retrive(p->Element->mssv,l);
printf("\n Ho ten SV : %s :",Retrive(p->Element->hoten,l);
printf("\n Nam sinh : %d :",Retrive(p->Element->namsinh,l);
p=Next(p,l);
}
printf("\n Ket Thuc in danh sach " );
}
void main()
{
List L ;
MakeNull_List(*L);
clrscr();
Read_List(L);
Print_List(L);
getch();
}
Vấn đề nằm ở chỗ khi em biên dịch nó báo sai lỗi cú pháp ở hàm Locate
position Locate(ElementType X,List l )
{
position p ;
int found=0 ;
p=First(l);
while( (p!=EndList(l)) && (found==0) )
{
if ( Retrive(p,l)==X )
found=1;
else p=Next(p,l);
}
return p ;
}
Mọi người giúp mình xem mình khai báo vậy có chính xác chưa ? và Code cần khắc phục hay Edit lại ở những điểm nào ? em mới học CTDL mong được mọi người góp ý ! Thanks mọi người