#include <string.h>
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
char *name;
char *desc;
struct _node *next;
}node;
#define HASHSIZE 5
static node*hashtab[HASHSIZE];
void inithashtab(){
int i;
for(i=0;i<HASHSIZE;i++)
hashtab[i];
}
unsigned int hash(char *s){
unsigned int h=0;
for(;*s;s++)
h=*s+h*31;
return h%HASHSIZE;
}
node* lookup(char *n){
unsigned int hi=hash(n);
node* np=hashtab[hi];
for(;np!=NULL;np=np->next){
if(!strcmp(np->name,n))
return np;
}
return NULL;
}
char* m_strdup(char *o){
int l=strlen(o)+1;
char *ns=(char*)malloc(l*sizeof(char));
strcpy(ns,o);
if(ns==NULL)
return NULL;
else
return ns;
}
char* get(char* name){
node* n=lookup(name);
if(n==NULL)
return NULL;
else
return n->desc;
}
int install(char* name,char* desc){
unsigned int hi;
node* np;
if((np=lookup(name))==NULL){
hi=hash(name);
np=(node*)malloc(sizeof(node));
if(np==NULL)
return 0;
np->name=m_strdup(name);
if(np->name==NULL) return 0;
np->next=hashtab[hi];
hashtab[hi]=np;
}
else
free(np->desc);
np->desc=m_strdup(desc);
if(np->desc==NULL) return 0;
return 1;
}
void displaytable(){
int i;
node*t;
for(i=0;i<HASHSIZE;i++){
if(hashtab[i]==NULL)
printf("");
else
t=hashtab[i];
printf("");
for(;t!=NULL;t=t->next)
printf("(%s:%s)",t->name,t->desc);
printf("");
printf("");
}
}
void cleanup(){
node *np, *t;
int i;
for(i=0;i<HASHSIZE;i++){
if(hashtab[i]!=NULL){
np=hashtab[i];
while(np!=NULL){
t=np->next;
free(np->name);
free(np->desc);
free(np);
np=t;
}
}
}
}
void data(){
int i;
char* names[]={"name ","address ","phone ","cita-cita ","sekolah"};
char* descs[]={"Mahfuz", "Bima", "087759662524", "guru", "mahasiswa"};
inithashtab();
for(i=0;i<5;i++)
install(names[i], descs[i]);
printf("\nDone\n");
printf("\nKerjakan tugas dengan jujur");
install("phone","9433120451");
printf("\nhasilnya adalah", get("name"), get("address"), get("phone"), get("cita-cita"), get("sekolah"));
}
main(){
int pilihan;
do{
printf("+________________________________________________________+\n");
printf("|name || address || phone ||cita-cita || sekolah |\n");
printf("+________________________________________________________+\n");
printf("|Mahfuz|| Bima || 087759662524||guru || mahasiswa|\n");
printf("+________________________________________________________+\n");
cout<<"+_________________+\n";
cout<<"| MENU PILIHAN |\n";
cout<<"+_________________+\n";
cout<<"|1. Display Table |\n";
cout<<"|2. Cleanup |\n";
cout<<"|3. Exit |\n";
cout<<"+_________________+\n";
cout<<"| PILIHAN ANDA? []|\n";
cout<<"+_________________+\n";
cin>>pilihan;
switch(pilihan){
case 1:
data();
case 2:
displaytable();
break;
case 3:
cleanup();
break;
case 4:
cout<<"Terima Kasih! ";
cout<<"";
break;
}
getch();
}while(pilihan !=4);
return 0;
}
Selamat mencoba!!
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
char *name;
char *desc;
struct _node *next;
}node;
#define HASHSIZE 5
static node*hashtab[HASHSIZE];
void inithashtab(){
int i;
for(i=0;i<HASHSIZE;i++)
hashtab[i];
}
unsigned int hash(char *s){
unsigned int h=0;
for(;*s;s++)
h=*s+h*31;
return h%HASHSIZE;
}
node* lookup(char *n){
unsigned int hi=hash(n);
node* np=hashtab[hi];
for(;np!=NULL;np=np->next){
if(!strcmp(np->name,n))
return np;
}
return NULL;
}
char* m_strdup(char *o){
int l=strlen(o)+1;
char *ns=(char*)malloc(l*sizeof(char));
strcpy(ns,o);
if(ns==NULL)
return NULL;
else
return ns;
}
char* get(char* name){
node* n=lookup(name);
if(n==NULL)
return NULL;
else
return n->desc;
}
int install(char* name,char* desc){
unsigned int hi;
node* np;
if((np=lookup(name))==NULL){
hi=hash(name);
np=(node*)malloc(sizeof(node));
if(np==NULL)
return 0;
np->name=m_strdup(name);
if(np->name==NULL) return 0;
np->next=hashtab[hi];
hashtab[hi]=np;
}
else
free(np->desc);
np->desc=m_strdup(desc);
if(np->desc==NULL) return 0;
return 1;
}
void displaytable(){
int i;
node*t;
for(i=0;i<HASHSIZE;i++){
if(hashtab[i]==NULL)
printf("");
else
t=hashtab[i];
printf("");
for(;t!=NULL;t=t->next)
printf("(%s:%s)",t->name,t->desc);
printf("");
printf("");
}
}
void cleanup(){
node *np, *t;
int i;
for(i=0;i<HASHSIZE;i++){
if(hashtab[i]!=NULL){
np=hashtab[i];
while(np!=NULL){
t=np->next;
free(np->name);
free(np->desc);
free(np);
np=t;
}
}
}
}
void data(){
int i;
char* names[]={"name ","address ","phone ","cita-cita ","sekolah"};
char* descs[]={"Mahfuz", "Bima", "087759662524", "guru", "mahasiswa"};
inithashtab();
for(i=0;i<5;i++)
install(names[i], descs[i]);
printf("\nDone\n");
printf("\nKerjakan tugas dengan jujur");
install("phone","9433120451");
printf("\nhasilnya adalah", get("name"), get("address"), get("phone"), get("cita-cita"), get("sekolah"));
}
main(){
int pilihan;
do{
printf("+________________________________________________________+\n");
printf("|name || address || phone ||cita-cita || sekolah |\n");
printf("+________________________________________________________+\n");
printf("|Mahfuz|| Bima || 087759662524||guru || mahasiswa|\n");
printf("+________________________________________________________+\n");
cout<<"+_________________+\n";
cout<<"| MENU PILIHAN |\n";
cout<<"+_________________+\n";
cout<<"|1. Display Table |\n";
cout<<"|2. Cleanup |\n";
cout<<"|3. Exit |\n";
cout<<"+_________________+\n";
cout<<"| PILIHAN ANDA? []|\n";
cout<<"+_________________+\n";
cin>>pilihan;
switch(pilihan){
case 1:
data();
case 2:
displaytable();
break;
case 3:
cleanup();
break;
case 4:
cout<<"Terima Kasih! ";
cout<<"";
break;
}
getch();
}while(pilihan !=4);
return 0;
}
Selamat mencoba!!
0 komentar:
Posting Komentar