THẦY ƠI? THAM KHẢO DÙM EM ĐOẠN CODE NÀY. CHẠY ĐƯỢC MÀ KO TRA ĐƯỢC NGHĨA. THẦY TỐI ƯU CHO EM LUÔN NHA THẦY.

Code:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define M 26 // so Buckets
// dinh nghia cau truc tu dien
typedef struct TuDien
{
char tu[12];
char nghia[100];
};
// dinh nghia cau truc nut cua buckets
typedef struct node
{
TuDien key;
struct node *next;
} Nodetype;
//dinh nghia nut con tro kieu Nodetype
typedef Nodetype *Nodeptr;
//mang con tro Bucket gom M bucket
Nodeptr Bucket[M];
//hàm này de? kho?i tao ba?ng ba(m
void Initialize()
{
for(int i=0;i<M;i++)
Bucket[i]=NULL;
}
//hàm ba(m
int HashFunc(char c)
{
if (c>=97) c=c-32;
return (c%65);
}
//hàm này de? tao nút có chia khóa k,tra? ve` kieu con tro? Nodeptr
Nodeptr MakeNode(TuDien k)
{
Nodeptr p;
p=(Nodeptr) malloc(sizeof(Nodetype));
p->key=k;
p->next=NULL;
return p;
}
// hàm này dùng de? chèn tu vào Bucket, sau dó sa'p xe'p theo thu tu ta(ng
void InsertListOrder(Nodeptr &Head,Nodeptr G)
{
Nodeptr P, Q;
P = Head;
while (P != NULL)
{
if (strcmp(P->key.tu,G->key.tu)>0) break;
Q = P;
P = Q->next;
}
if (P == Head)
{
G->next = Head;
Head = G;
}
else
{
G->next = Q->next;
Q->next = G;
}
}

//hàm này dùng de? chèn khóa k vào Bucket thu' b
void Place(int b,TuDien k)
{
Nodeptr p,t;
p=Bucket[b];
t=MakeNode(k);
if (p==NULL) Bucket[b]=t;
else InsertListOrder(Bucket[b],t);
}
//hàm này dùng de? chèn khóa k vào bang ba(m
void Insert(TuDien k)
{
int b=HashFunc(k.tu[0]);
Place(b,k);
}
//duyet Bucket
void TraverseBucket(int b)
{
Nodeptr p;
p=Bucket[b];
while (p!=NULL)
{
printf("%3s,",p->key.tu);
p=p->next;
}
}
//duyet toàn bo bang bam
void Traverse()
{
for(int b=0;b<M;b++)
{
printf("\nBucket[%d]:",b);
TraverseBucket(b);
}
}
//hàm này dùng de tra nghia cua 1 tu trong tu dien
void TraNghia(char *s)
{
int b=HashFunc(s[0]);
Nodeptr p = Bucket[b];
while (p!=NULL && strcmp(p->key.tu,s)!=0)
p=p->next;
if (p==NULL) printf("\n\nKhong tim thay \"%s\" trong tu dien",s);
else
printf("\n\nNghia cua tu \"%s\":%s",s,p->key.nghia);
}
//hàm này dùng de cap nhat nghia cho 1 tu
void CapNhatTu(char *s,char *s1)
{
fflush(stdin);
int b= HashFunc(s[0]);
Nodeptr p=Bucket[b];
while (p!=NULL && strcmp(p->key.tu,s)!=0)
p=p->next;
if (p==NULL) printf("\n\nKhong tim thay \"%s\" trong tu dien",s);
else
{
strcat(p->key.nghia,",");
strcat(p->key.nghia,s1);
}
}
void Pop(Nodeptr &p) // lay phan tu dau xau
{
TuDien k;
Nodeptr q;
q=p;
p=p->next;
q->next=NULL;
k=q->key;
free(q);
}
void DelAfter(Nodeptr &q) //xoa nut sau q
{
Nodeptr p;
p=q->next;
q->next=p->next;
p->next=NULL;
free(p);
}
//xóa 1 tu trong tu dien
void XoaTu(char *s)
{
int b=HashFunc(s[0]);
Nodeptr p,q;
p=Bucket[b];
while (p!=NULL && strcmp(p->key.tu,s)!=0)
{
q=p;
p=p->next;
}
if (p==NULL) printf("\n\nKhong tim thay \"%s\" trong tu dien",s);
if (p==Bucket[b]) Pop(Bucket[b]);
else DelAfter(q);
}
//menu cua chuong trình
void MENU()
{
printf("\n\n\t\t\t\tTU DIEN ANH-VIET");
printf("\n\nCac chuc nang chinh cua tu dien:");
printf("\n\n1.Them tu moi vao tu dien");
printf("\n\n2.Xoa mot tu khoi tu dien");
printf("\n\n3.Cap nhat tu");
printf("\n\n4.Tra nghia cua tu");
printf("\n\n0.Thoat khoi tu dien");
printf("\n\nBan chon chuc nang nao:");
}
int main()
{
char *s,*s1;
TuDien td;
int choice;
Initialize();
do
{
MENU();
scanf("%d",&choice);
switch(choice)
{
case 1:
//clrscr();
printf("THEM TU MOI VAO TU DIEN");
printf("\n\nNhap tu can them:");fflush(stdin);
gets(td.tu);
printf("\n\nNhap nghia cua tu \"%s\":",td.tu);
fflush(stdin);
gets(td.nghia);
Insert(td);
break;
case 2:
//clrscr();
printf("\n\nNhap tu can xoa:");fflush(stdin);
gets(s);
XoaTu(s);
break;
case 3:
// clrscr();
printf("CAP NHAT NGHIA CUA TU");
printf("\n\nNhap tu can cap nhat:");
gets(s);
printf("\n\nNhap nghia cap nhat cua \"%s\":");
fflush(stdin);
gets(s1);
CapNhatTu(s,s1);
break;
case 4:
// clrscr();
printf("TRA NGHIA CUA TU");
printf("\n\nNhap tu can tra nghia:");fflush(stdin);
gets(s);
TraNghia(s);
break;
}
} while (choice!=0);
return 0;
}