Cái này là bài tập nhỏ mình làm trong lớp, nay post code lên cho anh em học hỏi thêm, từ điển này còn thiếu nhiều tính năng lắm, bạn nào giỏi thì tối ưu lên nhé.
PHP 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 để khởi tạo bảng băm
void Initialize()
{
for(int i=0;i<M;i++)
Bucket[i]=NULL;
}
//hàm băm
int HashFunc(char c)
{
if (c>=97) c=c-32;
return (c%65);
}
//hàm này để tạo nút có chứa khóa k,trả về kiểu con trỏ 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 để chèn từ vào Bucket, sau đó sắp xếp theo thứ tự tă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 để chèn khóa k vào Bucket thứ 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 để chèn khóa k vào bảng băm
void Insert(TuDien k)
{
int b=HashFunc(k.tu[0]);
Place(b,k);
}
//duyệt Bucket
void TraverseBucket(int b)
{
Nodeptr p;
p=Bucket[b];
while (p!=NULL)
{
printf("%3s,",p->key.tu);
p=p->next;
}
}
//duyệt toàn bộ bảng băm
void Traverse()
{
for(int b=0;b<M;b++)
{
printf("\nBucket[%d]:",b);
TraverseBucket(b);
}
}
//hàm này dùng để tra nghĩa của 1 từ trong từ điển
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 để cập nhật nghĩa cho 1 từ
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 từ trong từ điển
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 của chương 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;
}