Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Element {
- struct element *next;
- int val;
- } Element;
- typedef struct List {
- struct Element *first;
- struct Element *current;
- int size;
- } List;
- typedef struct Iterator {
- struct Element *current;
- int size;
- } Iterator;
- List* newList() {
- List *lista = malloc(sizeof(List));
- lista->first = malloc(sizeof(Element));
- lista->current = lista->first;
- lista->size = 0;
- return lista;
- }
- Iterator* newIterator(List *lista) {
- Iterator *iter = malloc(sizeof(Iterator));
- iter->current = lista->first;
- iter->size = lista->size;
- }
- int hasNext(Iterator *iter) {
- return iter->size > 0;
- }
- int next(Iterator *iter) {
- iter->size--;
- int val = iter->current->val;
- iter->current = iter->current->next;
- return val;
- }
- void freeList(List *lista) {
- int i = lista->size-1;
- Element *item = lista->first;
- while (i > 0) {
- Element *temp = item;
- item = temp->next;
- free(temp);
- i--;
- }
- free(lista);
- }
- void addInt(List *lista, int val) {
- Element *current = lista->current;
- current->val = val;
- current->next = malloc(sizeof(Element));
- lista->current = current->next;
- lista->size++;
- }
- void addIntAt(List *lista, int val, int index) {
- if (index == lista->size) {
- addInt(lista, val);
- }
- else if (index == 0) {
- Element* oldFirst = lista->first;
- lista->first = malloc(sizeof(Element));
- lista->first->next = oldFirst;
- lista->first->val = val;
- lista->size++;
- }
- else {
- int i = index;
- Element *item = lista->first;
- while (i > 1) {
- item = item->next;
- i--;
- }
- Element* next = item->next;
- Element* newNext = malloc(sizeof(Element));
- item->next = newNext;
- newNext->next = next;
- newNext->val = val;
- lista->size++;
- }
- }
- int removeIntAt(List *lista, int index) {
- if (index >= lista->size) return ;
- if (index == 0) {
- Element *first = lista->first;
- Element *toRemove = first;
- lista->first = first->next;
- lista->size--;
- int toReturn = toRemove->val;
- free(toRemove);
- return toReturn;
- }
- int i = index;
- Element *item = lista->first;
- while (i > 1) {
- item = item->next;
- i--;
- }
- if (index == lista->size - 1) {
- lista->size--;
- Element *current = lista->current;
- int toReturn = current->val;
- lista->current = item->next;
- return toReturn;
- }
- Element *item1 = lista->first;
- i = index;
- while (i > -1) {
- item1 = item1->next;
- i--;
- }
- Element *toRemove = item->next;
- item->next = item1;
- lista->size--;
- int toReturn = toRemove->val;
- free(toRemove);
- return toReturn;
- }
- void removeInt(List* lista, int val) {
- Iterator* iter = newIterator(lista);
- int i = 0;
- while(hasNext(iter)) {
- int v = next(iter);
- if (v == val) removeIntAt(lista, i);
- i++;
- }
- free(iter);
- }
- int containsInt(List* lista, int val) {
- Iterator* iter = newIterator(lista);
- int i = 0;
- while(hasNext(iter)) {
- int v = next(iter);
- if (v == val) {
- free(iter);
- return 1;
- }
- i++;
- }
- free(iter);
- return 0;
- }
- int getInt(List *lista, int index) {
- if (index >= lista->size) return NULL;
- int i = index;
- Element *item = lista->first;
- while (i > 0) {
- item = item->next;
- i--;
- }
- return item->val;
- }
- void setInt(List *lista, int val, int index) {
- if (index >= lista->size) return ;
- int i = index;
- Element *item = lista->first;
- while (i > 0) {
- item = item->next;
- i--;
- }
- item->val = val;
- }
- int main() {
- List* lista = newList();
- addInt(lista, 10);//0
- addInt(lista, 20);//1
- addInt(lista, 30);//2
- addInt(lista, 55);//3
- addIntAt(lista, 38, 3);
- setInt(lista, 11, 0);
- removeIntAt(lista, 1);
- Iterator* iter = newIterator(lista);
- while (hasNext(iter)) {
- printf("%d\n", next(iter));
- }
- free(iter);
- freeList(lista);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement