#I need help with an Assignment. (ArrayLists)

14 messages · Page 1 of 1 (latest)

modern hazel
#

Hello i need help with my Assignment, we have to implement ArrayLists.
I'm having problems implementing methods for appending, resizing, and overall constructors.
We have multiple .c Files, and the assignment is in German, but the method naming should give you an idea what it does.

ArrayList Struct looks like this:

typedef struct ArrayList {
  int* arr;      // eigentliche Liste
  int size;      // #Elemente in Liste
  int capacity;  // Größe von arr
} ArrayList;
#include "arraylist.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

/* Initiale Kapazität der Arrayliste */
const int initial_capacity = 11;

/* Hilfsfunktion: Ändert die Kapazität der Liste
 * auf new_capacity; new_capacity kann größer
 * oder kleiner als die aktuelle Kapazität sein. */
void _resize(ArrayList* l, int new_capacity);

/* Konstruktur; initialisiert l als leere Liste
 * mit einem Array der Größe initial_capacity.*/
void al_init(ArrayList* l) {
  /* TODO Implementieren Sie hier al_init */
    l->arr = (int*) malloc(initial_capacity*sizeof(int));
    l->size = 0;
  l->capacity = initial_capacity;
}

/* Destruktor; gibt sämtlichen von der Arrayliste
 * dynamisch allokierten Speicher wieder frei */
void al_destroy(ArrayList* l) {
  /* TODO Implementieren Sie hier al_destroy */
  free(l->arr);
  l->arr=NULL;

  free(l->capacity);
  l->capacity=NULL;

  free(l->size);
  l->size=NULL;
}

/* Fügt den Schlüssel key an Position pos ein.
 * Schiebt dazu die Elemente l->arr[pos,...,l->size-1]
 * um eine Position nach rechts. Wenn die aktuelle
 * Kapazität der Arrayliste nicht ausreicht,
 * wird die Kapazität der Arrayliste verdoppelt.
 *
 * Wenn pos == l->size ist die Funktion äquivalent
 * zu al_append. Es muss 0 <= pos <= l->size gelten.
 */
void al_insert(ArrayList* l, int pos, int key) {
  /* TODO Implementieren Sie hier al_insert */
  if(l->size == l->capacity) {
    
  }

}

/* Hängt den Schlüssel key an das Ende der
 * Arrayliste an. Wenn die aktuelle Kapazität
 * der Arrayliste nicht ausreicht, wird die Kapazität
 * der Arrayliste verdoppelt. */
void al_append(ArrayList* l, int key) {
  /* TODO Implementieren Sie hier al_append */
}

/* Löscht den Schlüssel an Position pos und
 * schiebt die Elemente l->arr[pos+1,...,l->size-1]
 * um eine Position nach links. Falls nach der
 * Operation die Kapazität mehr als dreimal so groß
 * wie die Anzahl der Elemente in der Liste ist,
 * wird die Kapazität der Liste halbiert.
 * Die Kapazität der Liste wird niemals unter 11
 * reduziert. Es muss 0 <= pos < l->size gelten.
 */
void al_remove(ArrayList* l, int pos) {
  /* TODO Implementieren Sie al_remove */
}

/* Hilfsfunktion: Ändert die Kapazität der Liste
 * auf new_capacity; new_capacity kann größer
 * oder kleiner als die aktuelle Kapazität sein.
 *
 * Die Kapazität der Liste soll immer mindestens
 * 11 sein; daher soll die Funktion nichts tun,
 * wenn new_capacity < 11 ist.
 */
void _resize(ArrayList* l, int new_capacity) {
  /* TODO Implementieren Sie hier _resize */
    l->arr = realloc(l->arr, new_capacity*sizeof(int));
    l->size = new_capacity;
}

/* Gibt die Liste in die Datei f aus
 * Verändern Sie diese Funktion bitte nicht :)
 */
void al_print(FILE* f, const ArrayList* l) {
  fprintf(f, "[ ");
  for(int i=0; i < l->size; ++i) {
    fprintf(f, "%3d ", l->arr[i]);
  }
  for(int i=l->size; i < l->capacity; ++i) {
    fprintf(f, "%3s ", "");
  }
  fprintf(f, " ][ size: %3d capacity: %3d ]\n", l->size, l->capacity);
}
ocean groveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

modern hazel
#

Firstly, is the al_init(); method correct? I am not allocating any memory for the ArrayList itself, but the Attributes for it

#

Is freeing the Memory with al_destroy(); correct?

viscid bear
#

the destructor is wrong thoguh

#

you cant free anything you dont malloc

#

so only free l->arr

modern hazel
#

thank you

#

i corrected it now

#

I'll mark it as solved for now

#

i think i'll try myself from here thanks

#

!solved