HEAP


SUBMITTED BY: bitcoinsachen

DATE: Dec. 14, 2016, 10:07 p.m.

FORMAT: C

SIZE: 4.7 kB

HITS: 821

  1. #ifndef HEADER_H_INCLUDED
  2. #define HEADER_H_INCLUDED
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. typedef struct entry__s
  7. {
  8. char songtitle[256], interpreter[256];
  9. } entry_t;
  10. typedef struct heap__s
  11. {
  12. entry_t *entries;
  13. long size;
  14. long next;
  15. } heap_t;
  16. heap_t *create_heap(long size);
  17. void delete_heap(heap_t *heap);
  18. long compare(heap_t *heap, long index, char songtitle[], char interpreter[]);
  19. long is_empty(heap_t *heap, long index);
  20. void insert_entry(heap_t *heap, char songtitle[], char interpreter[]);
  21. long search_entry(heap_t *heap, char songtitle[], char interpreter[]);
  22. void print_entry(heap_t *heap, long index);
  23. void print(heap_t *heap);
  24. long count_entries(heap_t *heap);
  25. long count_entries_with_songtitle(heap_t *heap, char songtitle[]);
  26. void up_heap(heap_t *heap, long index);
  27. void down_heap(heap_t *heap, long index);
  28. #endif // HEADER_H_INCLUDED
  29. #include "header.h"
  30. heap_t *create_heap(long size)
  31. {
  32. heap_t *new = malloc(sizeof(heap_t));
  33. new->next = 1;
  34. new->size = size + 1;
  35. new->entries = calloc(new->size, sizeof(entry_t));
  36. return new;
  37. }
  38. void delete_heap(heap_t *heap)
  39. {
  40. heap->next = 0;
  41. heap->size = 0;
  42. free(heap->entries);
  43. heap->entries = 0;
  44. }
  45. long compare(heap_t *heap, long index, char songtitle[], char interpreter[])
  46. {
  47. if (index < heap->size && index < heap->next)
  48. {
  49. return strcmp(heap->entries[index].songtitle, songtitle) + strcmp(heap->entries[index].interpreter, interpreter);
  50. }
  51. else
  52. {
  53. return strcmp("", songtitle) + strcmp("", interpreter);
  54. }
  55. }
  56. long is_empty(heap_t *heap, long index)
  57. {
  58. return !compare(heap, index, "", "");
  59. }
  60. void insert_entry(heap_t *heap, char songtitle[], char interpreter[])
  61. {
  62. if (heap->size <= heap->next)
  63. {
  64. heap->size = 2*(heap->size - 1) - 1;
  65. heap->entries = realloc(heap->entries, heap->size * sizeof(entry_t));
  66. }
  67. while (!is_empty(heap, heap->next++));
  68. strcpy(heap->entries[heap->next - 1].songtitle, songtitle);
  69. strcpy(heap->entries[heap->next - 1].interpreter, interpreter);
  70. }
  71. long search_entry(heap_t *heap, char songtitle[], char interpreter[])
  72. {
  73. unsigned long i;
  74. for (i = 1; i < heap->next; i++)
  75. {
  76. if ((strcmp(heap->entries[i].interpreter, interpreter) == 0) && (strcmp(heap->entries[i].songtitle, songtitle) == 0))
  77. return i;
  78. }
  79. return -1;
  80. }
  81. void print_entry(heap_t *heap, long index)
  82. {
  83. if (index < heap->size)
  84. {
  85. fprintf(stdout, "[%ld]\t", index);
  86. fprintf(stdout, "%s\t", heap->entries[index].songtitle);
  87. fprintf(stdout, "%s\n", heap->entries[index].interpreter);
  88. }
  89. }
  90. void print(heap_t *heap)
  91. {
  92. unsigned long i;
  93. fprintf(stdout, "\n");
  94. for (i = 0; i < heap->size; i++)
  95. {
  96. print_entry(heap, i);
  97. }
  98. fprintf(stdout, "Next:\t%ld\n", heap->next);
  99. }
  100. long count_entries(heap_t *heap)
  101. {
  102. return heap->next - 1;
  103. }
  104. long count_entries_with_songtitle(heap_t *heap, char songtitle[])
  105. {
  106. unsigned long i, count = 0;
  107. for (i = 0; i < heap->next; i++)
  108. {
  109. if (!strcmp(heap->entries[i].songtitle, songtitle))
  110. count++;
  111. }
  112. return count;
  113. }
  114. void up_heap(heap_t *heap, long index)
  115. {
  116. }
  117. void down_heap(heap_t *heap, long index)
  118. {
  119. long last = heap->next - 1;
  120. if (2 * index <= last)
  121. {
  122. entry_t temp = heap->entries[index];
  123. while (2 * index <= last)
  124. {
  125. long next = index * 2;
  126. if ((next < last) && (strcmp(heap->entries[next + 1].songtitle, heap->entries[next].songtitle) > 0))
  127. next++;
  128. if (strcmp(temp.songtitle, heap->entries[next].songtitle) >= 0)
  129. break;
  130. memcpy(&heap->entries[index], &heap->entries[next], sizeof(entry_t));
  131. index = next;
  132. }
  133. memcpy(&heap->entries[index], &temp, sizeof(entry_t));
  134. }
  135. }
  136. #include "source.c"
  137. int main()
  138. {
  139. heap_t *heap = create_heap(12);
  140. insert_entry(heap, "B", "1");
  141. insert_entry(heap, "E", "2");
  142. insert_entry(heap, "I", "3");
  143. insert_entry(heap, "S", "4");
  144. insert_entry(heap, "P", "5");
  145. insert_entry(heap, "I", "6");
  146. insert_entry(heap, "E", "7");
  147. insert_entry(heap, "L", "8");
  148. insert_entry(heap, "H", "9");
  149. insert_entry(heap, "E", "10");
  150. insert_entry(heap, "A", "11");
  151. insert_entry(heap, "P", "12");
  152. print(heap);
  153. unsigned long i;
  154. for (i = heap->next - 1; i > 0; i--)
  155. down_heap(heap, i);
  156. print(heap);
  157. delete_heap(heap);
  158. free(heap);
  159. return 0;
  160. }

comments powered by Disqus