Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include<string.h>
- typedef struct hashentry_s
- {
- char songtitle [256] , interpreter [256];
- } hashentry_t ;
- typedef struct hasharray_s
- {
- hashentry_t * entries ;
- long num_entries ;
- } hasharray_t ;
- typedef struct hashcontainer_s
- {
- hasharray_t * hasharrays ;
- long hashsize ;
- } hashcontainer_t ;
- long hash_key (char songtitle [], char interpreter [], long
- hash_max )
- {
- unsigned long index =0, i;
- for (i = 0; i < strlen ( songtitle ); ++i)
- index = 64 * index + (long)( songtitle [i]);
- for (i = 0; i < strlen ( interpreter ); ++i)
- index = 64 * index + (long)( interpreter [i]);
- return index % hash_max ;
- }
- //funkcija da napravi hash
- hashcontainer_t *create_hash(long hashsize){
- hashcontainer_t *newcontainer;
- newcontainer = malloc(sizeof(hashcontainer_t));
- if(newcontainer){
- newcontainer->hasharrays = calloc(newcontainer->hashsize, sizeof(hasharray_t));
- if(newcontainer->hasharrays){
- newcontainer->hashsize = hashsize;
- return newcontainer;
- }
- else{
- printf("Fehler....");
- return 0;
- }
- }
- else{
- printf("Fehler beim Allozieren\n");
- return 0;
- }
- }
- void delete_hash(hashcontainer_t *hashcontainer){
- if(hashcontainer == 0){
- printf("Its already empty....");
- return;
- }
- if(hashcontainer->hasharrays == 0){
- free(hashcontainer);
- hashcontainer = 0;
- return;
- }
- long i;
- for(i=0; i<hashcontainer->hashsize; i++){
- if(hashcontainer->hasharrays[i].entries){
- free(hashcontainer->hasharrays->entries);
- }
- }
- free(hashcontainer->hasharrays);
- free(hashcontainer);
- }
- void insert_entry ( hashcontainer_t * hashcontainer , char songtitle [], char interpreter []){
- hashentry_t *tmp = 0;
- long index = hash_key(songtitle, interpreter, hashcontainer->hashsize);
- long velicina = hashcontainer->hasharrays[index].num_entries;
- tmp = realloc(hashcontainer->hasharrays[index].entries, (velicina+1)*sizeof(hashentry_t));
- //dodaj novi element na kraj jednog arraya
- strcpy(tmp[velicina].interpreter, interpreter);
- strcpy(tmp[velicina].songtitle, songtitle);
- hashcontainer->hasharrays[index].entries = tmp;
- hashcontainer->hasharrays[index].num_entries++;
- }
- hashentry_t * search_entry (hashcontainer_t * hashcontainer, char songtitle [], char interpreter []){
- long key = hash_key(songtitle, interpreter, hashcontainer->hashsize);
- hasharray_t tmp = hashcontainer ->hasharrays[key];
- long i;
- for(i=0; i<tmp.num_entries; i++){
- if((strcmp(tmp.entries[i].interpreter, interpreter)==0) && (strcmp(tmp.entries[i].songtitle, songtitle)==0))
- return &(tmp.entries[i]);
- }
- printf("Element nicht gefunden\n");
- return 0;
- }
- void delete_entry ( hashcontainer_t * hashcontainer , char songtitle [], char interpreter []){
- long key = hash_key(songtitle, interpreter, hashcontainer->hashsize);
- hasharray_t *tmp = &(hashcontainer->hasharrays[key]);
- hashentry_t *tmp_entry = malloc(tmp->num_entries * sizeof(hashentry_t));
- if(tmp->num_entries==0){
- printf("Das zu loeschende Elemenet nicht gefunden.....");
- return;
- }
- else if(tmp->num_entries == 1){
- free(tmp->entries);
- tmp->entries = 0;
- tmp->num_entries = 0;
- printf("Das einzige Element in diesem Array wurde geloescht\n");
- }
- else{
- long i;
- for(i=0; i<tmp->num_entries; i++){
- if(strcmp(tmp->entries[i].interpreter, interpreter) ==0 && strcmp(tmp->entries[i].songtitle, songtitle)==0){
- memcpy(tmp_entry, tmp->entries, sizeof(hashentry_t)*i);
- memcpy(&(tmp_entry[i]), &(tmp->entries[i+1]), sizeof(hashentry_t)*tmp->num_entries -i -1);
- //memcpy(&(tmp_entry[i]),&(tmp->entries[i+1]),sizeof(hashentry_t)*tmp->num_entries -i -1);
- tmp->entries = tmp_entry;
- tmp->num_entries--;
- tmp->entries = realloc(tmp->entries, sizeof(hashentry_t)*tmp->num_entries);
- printf("Gotovo....");
- return;
- }
- }
- }
- }
- long print_hash ( hashcontainer_t * hashcontainer ){
- long i,j;
- if(hashcontainer!=0){
- for(i=0; i<hashcontainer->hashsize; i++){
- printf("%ld: ", i);
- if(hashcontainer->hasharrays[i].entries){
- for(j=0; j<hashcontainer->hasharrays[i].num_entries; j++){
- printf(" %ld. %s - %s", j, hashcontainer->hasharrays[i].entries[j].interpreter, hashcontainer->hasharrays[i].entries[j].songtitle );
- printf("\n");
- }
- }
- }
- return i;
- }
- return 0;
- }
- long count_entries_with_interpreter ( hashcontainer_t* hashcontainer , char interpreter []){
- long i,j;
- long summe = 0;
- if(hashcontainer==0){
- printf("Prazan kontejner....");
- return 0;
- }
- for(i=0; i<hashcontainer->hashsize; i++){
- if(hashcontainer->hasharrays[i].num_entries!=0){
- for(j=0; j<hashcontainer->hasharrays[i].num_entries; j++){
- if(strcmp(hashcontainer->hasharrays[i].entries[j].interpreter , interpreter)==0)
- summe=summe++;
- }
- }
- }
- return summe;
- }
- int main()
- {
- int w=0;
- long z;
- hashcontainer_t *myhash=0;
- char songtitle[256], interpreter[256];
- hashentry_t *find;
- myhash = create_hash(3);
- insert_entry(myhash, "Lijepaje", "Eldira");
- insert_entry(myhash, "Glupost", "Glupan");
- insert_entry(myhash, "NezBgm", "Niko");
- insert_entry(myhash, "Smor", "Dosada");
- do{
- printf("1. CREATE HASH\n");
- printf("2. DELETE HASH\n");
- printf("3. INSERT ENTRY\n");
- printf("4. SEARCH ENTRY\n");
- printf("5. DELETE ENTRY\n");
- printf("6. PRINT HASH\n");
- printf("7. COUNT WITH songtitle\n");
- printf("0. EXIT\n");
- scanf("%d", &w);
- switch(w){
- case 1:
- printf("Size? ");
- scanf("%ld", &z);
- delete_hash(myhash);
- myhash=0;
- myhash=create_hash(z);
- break;
- case 2:
- delete_hash(myhash);
- myhash=0;
- break;
- case 3:
- while(getchar()!='\n');
- printf("Songtitle:");
- fgets(songtitle,255,stdin);
- *strchr(songtitle,'\n')=0;
- printf("Interpreter:");
- fgets(interpreter,255,stdin);
- *(strchr(interpreter,'\n'))=0;
- insert_entry(myhash,songtitle,interpreter);
- break;
- case 4:
- while(getchar()!='\n');
- printf("Songtitle:");
- fgets(songtitle,255,stdin);
- *(strchr(songtitle,'\n'))=0;
- printf("Interpreter:");
- fgets(interpreter,255,stdin);
- *(strchr(interpreter,'\n'))=0;
- find = search_entry(myhash,songtitle,interpreter);
- printf("Gefunden auf Adresse : %p\n",find);
- break;
- case 5:
- while(getchar()!='\n');
- printf("Songtitle:");
- fgets(songtitle,255,stdin);
- *(strchr(songtitle,'\n'))=0;
- printf("Interpreter:");
- fgets(interpreter,255,stdin);
- *(strchr(interpreter,'\n'))=0;
- delete_entry(myhash,songtitle,interpreter);
- break;
- case 6:
- if(myhash)
- print_hash(myhash);
- break;
- case 7:
- while(getchar()!='\n');
- printf("Interpreter:");
- fgets(interpreter,255,stdin);
- *(strchr(interpreter,'\n'))=0;
- printf("Count: %ld\n",count_entries_with_interpreter(myhash,interpreter));
- break;
- case 0:
- break;
- default:
- printf("Falsche Eingabe\n");
- }
- }while(w!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment