Although your doubt is not very clear I chose to answer it, even to try to help in some points of logic that do not seem to me to be correct.
I begin by stating that you have several small misspellings that you must correct so that the code is as readable as possible.
Examples taken from the code:
Turning now to the implementation of the attach functions.
Attach Line
The MATRIZ
stores an array of rows (in the A
field), so adding a row is increasing this array.
This makes this line attach function easier because you just need a reallocation of the array of lines with the realloc
and increase the value of the lines in the structure:
void anexarLinha(MATRIZ* m){
m->A = realloc(m->A, (++m->linhas*sizeof(PONT)));
}
The realloc
works in everything similar to malloc
and we are guaranteed that the content of the array is preserved even if it is repositioned in memory.
Append Column
The logic behind the implementation of this is different because there is no array of columns. The columns are each of the nodes of the rows, so reallocating the array will not make sense.
Instead we should go through each line and add a new node at the end of each one, which in itself will represent a new column.
void anexarColuna(MATRIZ* m){
int linha;
//percorrer todas as linhas para que crie um novo nó em cada
for (linha = 0; linha < m->linhas ; linha++){
//criar o novo nó da linha que representa a nova coluna
PONT novo = malloc (sizeof(NO));
novo->valor = 0;
novo->prox = NULL;
if (m->A[linha] == NULL){ //se a linha não tem nada atribui diretamente no array
novo->coluna = 0; //coluna 0 pois é o primeiro nó da linha
m->A[linha] = novo;
}
else { //a linha já tem nós por isso adiciona ao fim
PONT atual = m->A[linha];
while(atual->prox != NULL){
atual=atual->prox;
}
novo->coluna = atual->coluna + 1; //atribuir a coluna correta ao nó
atual->prox = novo;
}
}
m->colunas++; //aumentar o tamanho das colunas na MATRIZ
}
Example working with these attachments in Ideone