void addLL(LinkedList* liste, void* element, unsigned long index) { if (index == 0) { addFirstLL(liste, element); return; } unsigned long l = sizeLL(liste); if (index == l) { addLastLL(liste, element); return; } if (index > l) { printf("list out of bounds : %lu (taille : %lu)", index, l); exit(1); } liste->longueur++; LinkedList* position = liste->suiv; for (unsigned long i = 1; i < index; i++) { position = position->suiv; } LinkedList* nouv = newLinkedList(); nouv->valeur = element; nouv->prec = position->prec; nouv->suiv = position; (position->prec)->suiv = nouv; position->prec = nouv; }
char * newString(int size) { char * string; string = (char *) malloc(size + 1); memset(string, 0, size + 1); if (!NULL) { insertLL(strings, string, sizeLL(strings) + 1); } showLL(strings); return string; }
void addLastLL(LinkedList* liste, void* element) { if (sizeLL(liste) == 0) { liste->longueur++; liste->valeur = element; } else { liste->longueur++; LinkedList* nouv = newLinkedList(); nouv->valeur = element; (liste->dernier)->suiv = nouv; nouv->prec = liste->dernier; liste->dernier = nouv; } }
void swapNodes(struct node **root, int k) { int size = sizeLL(*root); int f, l; if (k <= 0 || k > size) { printf("Invalid k!\n"); return; } if (k <= size/2) { f = k; l = size - k + 1; } else { f = size - k + 1; l = k; } // f is always <= l struct node *n1, *n1_prev, *n2, *n2_prev, *temp; int i; // seeking the pointers to n1 n1 = *root; n1_prev = NULL; for (i = 1; i < f && n1; i++) { n1_prev = n1; n1 = n1->next; } // seeking the pointers to n2 n2 = *root; n2_prev = NULL; for (i = 1; i < l && n2; i++) { n2_prev = n2; n2 = n2->next; } // do this only for non-root n1 if (n1_prev) { n1_prev->next = n2; } else { (*root) = n2; } n2_prev->next = n1; temp = n1->next; n1->next = n2->next; n2->next = temp; }
void addFirstLL(LinkedList* liste, void* element) { if (sizeLL(liste) == 0) { liste->longueur++; liste->valeur = element; } else { liste->longueur++; LinkedList* nouv = newLinkedList(); nouv->valeur = liste->valeur; nouv->suiv = liste->suiv; nouv->prec = liste; if (liste->suiv != NULL) { (liste->suiv)->prec = nouv; } liste->valeur = element; liste->suiv = nouv; if (nouv->suiv == NULL) { liste->dernier = nouv; } } }
int getNumStrings() { return sizeLL(strings); }