CÀI ĐẶT TỪ ĐIỂN ĐƠN GIẢN BẰNG TẬP HỢP
Code:
#include <stdio.h>
#include <conio.h>
#define MaxLength 24
typedef char ElementType;
typedef int Position;
typedef struct Set{
ElementType Data[MaxLength];
Position Last;
};
//khoi tao tap hop rong
void MakeNull(Set *L) {
(*L).Last=0;
}
//kiem tra tap hop rong
char Empty(Set L) {
return (L.Last == 0);
}
//ham kiem tra thanh vien cua tap hop
int Member(ElementType x, Set L) {
Position P = 0, Found = 0;
while ((P < (L.Last)) && (Found == 0))
{
if ((L.Data[P]) == x)
Found = 1;
else
P++;
}
return Found;
}
//kiem tra tap hop day
char Full(Set L) {
return (L.Last == MaxLength);
}
//them phan tu vao tap hop
void Insert(ElementType x, Set *L) {
if (Full(*L))
printf("Tap hop day");
else
if (Member(x,*L)==0) {
(*L).Data[(*L).Last] = x;
(*L).Last++;
}
else
printf ("\nPhan tu da ton tai trong tu dien");
}
//xoa phan tu trong tap hop
void Delete(ElementType x, Set *L) {
if (Empty(*L))
printf("Tap hop rong!");
else {
Position Q=0;
while ((Q<(*L).Last)&& ((*L).Data[Q]!=x))
Q++;
if ( (*L).Data[Q]==x) {
for (int i=Q;i<(*L).Last-1;i++)
(*L).Data[i]=(*L).Data[i+1];
(*L).Last--;
}
}
}
//doc tu dien
void Read(Set *L) {
char ch;
printf("\nNhap den nhan ESC thi ket thuc: ");
do {
ch = getche();
if(ch!=27)
Insert(ch,L);
}while(ch!=27);
}
//xuat tu dien
void Write(Set L) {
for(int i = 0; i<L.Last; i++)
printf("%c ",L.Data[i]);
}
//chuong trinh chinh
void main() {
clrscr();
Set L;
MakeNull(&L);
Read(&L);
printf("\nTu dien vua nhap:\n");
Write(L);
getch();
}