-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatriz-esparsa.c
94 lines (83 loc) · 1.67 KB
/
matriz-esparsa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <malloc.h>
typedef enum
{
false = 0,
true = 1
} boolean;
typedef struct aux
{
float valor;
int coluna;
struct aux *prox;
} NO, *PONT;
typedef struct
{
PONT *A;
int linhas;
int colunas;
} MATRIZ;
void inicializarMatriz(MATRIZ *m, int lin, int col);
boolean atribuirMatriz(MATRIZ *m, int lin, int col, float val);
float valorMatriz(MATRIZ *m, int lin, int col);
int main()
{
return 0;
}
void inicializarMatriz(MATRIZ *m, int lin, int col)
{
int i;
m->linhas = lin;
m->colunas = col;
m->A = (PONT *)malloc(lin * sizeof(PONT));
for (i = 0; i < lin; i++)
m->A[i] = NULL;
}
boolean atribuirMatriz(MATRIZ *m, int lin, int col, float val)
{
if (lin < 0 || lin >= m->linhas || col < 0 || col >= m->colunas)
return false;
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 (val == 0)
{
if (ant == NULL)
m->A[lin] = atual->prox;
else
ant->prox = atual->prox;
free(atual);
}
else
atual->valor = val;
}
else
{
PONT novo = (PONT)malloc(sizeof(NO));
novo->coluna = col;
novo->valor = val;
novo->prox = atual;
if (ant == NULL)
m->A[lin] = novo;
else
ant->prox = novo;
}
return true;
}
float valorMatriz(MATRIZ *m, int lin, int col)
{
if (lin < 0 || lin >= m->linhas || col < 0 || col >= m->colunas)
return false;
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;
}