Compare commits

1 Commits

Author SHA1 Message Date
Ваше Имя
2558f9a414 всякое разное 2025-11-15 04:36:38 -05:00

View File

@@ -1,5 +1,4 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -25,7 +24,11 @@ void
vector_initdata(ivector *v, int *arr, size_t arrsz)
{
assert(v && arrsz);
// <YOURCODE>
v->n = arrsz;
v->maxsz = (arrsz > VECTOR_START_SIZE) ? arrsz : VECTOR_START_SIZE;
v->data = xmalloc(ITEMSZ * v->maxsz);
memcpy(v->data, arr, ITEMSZ * arrsz);
}
void
@@ -44,16 +47,14 @@ vector_len(const ivector *v)
{
assert(v);
// <YOURCODE>
return 0;
return v->n;
}
int *
vector_data(const ivector *v)
{
assert(v);
// <YOURCODE>
return NULL;
return v->data;
}
void
@@ -61,49 +62,93 @@ vector_resize(ivector *v, int n)
{
assert(v);
// <YOURCODE>
if (n <= v->maxsz) {
v->n = n;
return;
}
size_t new_maxsz = v->maxsz;
while (new_maxsz < n) {
new_maxsz *= VECTOR_GROW_FACTOR;
}
v->data = xrealloc(v->data, ITEMSZ * new_maxsz);
for (size_t i = v->n; i < (size_t)n; i++) {
v->data[i] = 0;
}
v->maxsz = new_maxsz;
v->n = n;
}
void
vector_set(ivector *v, size_t idx, int val)
{
assert(v && idx < v->n);
// <YOURCODE>
v->data[idx] = val;
}
int
vector_get(const ivector *v, size_t idx)
{
assert(v && idx < v->n);
// <YOURCODE>
return 0;
return v->data[idx];
}
void
vector_push(ivector *v, int val)
{
assert(v);
// <YOURCODE>
if (v->n == v->maxsz) {
size_t new_maxsz = v->maxsz * VECTOR_GROW_FACTOR;
v->data = xrealloc(v->data, ITEMSZ * new_maxsz);
v->maxsz = new_maxsz;
}
v->data[v->n] = val;
v->n++;
}
void
vector_pop(ivector *v)
{
assert(v);
// <YOURCODE>
if (v->n > 0) {
v->n--;
}
}
void
vector_insert(ivector *v, size_t idx, int val)
{
assert(v && v->n >= idx);
// <YOURCODE>
if (v->n == v->maxsz) {
size_t new_maxsz = v->maxsz * VECTOR_GROW_FACTOR;
v->data = xrealloc(v->data, ITEMSZ * new_maxsz);
v->maxsz = new_maxsz;
}
for (size_t i = v->n; i > idx; i--) {
v->data[i] = v->data[i - 1];
}
v->data[idx] = val;
v->n++;
}
void
vector_del(ivector *v, size_t idx)
{
// <YOURCODE>
assert(v && idx < v->n);
for (size_t i = idx; i < v->n - 1; i++) {
v->data[i] = v->data[i + 1];
}
v->n--;
}
void