Advertisement
stirante

List

Jan 21st, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  typedef struct Element {
  5.    struct element *next;
  6.    int val;
  7.  } Element;
  8.  
  9.  typedef struct List {
  10.     struct Element *first;
  11.     struct Element *current;
  12.     int size;
  13.  } List;
  14.  typedef struct Iterator {
  15.     struct Element *current;
  16.     int size;
  17.  } Iterator;
  18. List* newList() {
  19.     List *lista = malloc(sizeof(List));
  20.     lista->first = malloc(sizeof(Element));
  21.     lista->current = lista->first;
  22.     lista->size = 0;
  23.     return lista;
  24. }
  25. Iterator* newIterator(List *lista) {
  26.     Iterator *iter = malloc(sizeof(Iterator));
  27.     iter->current = lista->first;
  28.     iter->size = lista->size;
  29. }
  30. int hasNext(Iterator *iter) {
  31.     return iter->size > 0;
  32. }
  33.  
  34. int next(Iterator *iter) {
  35.     iter->size--;
  36.     int val = iter->current->val;
  37.     iter->current = iter->current->next;
  38.     return val;
  39. }
  40.  
  41. void freeList(List *lista) {
  42.     int i = lista->size-1;
  43.     Element *item = lista->first;
  44.     while (i > 0) {
  45.         Element *temp = item;
  46.         item = temp->next;
  47.         free(temp);
  48.         i--;
  49.     }
  50.     free(lista);
  51. }
  52.  
  53. void addInt(List *lista, int val) {
  54.     Element *current = lista->current;
  55.     current->val = val;
  56.     current->next = malloc(sizeof(Element));
  57.     lista->current = current->next;
  58.     lista->size++;
  59. }
  60.  
  61. void addIntAt(List *lista, int val, int index) {
  62.     if (index == lista->size) {
  63.         addInt(lista, val);
  64.     }
  65.     else if (index == 0) {
  66.         Element* oldFirst = lista->first;
  67.         lista->first = malloc(sizeof(Element));
  68.         lista->first->next = oldFirst;
  69.         lista->first->val = val;
  70.         lista->size++;
  71.     }
  72.     else {
  73.         int i = index;
  74.         Element *item = lista->first;
  75.         while (i > 1) {
  76.             item = item->next;
  77.             i--;
  78.         }
  79.         Element* next = item->next;
  80.         Element* newNext = malloc(sizeof(Element));
  81.         item->next = newNext;
  82.         newNext->next = next;
  83.         newNext->val = val;
  84.         lista->size++;
  85.     }
  86.  
  87. }
  88.  
  89. int removeIntAt(List *lista, int index) {
  90.     if (index >= lista->size) return ;
  91.     if (index == 0) {
  92.         Element *first = lista->first;
  93.         Element *toRemove = first;
  94.         lista->first = first->next;
  95.         lista->size--;
  96.         int toReturn = toRemove->val;
  97.         free(toRemove);
  98.         return toReturn;
  99.     }
  100.     int i = index;
  101.     Element *item = lista->first;
  102.     while (i > 1) {
  103.         item = item->next;
  104.         i--;
  105.     }
  106.     if (index == lista->size - 1) {
  107.         lista->size--;
  108.         Element *current = lista->current;
  109.         int toReturn = current->val;
  110.         lista->current = item->next;
  111.         return toReturn;
  112.     }
  113.     Element *item1 = lista->first;
  114.     i = index;
  115.     while (i > -1) {
  116.         item1 = item1->next;
  117.         i--;
  118.     }
  119.     Element *toRemove = item->next;
  120.     item->next = item1;
  121.     lista->size--;
  122.     int toReturn = toRemove->val;
  123.     free(toRemove);
  124.     return toReturn;
  125. }
  126.  
  127. void removeInt(List* lista, int val) {
  128.     Iterator* iter = newIterator(lista);
  129.     int i = 0;
  130.     while(hasNext(iter)) {
  131.         int v = next(iter);
  132.         if (v == val) removeIntAt(lista, i);
  133.         i++;
  134.     }
  135.     free(iter);
  136. }
  137.  
  138. int containsInt(List* lista, int val) {
  139.     Iterator* iter = newIterator(lista);
  140.     int i = 0;
  141.     while(hasNext(iter)) {
  142.         int v = next(iter);
  143.         if (v == val) {
  144.             free(iter);
  145.             return 1;
  146.         }
  147.         i++;
  148.     }
  149.     free(iter);
  150.     return 0;
  151. }
  152.  
  153. int getInt(List *lista, int index) {
  154.     if (index >= lista->size) return NULL;
  155.     int i = index;
  156.     Element *item = lista->first;
  157.     while (i > 0) {
  158.         item = item->next;
  159.         i--;
  160.     }
  161.     return item->val;
  162. }
  163. void setInt(List *lista, int val, int index) {
  164.     if (index >= lista->size) return ;
  165.     int i = index;
  166.     Element *item = lista->first;
  167.     while (i > 0) {
  168.         item = item->next;
  169.         i--;
  170.     }
  171.     item->val = val;
  172. }
  173.  
  174. int main() {
  175.     List* lista = newList();
  176.     addInt(lista, 10);//0
  177.     addInt(lista, 20);//1
  178.     addInt(lista, 30);//2
  179.     addInt(lista, 55);//3
  180.     addIntAt(lista, 38, 3);
  181.     setInt(lista, 11, 0);
  182.     removeIntAt(lista, 1);
  183.     Iterator* iter = newIterator(lista);
  184.     while (hasNext(iter)) {
  185.         printf("%d\n", next(iter));
  186.     }
  187.     free(iter);
  188.     freeList(lista);
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement