I am reproducing a sort ordering algorithm using threaded list.
I'm using two loops using a cell min, iej, with respect to cell j, ok, but when changing the min pointers and i is not working, when i change the two, i starts the loop in the position i would previously be of min, but when I have to point i back to the previous position and start the loop in what would be the i->prox
position I can not without getting lost in the pointers. I already tried auxiliary cells, auxiliary variables, Boolean variables and there was no evolution .
Detail: I can not create cell vectors or auxiliary linked lists.
Code below:
#include<stdio.h>
#include<stdlib.h>
struct cel {
int cont;
struct cel *prox;
};
typedef struct cel celula;
void insere (int x, celula *p) {
celula *nova;
nova = NULL;
nova = (celula *)malloc (sizeof (celula));
nova->cont = x;
nova->prox = p->prox;
p->prox = nova;
}
void selectsort(celula *p) {
celula *ai, *i, *aj, *j, *min, *amin, *pmin;
ai = p;
i = p->prox;
while (i != NULL) {
aj = i;
j = i->prox;
min = i;
while(j != NULL) {
if (j->cont < min->cont) {
amin = aj; min = j, pmin = j->prox;
}
aj = j;
j = j->prox;
}
if (i->cont > min->cont) {
min->prox = i->prox;
ai->prox = min;
i->prox = pmin;
amin->prox = i;
}
ai = i;
i = i->prox;
}
}
void imprima (celula *p) {
celula *v;
for (v = p->prox; v != NULL; v = v->prox)
printf ("%d ", v->cont);
printf("\n");
}
int main () {
celula *l = malloc (sizeof (celula));
l->prox = NULL;
insere(7, l);
insere(11, l);
insere(8, l);
insere(4, l);
insere(12, l);
insere(9, l);
insere(1, l);
imprima(l);
selectsort(l);
printf("\n");
imprima(l);
return 0;
}