Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Em {
- int *start;
- size_t size;
- size_t memory_allocated;
- } Em;
- void init(Em* a) {
- a->start = NULL;
- a->size = 0;
- a->memory_allocated = 0;
- }
- void push_back(int x, Em* a) {
- if (a->memory_allocated == 0) {
- a->start = calloc(1, sizeof(int));
- a->size = 1;
- a->memory_allocated = 1;
- a->start[0] = x;
- } else if (a->size < a->memory_allocated) {
- a->start[a->size] = x;
- ++a->size;
- } else {
- a->start = realloc(a->start, 2 * sizeof(int) * a->memory_allocated);
- a->memory_allocated *= 2;
- a->start[a->size] = x;
- ++a->size;
- }
- }
- int main() {
- int n;
- Em a;
- init(&a);
- scanf("%d", &n);
- for (int i = 0; i < n; ++i) {
- int x;
- scanf("%d", &x);
- push_back(x, &a);
- }
- for (int i = 0; i < n; ++i) {
- printf("%d-th element of array: %d \n", i, a.start[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement