Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
aa:lab:sol:3 [2025/10/19 14:39]
andreidarlau04 recalculare
aa:lab:sol:3 [2025/10/19 21:59] (current)
andreidarlau04 [Algebraic Data Types]
Line 104: Line 104:
 Aflam ca pentru un load factor de peste 1/2 (suntem aproape de capacitatea maxima) avem cost amortizat -1, iar 2 daca ne apropiem de injumatatire. Aflam ca pentru un load factor de peste 1/2 (suntem aproape de capacitatea maxima) avem cost amortizat -1, iar 2 daca ne apropiem de injumatatire.
  
-<note important>​In operatiile de analiza amortizata, noi luam drept cost amortizat costul operatiilor mai "​scumpe",​ neluand in calcul operatiile "​ieftine",​ deci ĉ<​sub>​i</​sub>​ = 2 in acest caz, nicidecum ​-1. </​note>​+<note important>​In operatiile de analiza amortizata, noi luam drept cost amortizat costul operatiilor mai "​scumpe",​ neluand in calcul operatiile "​ieftine",​ deci ĉ<​sub>​i</​sub>​ = 2 in acest caz, nu -1. </​note>​
  
 2. Cazul stergerii in care capacitatea se schimba: 2. Cazul stergerii in care capacitatea se schimba:
Line 111: Line 111:
   * c<​sub>​i</​sub>​ = 1 + n (deletion + copiere)   * c<​sub>​i</​sub>​ = 1 + n (deletion + copiere)
   * ĉ<​sub>​i</​sub>​ = 1 + n + (1 - n) = 2   * ĉ<​sub>​i</​sub>​ = 1 + n + (1 - n) = 2
 +
 +===== Algebraic Data Types =====
 +
 +1. <​code>​
 +typedef struct list {
 +    int e;
 +    struct list *next;
 +} *List;
 +
 +List Void() {
 +    return NULL;
 +}
 +
 +List Cons(int e, List list) {
 +    List newElem = (List)malloc(sizeof(struct list));
 +    newElem->​e = e;
 +    newElem->​next = list;
 +
 +    return newElem;
 +}
 +</​code>​
 +2. 
 +<​code>​
 +Size(Void) = 0
 +Size(Cons(e,​ L)) = 1 + Size(L)
 +</​code>​
 +<​code>​
 +Add(Void, x) = Cons(x, Void)
 +Add(Cons(y, L), x) = Cons(y, Add(L, x)
 +</​code>​
 +<​code>​
 +Append(Void,​ L) = L
 +Append(Cons(x,​ L1), L2) = Cons(x, Append(L1, L2))
 +</​code>​
 +<​code>​
 +Reverse(Void) = Void
 +Reverse(Cons(x,​ L)) = Append(Reverse(L),​ Cons(x, Void))
 +</​code>​
 +Helper:
 +<​code>​
 +isNull(Void) = True
 +isNull(Cons(x,​ L)) = False
 +</​code>​
 +
 +3.
 +<​code>​
 +bool isNull(List l) {
 +    return l == NULL;
 +}
 +
 +int size(List l) {
 +    if (isNull(l))
 +        return 0;
 +    return 1 + size(l->​next);​
 +}
 +
 +List add(List l, int x) {
 +    if isNull(l))
 +        return Cons(x, Void());
 +    return Cons(l->​e,​ add(l->​next,​ x));
 +}
 +
 +List append(List l1, List l2) {
 +    if (isNull(l1))
 +        return l2;
 +    return Cons(l1->​e,​ append(l1->​next,​ l2));
 +}
 +
 +List reverse(List l) {
 +    if (isNull(l))
 +        return Void();
 +    return append(reverse(l->​next),​ Cons(l->​e,​ Void()));
 +}
 +</​code>​