-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_array.c
137 lines (112 loc) · 2.73 KB
/
static_array.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// sequence implemented as static array
#include <stdlib.h>
#include <stdio.h>
typedef struct s_a static_array;
static_array *new_static_array(int *array, int n);
int length(static_array *s_a);
void iter_sequence(static_array *s_a);
int get_at(static_array *s_a, int i);
void set_at(static_array *s_a, int i, int x);
void insert_at(static_array *s_a, int i, int x);
int delete_at(static_array *s_a, int i);
void insert_first(static_array *s_a, int x);
int delete_first(static_array *s_a);
void insert_last(static_array *s_a, int x);
int delete_last(static_array *s_a);
void resize_array(static_array *s_a);
typedef struct s_a {
int *sequence;
int size;
int length;
} static_array;
static_array* new_static_array(int *array, int n) {
static_array *s_a;
s_a = (static_array*) malloc(sizeof(static_array));
if (s_a == NULL) {
return NULL;
}
s_a->sequence = (int*) malloc(sizeof(int) * n);
if (s_a == NULL) {
return NULL;
}
for (int i = 0; i < n; i++) {
s_a->sequence[i] = array[i];
}
s_a->length = s_a->size = n;
return s_a;
}
int get_at(static_array *s_a, int i) {
if (i >= 0 && i < s_a->length) {
return s_a->sequence[i];
}
}
void set_at(static_array *s_a, int i, int x) {
if (i >= 0 && i < s_a->length) {
s_a->sequence[i] = x;
}
}
// insert an element in i-th position
// if array is full then resize
void insert_at(static_array *s_a, int i, int x) {
if (i >= 0 && i <= s_a->length) {
int curr;
if (s_a->length == s_a->size) {
resize_array(s_a);
}
curr = s_a->length;
while (curr > i) {
s_a->sequence[curr] = s_a->sequence[curr - 1]; // shift rigth
curr--;
}
s_a->sequence[curr] = x;
s_a->length++;
}
}
// delete an element in i-th position
int delete_at(static_array *s_a, int i) {
if (i >= 0 && i < s_a->length) {
int ret;
ret = s_a->sequence[i];
while (i < s_a->length - 1) {
s_a->sequence[i] = s_a->sequence[i + 1]; // shift left
i++;
}
s_a->length--;
return ret;
}
}
void insert_first(static_array *s_a, int x) {
insert_at(s_a, 0, x);
}
int delete_first(static_array *s_a) {
return delete_at(s_a, 0);
}
void insert_last(static_array *s_a, int x) {
insert_at(s_a, s_a->length, x);
}
int delete_last(static_array *s_a) {
return delete_at(s_a, s_a->length - 1);
}
int length(static_array *s_a) {
return s_a->length;
}
// resize static array to n + 1
void resize_array(static_array *s_a) {
int *new_array;
new_array = (int*) malloc(sizeof(int) * (s_a->size + 1));
if (new_array == NULL) {
return;
}
for (int i = 0; i < s_a->length; i++) {
new_array[i] = s_a->sequence[i];
}
free(s_a->sequence);
s_a->sequence = new_array;
s_a->size++;
}
void iter_sequence(static_array *s_a) {
for (int i = 0; i < s_a->length; i++) {
printf("%d ", s_a->sequence[i]);
}
printf("\n");
}