void list_enqueue(struct List *l, struct Node *n) { // Empty list so just add it if (list_isEmpty(l)) { list_addHead(l, n); return; } // Find the first node with lower priority to ours. // So, if we have nodes of the same priority we skip it as we // enqueue after it struct Node *c = l->l_head; while (list_isNode(c) && c->pri >= n->pri) c = c->n_succ; if (list_isNode(c)) { // If it's the head then just add to the head if (list_isHead(c)) list_addHead(l, n); else { // insert before the found node n->n_pred = c->n_pred; n->n_succ = c; c->n_pred = n; n->n_pred->n_succ = n; } } else // Add to the tail list_addTail(l, n); }
/* Syfte: Avallokera minne som används av listan. Parametrar: listan (list *) Kommentarer: Minne för ev värden i listan avallokeras bara om en funktion för detta är satt via list_setMemFunc */ void list_free(list *l){ list_position temppos=list_first(l); while(!list_isEmpty(l)){ temppos=list_remove(l,temppos); } free(l->top); free(l->bottom); free(l); return; }
static int compare_help(int so_far, list_t list, int (*fn)(int, int)) { if (list_isEmpty(list)) { return so_far; } else { so_far = fn(so_far, list_first(list)); return compare_help(so_far, list_rest(list), fn); } }
/* ============================================================================= * hashtable_isEmpty * ============================================================================= */ bool_t hashtable_isEmpty (hashtable_t* hashtablePtr) { #ifdef HASHTABLE_SIZE_FIELD return ((hashtablePtr->size == 0) ? TRUE : FALSE); #else long i; for (i = 0; i < hashtablePtr->numBucket; i++) { if (!list_isEmpty(hashtablePtr->buckets[i])) { return FALSE; } } return TRUE; #endif }
void list_print( const List L ) { //*** Runs in O(n) if (list_isEmpty ( L ) == 0) // if there are data in the list { nodeType *X = L -> head; printf ("Data in the list: \n"); int i; for (i = 0; i < list_length( L ); i++) // go through the list { printf ("-> [ %d ] \n", X -> data); X = X -> next; //move on to the next node } } else // if the list is empty { printf ("The list is empty \n"); } }
/* Syfte: L�gga till ett v�rde p� r�tt st�lle utifr�n sin prioritet i priok�n Parametrar: q - priok�n (pqueue *) d- v�rdet som ska l�ggas till (data) Kommentarer: */ void pqueue_insert(pqueue *q,data d){ MyPQ *prioq = (MyPQ*)q; int placed = 0; list_position pos =list_first(prioq->pq); if (list_isEmpty(prioq->pq)){ list_insert(prioq->pq, d, list_first(prioq->pq)); }else{ while(!placed && pos!=list_end(prioq->pq)){ if(prioq->cf(d, list_inspect(prioq->pq, pos))){ placed = 1; list_insert(prioq->pq, d, pos); } pos = list_next(prioq->pq, pos); } if (!placed){ list_insert(prioq->pq, d, pos); } } }
/* Syfte: Konrollera om kön är tom Parametrar: q - Kön (queue *) Returvärde: true om kön är tom false annars Kommentarer: */ bool queue_isEmpty(queue *q) { return list_isEmpty(q); }
static list_t append_helper(list_t first, list_t appended) { if (list_isEmpty(first)) return appended; return append_helper(list_rest(first), list_make(list_first(first), appended)); }
static list_t reverse_helper(list_t list, list_t reversed) { if (list_isEmpty(list)) return reversed; return reverse_helper(list_rest(list), list_make(list_first(list), reversed)); }
static int product_helper(list_t list, int product) { if (list_isEmpty(list)) return product; return product_helper(list_rest(list), product * list_first(list)); }
static int sum_helper(list_t list, int sum) { if (list_isEmpty(list)) return sum; return sum_helper(list_rest(list), sum + list_first(list)); }
static list_t rotate_helper(list_t list, unsigned int n, list_t rotated) { if (!n) return append(list, reverse(rotated)); if (list_isEmpty(list) && n) rotate_helper(reverse(rotated), n-1, list_make()); return rotate_helper(list_rest(list), n-1, list_make(list_first(list), rotated)); }
static list_t filter_helper(list_t list, bool (*fn)(int), list_t filteredList) { if (list_isEmpty(list)) return filteredList; if (fn(list_first(list))) return filter_helper(list_rest(list), fn, list_make(list_first(list), filteredList)); return filter_helper(list_rest(list), fn, filteredList); }
static list_t filter_even_helper(list_t list, list_t filteredList) { if (list_isEmpty(list)) return filteredList; if (!list_first(list)%2) return filter_even_helper(list_rest(list), list_make(list_first(list), filteredList)); return filter_even_helper(list_rest(list), filteredList); }
void exampleList() { // Use pooling for efficiency, if you don't want to use pooling // then comment out this line. pool_list(16); List* L = newList(); list_add(L, "a"); list_add(L, "b"); list_add(L, "c"); list_add(L, "d"); list_add(L, "e"); list_add(L, "f"); // Display the current list displayStrings(L); //ABCDEF // Remove first item list_removeFirst(L); displayStrings(L); //BCDEF // Add first item back list_addFirst(L, "a"); displayStrings(L); list_clear(L); if (list_isEmpty(L)) printf("List was cleared.\n"); // Add some strings and remove all that begin with - int nums[] = {1, 2, 3, 4, 6, 7, 8}; int x; for (x = 0; x < 7; x++) list_add(L, &nums[x]); displayIntegers(L); list_start(L); while (list_hasNext(L)) { // get does not move the current node x = *((int*)list_peek(L)); if (x % 2 == 0) // remove will remove the node from the list altogether and // return the data removed. list_remove(L); else // next will just goto the next node and return the data. list_next(L); } // Print out the odd numbers displayIntegers(L); // Try removing all while traversing list_start(L); while (list_hasNext(L)) list_remove(L); displayIntegers(L); if (L->first == NULL && L->last == NULL && L->size == 0) printf("All cleaned up!\n"); // Traverse through an array of strings and find any that start // with . and after it add 0 and add one before it that is - list_add(L, ".1"); list_add(L, " two "); list_add(L, ".3"); list_add(L, " four "); list_add(L, ".5"); displayStrings(L); list_start(L); char* c; while (list_hasNext(L)) { c = (char*)list_peek(L); if (c[0] == '.') { list_insertBefore(L, "-"); list_insertAfter(L, "0"); } list_next(L); } displayStrings(L); char* first = (char*)L->first->data; char* last = (char*)L->last->data; if (first[0] == '-' && last[0] == '+') printf("Insertions correct.\n"); // This will clear the list of any nodes and pool them and then free // the list itself from memory list_free(L); // If you're not using pooling this can be commented out. This will // free all pooled nodes from memory. Always call this at the end // of using any List. unpool_list(); }
static int accumulate_helper(list_t list, int (*fn)(int, int), int result) { if (list_isEmpty(list)) return result; return accumulate_helper(list_rest(list), fn, fn(result, list_first(list))); }
int main(int argc, char *argv[]){ int it; int *copia; int numElementos, i; FILE *f = NULL; List *list = NULL; List *laux = NULL; EleList *pele = NULL; if(argc != 2){ printf("Faltan argumentos\n"); return -1; } f = fopen(argv[1], "r"); if (!f){ return -1; } list = list_ini(); if(!list){ fclose(f); return -1; } while(feof(f) == 0){ /*Bucle que inserta dependiendo de si es par o impar*/ copia = (int *)malloc(sizeof(int)); if(!copia){ list_free(list); fclose(f); return -1; } fscanf(f, "%d", &it); pele = elelist_ini(); if(!pele){ free(copia); list_free(list); fclose(f); return -1; } *copia = it; if(!elelist_setInfo(pele, copia)){ list_free(list); fclose(f); free(copia); elelist_free(pele); return -1; } if((it)%2){ if(!list_insertLast(list, pele)){ list_free(list); fclose(f); elelist_free(pele); return -1; } if(list_print(stdout, list) < 0){ list_free(list); fclose(f); elelist_free(pele); return -1; } fprintf(stdout, "\n"); }else{ if(!list_insertFirst(list, pele)){ list_free(list); fclose(f); elelist_free(pele); return -1; } if(list_print(stdout, list)<0){ list_free(list); fclose(f); elelist_free(pele); return -1; } fprintf(stdout, "\n"); } elelist_free(pele); } fclose(f); numElementos = list_size(list); printf("Lista con %d elementos: \n", numElementos); if(list_print(stdout, list)<0){ list_free(list); return -1; } fprintf(stdout, "\n"); laux = list_ini(); if(!laux){ list_free(list); return -1; } for(i=0; i< (numElementos/2); i++){ pele = list_extractFirst(list); printf("Elemento extraido: "); elelist_print(stdout, pele); fprintf(stdout, "\n"); list_print(stdout, list); fprintf(stdout, "\n"); if(!pele){ list_free(list); list_free(laux); return -1; } if(!list_insertInOrder(laux, pele)){ list_free(list); list_free(laux); elelist_free(pele); return -1; } list_print(stdout, laux); fprintf(stdout, "\n"); elelist_free(pele); } while(list_isEmpty(list) == FALSE){ pele = list_extractLast(list); if(!pele){ list_free(list); list_free(laux); return -1; } printf("Elemento extraido: "); elelist_print(stdout, pele); fprintf(stdout, "\n"); list_print(stdout, list); fprintf(stdout, "\n"); if(!list_insertInOrder(laux, pele)){ list_free(list); list_free(laux); elelist_free(pele); return -1; } list_print(stdout, laux); fprintf(stdout, "\n"); elelist_free(pele); } printf("Lista con %d elementos: \n", list_size(laux)); if(list_print(stdout, laux)<0){ list_free(laux); return -1; } fprintf(stdout, "\n"); list_free(laux); list_free(list); return 0; }
/* Syfte: Konrollera om priok�n �r tom Parametrar: q - priok�n (pqueue *) Returv�rde: true om priok�n �r tom false annars Kommentarer: */ bool pqueue_isEmpty(pqueue *q){ MyPQ *prioq = (MyPQ*)q; return list_isEmpty(prioq->pq); }