#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct SinonimNode {
char sinonim[50];
struct SinonimNode *next;
} SinonimNode;
typedef struct KamusNode {
char kata[50];
SinonimNode *sinonimHead;
struct KamusNode *next;
} KamusNode;
typedef struct Kamus {
KamusNode *head;
} Kamus;
Kamus* buatKamus() {
Kamus
*kamus
= (Kamus
*)malloc(sizeof(Kamus
)); kamus->head = NULL;
return kamus;
}
void tambahSinonim(SinonimNode **head, const char *sinonim) {
SinonimNode
*nodeBaru
= (SinonimNode
*)malloc(sizeof(SinonimNode
)); strcpy(nodeBaru
->sinonim
, sinonim
); nodeBaru->next = NULL;
if (*head == NULL) {
*head = nodeBaru;
} else {
SinonimNode *temp = *head;
while (temp->next != NULL) {
if (strcmp(temp
->sinonim
, sinonim
) == 0) return; temp = temp->next;
}
if (strcmp(temp
->sinonim
, sinonim
) != 0) { temp->next = nodeBaru;
} else {
}
}
}
void tambah(Kamus *kamus, const char *kata, const char *sinonim) {
KamusNode *temp = kamus->head;
while (temp != NULL) {
if (strcmp(temp
->kata
, kata
) == 0) { tambahSinonim(&temp->sinonimHead, sinonim);
return;
}
temp = temp->next;
}
KamusNode
*nodeBaru
= (KamusNode
*)malloc(sizeof(KamusNode
)); nodeBaru->sinonimHead = NULL;
nodeBaru->next = kamus->head;
kamus->head = nodeBaru;
tambahSinonim(&nodeBaru->sinonimHead, sinonim);
}
void ambilSinonim(Kamus *kamus, const char *kata) {
KamusNode *temp = kamus->head;
while (temp != NULL) {
if (strcmp(temp
->kata
, kata
) == 0) { SinonimNode *sinonimTemp = temp->sinonimHead;
while (sinonimTemp != NULL) {
printf("%s", sinonimTemp
->sinonim
); sinonimTemp = sinonimTemp->next;
if (sinonimTemp
!= NULL
) printf(", "); }
return;
}
temp = temp->next;
}
printf("null / belom ditambahkan\n"); }
int main() {
Kamus *kamus = buatKamus();
tambah(kamus, "big", "large");
tambah(kamus, "big", "great");
tambah(kamus, "big", "huge");
tambah(kamus, "big", "fat");
tambah(kamus, "huge", "enormous");
tambah(kamus, "huge", "gigantic");
tambah(kamus, "tall", "big");
tambah(kamus, "tall", "high");
tambah(kamus, "little", "tiny");
tambah(kamus, "tiny", "little");
printf("Sinonim untuk 'big' adalah: "); ambilSinonim(kamus, "big");
printf("Sinonim untuk 'huge' adalah: "); ambilSinonim(kamus, "huge");
printf("Sinonim untuk 'large' adalah: "); ambilSinonim(kamus, "large");
printf("Sinonim untuk 'tall' adalah: "); ambilSinonim(kamus, "tall");
printf("Sinonim untuk 'little' adalah: "); ambilSinonim(kamus, "little");
printf("Sinonim untuk 'tiny' adalah: "); ambilSinonim(kamus, "tiny");
printf("Sinonim untuk 'high' adalah: "); ambilSinonim(kamus, "high");
printf("Sinonim untuk 'gigantic' adalah: "); ambilSinonim(kamus, "gigantic");
printf("Sinonim untuk 'colossal' adalah: "); ambilSinonim(kamus, "colossal");
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdHlwZWRlZiBzdHJ1Y3QgU2lub25pbU5vZGUgewogICAgY2hhciBzaW5vbmltWzUwXTsKICAgIHN0cnVjdCBTaW5vbmltTm9kZSAqbmV4dDsKfSBTaW5vbmltTm9kZTsKCnR5cGVkZWYgc3RydWN0IEthbXVzTm9kZSB7CiAgICBjaGFyIGthdGFbNTBdOwogICAgU2lub25pbU5vZGUgKnNpbm9uaW1IZWFkOwogICAgc3RydWN0IEthbXVzTm9kZSAqbmV4dDsKfSBLYW11c05vZGU7Cgp0eXBlZGVmIHN0cnVjdCBLYW11cyB7CiAgICBLYW11c05vZGUgKmhlYWQ7Cn0gS2FtdXM7CgpLYW11cyogYnVhdEthbXVzKCkgewogICAgS2FtdXMgKmthbXVzID0gKEthbXVzICopbWFsbG9jKHNpemVvZihLYW11cykpOwogICAga2FtdXMtPmhlYWQgPSBOVUxMOwogICAgcmV0dXJuIGthbXVzOwp9Cgp2b2lkIHRhbWJhaFNpbm9uaW0oU2lub25pbU5vZGUgKipoZWFkLCBjb25zdCBjaGFyICpzaW5vbmltKSB7CiAgICBTaW5vbmltTm9kZSAqbm9kZUJhcnUgPSAoU2lub25pbU5vZGUgKiltYWxsb2Moc2l6ZW9mKFNpbm9uaW1Ob2RlKSk7CiAgICBzdHJjcHkobm9kZUJhcnUtPnNpbm9uaW0sIHNpbm9uaW0pOwogICAgbm9kZUJhcnUtPm5leHQgPSBOVUxMOwoKICAgIGlmICgqaGVhZCA9PSBOVUxMKSB7CiAgICAgICAgKmhlYWQgPSBub2RlQmFydTsKICAgIH0gZWxzZSB7CiAgICAgICAgU2lub25pbU5vZGUgKnRlbXAgPSAqaGVhZDsKICAgICAgICB3aGlsZSAodGVtcC0+bmV4dCAhPSBOVUxMKSB7CiAgICAgICAgICAgIGlmIChzdHJjbXAodGVtcC0+c2lub25pbSwgc2lub25pbSkgPT0gMCkgcmV0dXJuOwogICAgICAgICAgICB0ZW1wID0gdGVtcC0+bmV4dDsKICAgICAgICB9CiAgICAgICAgaWYgKHN0cmNtcCh0ZW1wLT5zaW5vbmltLCBzaW5vbmltKSAhPSAwKSB7CiAgICAgICAgICAgIHRlbXAtPm5leHQgPSBub2RlQmFydTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBmcmVlKG5vZGVCYXJ1KTsKICAgICAgICB9CiAgICB9Cn0KCnZvaWQgdGFtYmFoKEthbXVzICprYW11cywgY29uc3QgY2hhciAqa2F0YSwgY29uc3QgY2hhciAqc2lub25pbSkgewogICAgS2FtdXNOb2RlICp0ZW1wID0ga2FtdXMtPmhlYWQ7CgogICAgd2hpbGUgKHRlbXAgIT0gTlVMTCkgewogICAgICAgIGlmIChzdHJjbXAodGVtcC0+a2F0YSwga2F0YSkgPT0gMCkgewogICAgICAgICAgICB0YW1iYWhTaW5vbmltKCZ0ZW1wLT5zaW5vbmltSGVhZCwgc2lub25pbSk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgdGVtcCA9IHRlbXAtPm5leHQ7CiAgICB9CgogICAgS2FtdXNOb2RlICpub2RlQmFydSA9IChLYW11c05vZGUgKiltYWxsb2Moc2l6ZW9mKEthbXVzTm9kZSkpOwogICAgc3RyY3B5KG5vZGVCYXJ1LT5rYXRhLCBrYXRhKTsKICAgIG5vZGVCYXJ1LT5zaW5vbmltSGVhZCA9IE5VTEw7CiAgICBub2RlQmFydS0+bmV4dCA9IGthbXVzLT5oZWFkOwogICAga2FtdXMtPmhlYWQgPSBub2RlQmFydTsKCiAgICB0YW1iYWhTaW5vbmltKCZub2RlQmFydS0+c2lub25pbUhlYWQsIHNpbm9uaW0pOwp9Cgp2b2lkIGFtYmlsU2lub25pbShLYW11cyAqa2FtdXMsIGNvbnN0IGNoYXIgKmthdGEpIHsKICAgIEthbXVzTm9kZSAqdGVtcCA9IGthbXVzLT5oZWFkOwoKICAgIHdoaWxlICh0ZW1wICE9IE5VTEwpIHsKICAgICAgICBpZiAoc3RyY21wKHRlbXAtPmthdGEsIGthdGEpID09IDApIHsKICAgICAgICAgICAgU2lub25pbU5vZGUgKnNpbm9uaW1UZW1wID0gdGVtcC0+c2lub25pbUhlYWQ7CiAgICAgICAgICAgIHByaW50ZigiWyIpOwogICAgICAgICAgICB3aGlsZSAoc2lub25pbVRlbXAgIT0gTlVMTCkgewogICAgICAgICAgICAgICAgcHJpbnRmKCIlcyIsIHNpbm9uaW1UZW1wLT5zaW5vbmltKTsKICAgICAgICAgICAgICAgIHNpbm9uaW1UZW1wID0gc2lub25pbVRlbXAtPm5leHQ7CiAgICAgICAgICAgICAgICBpZiAoc2lub25pbVRlbXAgIT0gTlVMTCkgcHJpbnRmKCIsICIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHByaW50ZigiXVxuIik7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgdGVtcCA9IHRlbXAtPm5leHQ7CiAgICB9CgogICAgcHJpbnRmKCJudWxsIC8gYmVsb20gZGl0YW1iYWhrYW5cbiIpOwp9CgppbnQgbWFpbigpIHsKICAgIEthbXVzICprYW11cyA9IGJ1YXRLYW11cygpOwoKICAgIHRhbWJhaChrYW11cywgImJpZyIsICJsYXJnZSIpOwogICAgdGFtYmFoKGthbXVzLCAiYmlnIiwgImdyZWF0Iik7CiAgICB0YW1iYWgoa2FtdXMsICJiaWciLCAiaHVnZSIpOwogICAgdGFtYmFoKGthbXVzLCAiYmlnIiwgImZhdCIpOwogICAgdGFtYmFoKGthbXVzLCAiaHVnZSIsICJlbm9ybW91cyIpOwogICAgdGFtYmFoKGthbXVzLCAiaHVnZSIsICJnaWdhbnRpYyIpOwogICAgdGFtYmFoKGthbXVzLCAidGFsbCIsICJiaWciKTsKICAgIHRhbWJhaChrYW11cywgInRhbGwiLCAiaGlnaCIpOwogICAgdGFtYmFoKGthbXVzLCAibGl0dGxlIiwgInRpbnkiKTsKICAgIHRhbWJhaChrYW11cywgInRpbnkiLCAibGl0dGxlIik7CgogICAgcHJpbnRmKCJTaW5vbmltIHVudHVrICdiaWcnIGFkYWxhaDogIik7CiAgICBhbWJpbFNpbm9uaW0oa2FtdXMsICJiaWciKTsKCiAgICBwcmludGYoIlNpbm9uaW0gdW50dWsgJ2h1Z2UnIGFkYWxhaDogIik7CiAgICBhbWJpbFNpbm9uaW0oa2FtdXMsICJodWdlIik7CgogICAgcHJpbnRmKCJTaW5vbmltIHVudHVrICdsYXJnZScgYWRhbGFoOiAiKTsKICAgIGFtYmlsU2lub25pbShrYW11cywgImxhcmdlIik7CgogICAgcHJpbnRmKCJTaW5vbmltIHVudHVrICd0YWxsJyBhZGFsYWg6ICIpOwogICAgYW1iaWxTaW5vbmltKGthbXVzLCAidGFsbCIpOwogICAgCiAgICBwcmludGYoIlNpbm9uaW0gdW50dWsgJ2xpdHRsZScgYWRhbGFoOiAiKTsKICAgIGFtYmlsU2lub25pbShrYW11cywgImxpdHRsZSIpOwoKICAgIHByaW50ZigiU2lub25pbSB1bnR1ayAndGlueScgYWRhbGFoOiAiKTsKICAgIGFtYmlsU2lub25pbShrYW11cywgInRpbnkiKTsKICAgIAogICAgcHJpbnRmKCJTaW5vbmltIHVudHVrICdoaWdoJyBhZGFsYWg6ICIpOwogICAgYW1iaWxTaW5vbmltKGthbXVzLCAiaGlnaCIpOwogICAgCiAgICBwcmludGYoIlNpbm9uaW0gdW50dWsgJ2dpZ2FudGljJyBhZGFsYWg6ICIpOwogICAgYW1iaWxTaW5vbmltKGthbXVzLCAiZ2lnYW50aWMiKTsKCiAgICBwcmludGYoIlNpbm9uaW0gdW50dWsgJ2NvbG9zc2FsJyBhZGFsYWg6ICIpOwogICAgYW1iaWxTaW5vbmltKGthbXVzLCAiY29sb3NzYWwiKTsKCiAgICByZXR1cm4gMDsKfQ==