Reallocate My Structure Size in C?

Asked

Viewed 214 times

0

typedef struct tempNo {

    int valor;
    int coluna;
    struct tempNo* prox; 

} NO;

typedef NO* PONT;

typedef struct {

    PONT* A;
    int linhas;
    int colunas;

} MATRIZ;

void inicializaMatriz(MATRIZ* m, int linnha, int coluna) {

    int i;

    m->linhas = linnha;
    m->colunas = coluna;
    m->A = (PONT*) malloc(linnha*sizeof(PONT));
    for (i = 0; i < linnha ; i++){
        m->A[i] = NULL;
    }   
}



int atribuirValor(MATRIZ* m, int lin, int col, int valor) {

    if (lin < 0 || lin >= m->linhas || col < 0 || col >= m->colunas ) return  1;

    PONT ant = NULL;
    PONT atual = m->A[lin];

    while(atual !=  NULL && atual->coluna < col ){

        ant = atual;
        atual = atual->prox;
    }

    if (atual != NULL && atual->coluna == col) {
        if (valor == 0) {
            if (ant == NULL) m->A[lin] = atual->prox;
            else ant->prox = atual->prox;
            free(atual);        
        }
        else  atual->valor = valor;  

    } else if (valor != 0){
        PONT novo = (PONT) malloc (sizeof(NO));
        novo->coluna = col;
        novo->valor = valor;
        novo->prox = atual;
        if (ant == NULL) m->A[lin] = novo;
        else ant->prox = novo;   
    }
    return 0; 

}


int acessarValor(MATRIZ* m, int lin , int col) {

    if (lin < 0 || lin >= m->linhas || col < 0 || col >= m->colunas ) return 0;

    PONT atual = m->A[lin];
    while (atual != NULL && atual->coluna < col )
        atual = atual->prox;
    if (atual != NULL && atual->coluna == col)
        return atual->valor; 
    return 0;

}

void anxexarColuna(MATRIZ* m){

    int i = m->colunas;
    i = i + 1;
    m->colunas = i;
}


void anexarLinha(MATRIZ* m){

    int i = m->linhas;
    i = i + 1;


}
  • What structure are you trying to relocate and where in the code exactly ? When you expose your problem try to be as specific as possible.

  • PONT* A : It is a pointer list for NO that points to the beginning of each list of NO. PONT* A, represents the columns of my Matrix. Already NO are linked lists with pointers to Prox that represent the lines of my Matrix. I want to allocate more space for PONT* A. because as seen in the Attach columns function to add more columns I need to do this, allocate more space in PONT* A.

  • In what function ? reallocate to an array is done with realloc

  • In the column attach function. I tried to use realloc but was unsuccessful.

1 answer

0


Although your doubt is not very clear I chose to answer in it, even to try to help in some points of logic that do not seem to me to be correct.

I start by indicating that there are several small spelling errors that you should correct to make the code as readable as possible.

Examples taken from the code:

  • anxexarColuna
  • linnha

Moving on now to the implementation of the attachment functions.

Attach Line

To MATRIZ keeps an array of lines (in the field A), then adding a row is increasing this array.

This makes this function of attaching line easier because it only needs a reallocation of the array of lines with the function realloc and to increase the value of 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 assured that the contents of the array are preserved even if it is repositioned in memory.

Attach Column

The logic behind this implementation is different because there is no column array. Columns are each of the row nodes, so reallocating the array will not make sense.

Instead we should scroll through each row and add a new node at the end of each, which 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 attach functions in Ideone

  • You solved my problem, thank you so much for everything.

  • @Alexcosta Of nothing. Any question is just put. Rest of good development for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.