-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted-list.c
102 lines (91 loc) · 2.72 KB
/
sorted-list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "sorted-list.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STACK_EMPTY -1
/**
* \param lst This is a pointer to the list which will be sorted using the insertion sort algorithm.
*/
void insertion_sort(sorted_list_t* lst){
for (size_t i = 1; i < lst->length; i++) {
int key = lst->data[i]; // Element to be placed in the sorted portion
int j = i - 1;
// Move elements of lst->data[], that are greater than key, to one position ahead
// of their current position
while (j >= 0 && lst->data[j] > key) {
lst->data[j + 1] = lst->data[j];
j = j - 1;
}
lst->data[j + 1] = key; // Place the key at the correct position
}
}
/**
* Initialize a sorted list.
*
* \param lst This is a pointer to space that should be initialized as a sorted list. The caller of
* this function retains ownership of the memory that lst points to (meaning the caller must free it
* if the pointer was returned from malloc)
*/
void sorted_list_init(sorted_list_t* lst) {
lst->data = NULL;
lst->length = 0;
}
/**
* Destroy a sorted list. Free any memory allocated to store the list, but not the list itself.
*
* \param lst This is a pointer to the space that holds the list being destroyred. This function
* should free any memory used to represent the list, but the caller retains ownership of the lst
* pointer itself.
*/
void sorted_list_destroy(sorted_list_t* lst) {
free(lst->data);
sorted_list_init(lst);
}
/**
* Add an element to a sorted list, maintaining the lowest-to-highest sorted order.
*
* \param lst The list that is being inserted to
* \param value The value being inserted
*/
void sorted_list_insert(sorted_list_t* lst, int value) {
// Only inserts into list, does not order it yet.
lst->data = realloc(lst->data, sizeof(int)*(lst->length + 1));
lst->data[lst->length] = value;
lst->length++;
}
/**
* Count how many times a value appears in a sorted list.
*
* \param lst The list being searched
* \param value The value being counted
* \returns the number of times value appears in lst
*/
size_t sorted_list_count(sorted_list_t* lst, int value) {
size_t count = 0;
// Traverse through the list and count occurrences of the value
for (size_t i = 0; i < lst->length; i++) {
if (lst->data[i] == value) {
count++;
}
}
return count;
}
/**
* Print the values in a sorted list in ascending order, separated by spaces and followed by a
* newline.
*
* \param lst The list to print
*/
void sorted_list_print(sorted_list_t* lst) {
if (lst->length == 0){
printf("%d\n", STACK_EMPTY);
}
else{
insertion_sort(lst);
for (size_t i = 0; i < lst->length; i++)
{
printf("%d ", *(lst->data + i));
}
printf("\n");
}
}