#ifndef HEADER_H_INCLUDED #define HEADER_H_INCLUDED #include #include #include typedef struct entry__s { char songtitle[256], interpreter[256]; } entry_t; typedef struct heap__s { entry_t *entries; long size; long next; } heap_t; heap_t *create_heap(long size); void delete_heap(heap_t *heap); long compare(heap_t *heap, long index, char songtitle[], char interpreter[]); long is_empty(heap_t *heap, long index); void insert_entry(heap_t *heap, char songtitle[], char interpreter[]); long search_entry(heap_t *heap, char songtitle[], char interpreter[]); void print_entry(heap_t *heap, long index); void print(heap_t *heap); long count_entries(heap_t *heap); long count_entries_with_songtitle(heap_t *heap, char songtitle[]); void up_heap(heap_t *heap, long index); void down_heap(heap_t *heap, long index); #endif // HEADER_H_INCLUDED #include "header.h" heap_t *create_heap(long size) { heap_t *new = malloc(sizeof(heap_t)); new->next = 1; new->size = size + 1; new->entries = calloc(new->size, sizeof(entry_t)); return new; } void delete_heap(heap_t *heap) { heap->next = 0; heap->size = 0; free(heap->entries); heap->entries = 0; } long compare(heap_t *heap, long index, char songtitle[], char interpreter[]) { if (index < heap->size && index < heap->next) { return strcmp(heap->entries[index].songtitle, songtitle) + strcmp(heap->entries[index].interpreter, interpreter); } else { return strcmp("", songtitle) + strcmp("", interpreter); } } long is_empty(heap_t *heap, long index) { return !compare(heap, index, "", ""); } void insert_entry(heap_t *heap, char songtitle[], char interpreter[]) { if (heap->size <= heap->next) { heap->size = 2*(heap->size - 1) - 1; heap->entries = realloc(heap->entries, heap->size * sizeof(entry_t)); } while (!is_empty(heap, heap->next++)); strcpy(heap->entries[heap->next - 1].songtitle, songtitle); strcpy(heap->entries[heap->next - 1].interpreter, interpreter); } long search_entry(heap_t *heap, char songtitle[], char interpreter[]) { unsigned long i; for (i = 1; i < heap->next; i++) { if ((strcmp(heap->entries[i].interpreter, interpreter) == 0) && (strcmp(heap->entries[i].songtitle, songtitle) == 0)) return i; } return -1; } void print_entry(heap_t *heap, long index) { if (index < heap->size) { fprintf(stdout, "[%ld]\t", index); fprintf(stdout, "%s\t", heap->entries[index].songtitle); fprintf(stdout, "%s\n", heap->entries[index].interpreter); } } void print(heap_t *heap) { unsigned long i; fprintf(stdout, "\n"); for (i = 0; i < heap->size; i++) { print_entry(heap, i); } fprintf(stdout, "Next:\t%ld\n", heap->next); } long count_entries(heap_t *heap) { return heap->next - 1; } long count_entries_with_songtitle(heap_t *heap, char songtitle[]) { unsigned long i, count = 0; for (i = 0; i < heap->next; i++) { if (!strcmp(heap->entries[i].songtitle, songtitle)) count++; } return count; } void up_heap(heap_t *heap, long index) { } void down_heap(heap_t *heap, long index) { long last = heap->next - 1; if (2 * index <= last) { entry_t temp = heap->entries[index]; while (2 * index <= last) { long next = index * 2; if ((next < last) && (strcmp(heap->entries[next + 1].songtitle, heap->entries[next].songtitle) > 0)) next++; if (strcmp(temp.songtitle, heap->entries[next].songtitle) >= 0) break; memcpy(&heap->entries[index], &heap->entries[next], sizeof(entry_t)); index = next; } memcpy(&heap->entries[index], &temp, sizeof(entry_t)); } } #include "source.c" int main() { heap_t *heap = create_heap(12); insert_entry(heap, "B", "1"); insert_entry(heap, "E", "2"); insert_entry(heap, "I", "3"); insert_entry(heap, "S", "4"); insert_entry(heap, "P", "5"); insert_entry(heap, "I", "6"); insert_entry(heap, "E", "7"); insert_entry(heap, "L", "8"); insert_entry(heap, "H", "9"); insert_entry(heap, "E", "10"); insert_entry(heap, "A", "11"); insert_entry(heap, "P", "12"); print(heap); unsigned long i; for (i = heap->next - 1; i > 0; i--) down_heap(heap, i); print(heap); delete_heap(heap); free(heap); return 0; }