Example #1
0
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;
}
Example #2
0
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;
}
Example #3
0
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;
}
Example #5
0
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;
        }
    }
}
Example #6
0
int getNumStrings() {
	return sizeLL(strings);
}