#include <stdio.h>
#include <stdlib.h>
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;
}
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)
return atual->valor;
return 0;
}
void anexarColuna(MATRIZ* m){
int linha;
for (linha = 0; linha < m->linhas ; linha++){
PONT novo
= malloc (sizeof(NO
)); novo->valor = 0;
novo->prox = NULL;
if (m->A[linha] == NULL){
novo->coluna = 0;
m->A[linha] = novo;
}
else {
PONT atual = m->A[linha];
while(atual->prox != NULL){
atual=atual->prox;
}
novo->coluna = atual->coluna + 1;
atual->prox = novo;
}
}
m->colunas++;
}
void anexarLinha(MATRIZ* m){
m
->A
= realloc(m
->A
, (++m
->linhas
*sizeof(PONT
)));}
int main()
{
MATRIZ m;
inicializaMatriz(&m, 3, 3);
atribuirValor(&m, 1, 1, 10);
printf("\nEm %d %d tem o valor %d (linhas:%d , colunas:%d)", 1, 1, acessarValor
(&m
,1,1), m.
linhas, m.
colunas);
anexarLinha(&m);
atribuirValor(&m, 3, 1, 15);
printf("\nEm %d %d tem o valor %d (linhas:%d , colunas:%d)", 3, 1, acessarValor
(&m
,3,1),m.
linhas, m.
colunas);
anexarColuna(&m);
anexarColuna(&m);
anexarColuna(&m);
atribuirValor(&m, 1, 5, 20);
printf("\nEm %d %d tem o valor %d (linhas:%d , colunas:%d)", 1, 5, acessarValor
(&m
,1,5),m.
linhas, m.
colunas);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IHRlbXBObyB7CgogICAgaW50IHZhbG9yOwogICAgaW50IGNvbHVuYTsKICAgIHN0cnVjdCB0ZW1wTm8qIHByb3g7Cgp9IE5POwoKdHlwZWRlZiBOTyogUE9OVDsKCnR5cGVkZWYgc3RydWN0IHsKCiAgICBQT05UKiBBOwogICAgaW50IGxpbmhhczsKICAgIGludCBjb2x1bmFzOwoKfSBNQVRSSVo7Cgp2b2lkIGluaWNpYWxpemFNYXRyaXooTUFUUklaKiBtLCBpbnQgbGlubmhhLCBpbnQgY29sdW5hKSB7CgogICAgaW50IGk7CgogICAgbS0+bGluaGFzID0gbGlubmhhOwogICAgbS0+Y29sdW5hcyA9IGNvbHVuYTsKICAgIG0tPkEgPSAoUE9OVCopIG1hbGxvYyhsaW5uaGEqc2l6ZW9mKFBPTlQpKTsKCiAgICBmb3IgKGkgPSAwOyBpIDwgbGlubmhhIDsgaSsrKXsKICAgICAgICBtLT5BW2ldID0gTlVMTDsKICAgIH0KfQoKCgppbnQgYXRyaWJ1aXJWYWxvcihNQVRSSVoqIG0sIGludCBsaW4sIGludCBjb2wsIGludCB2YWxvcikgewoKICAgIGlmIChsaW4gPCAwIHx8IGxpbiA+PSBtLT5saW5oYXMgfHwgY29sIDwgMCB8fCBjb2wgPj0gbS0+Y29sdW5hcyApIHJldHVybiAgMTsKCiAgICBQT05UIGFudCA9IE5VTEw7CiAgICBQT05UIGF0dWFsID0gbS0+QVtsaW5dOwoKICAgIHdoaWxlKGF0dWFsICE9ICBOVUxMICYmIGF0dWFsLT5jb2x1bmEgPCBjb2wgKXsKICAgICAgICBhbnQgPSBhdHVhbDsKICAgICAgICBhdHVhbCA9IGF0dWFsLT5wcm94OwogICAgfQoKICAgIGlmIChhdHVhbCAhPSBOVUxMICYmIGF0dWFsLT5jb2x1bmEgPT0gY29sKSB7CiAgICAgICAgaWYgKHZhbG9yID09IDApIHsKICAgICAgICAgICAgaWYgKGFudCA9PSBOVUxMKSBtLT5BW2xpbl0gPSBhdHVhbC0+cHJveDsKICAgICAgICAgICAgZWxzZSBhbnQtPnByb3ggPSBhdHVhbC0+cHJveDsKICAgICAgICAgICAgZnJlZShhdHVhbCk7CiAgICAgICAgfQogICAgICAgIGVsc2UgIGF0dWFsLT52YWxvciA9IHZhbG9yOwoKICAgIH0gZWxzZSBpZiAodmFsb3IgIT0gMCl7CiAgICAgICAgUE9OVCBub3ZvID0gKFBPTlQpIG1hbGxvYyAoc2l6ZW9mKE5PKSk7CiAgICAgICAgbm92by0+Y29sdW5hID0gY29sOwogICAgICAgIG5vdm8tPnZhbG9yID0gdmFsb3I7CiAgICAgICAgbm92by0+cHJveCA9IGF0dWFsOwogICAgICAgIGlmIChhbnQgPT0gTlVMTCkgbS0+QVtsaW5dID0gbm92bzsKICAgICAgICBlbHNlIGFudC0+cHJveCA9IG5vdm87CiAgICB9CiAgICByZXR1cm4gMDsKCn0KCgppbnQgYWNlc3NhclZhbG9yKE1BVFJJWiogbSwgaW50IGxpbiAsIGludCBjb2wpIHsKCiAgICBpZiAobGluIDwgMCB8fCBsaW4gPj0gbS0+bGluaGFzIHx8IGNvbCA8IDAgfHwgY29sID49IG0tPmNvbHVuYXMgKSByZXR1cm4gMDsKCiAgICBQT05UIGF0dWFsID0gbS0+QVtsaW5dOwogICAgd2hpbGUgKGF0dWFsICE9IE5VTEwgJiYgYXR1YWwtPmNvbHVuYSA8IGNvbCApCiAgICAgICAgYXR1YWwgPSBhdHVhbC0+cHJveDsKICAgIGlmIChhdHVhbCAhPSBOVUxMKQogICAgICAgIHJldHVybiBhdHVhbC0+dmFsb3I7CiAgICByZXR1cm4gMDsKCn0KCnZvaWQgYW5leGFyQ29sdW5hKE1BVFJJWiogbSl7CgogICAgaW50IGxpbmhhOwogICAgZm9yIChsaW5oYSA9IDA7IGxpbmhhIDwgbS0+bGluaGFzIDsgbGluaGErKyl7CgogICAgICAgIFBPTlQgbm92byA9IG1hbGxvYyAoc2l6ZW9mKE5PKSk7CiAgICAgICAgbm92by0+dmFsb3IgPSAwOwogICAgICAgIG5vdm8tPnByb3ggPSBOVUxMOwoKICAgICAgICBpZiAobS0+QVtsaW5oYV0gPT0gTlVMTCl7CiAgICAgICAgICAgIG5vdm8tPmNvbHVuYSA9IDA7CiAgICAgICAgICAgIG0tPkFbbGluaGFdID0gbm92bzsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIFBPTlQgYXR1YWwgPSBtLT5BW2xpbmhhXTsKCiAgICAgICAgICAgIHdoaWxlKGF0dWFsLT5wcm94ICE9IE5VTEwpewogICAgICAgICAgICAgICAgYXR1YWw9YXR1YWwtPnByb3g7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIG5vdm8tPmNvbHVuYSA9IGF0dWFsLT5jb2x1bmEgKyAxOwogICAgICAgICAgICBhdHVhbC0+cHJveCA9IG5vdm87CiAgICAgICAgfQogICAgfQoKICAgIG0tPmNvbHVuYXMrKzsKfQoKCnZvaWQgYW5leGFyTGluaGEoTUFUUklaKiBtKXsKICAgIG0tPkEgPSByZWFsbG9jKG0tPkEsICgrK20tPmxpbmhhcypzaXplb2YoUE9OVCkpKTsKfQoKCgppbnQgbWFpbigpCnsKCgogICAgTUFUUklaIG07CgogICAgaW5pY2lhbGl6YU1hdHJpeigmbSwgMywgMyk7CiAgICBhdHJp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