-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
225 lines (193 loc) · 3.85 KB
/
graph.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include<stdlib.h>
#include<stdio.h>
typedef struct Edge{
int value;
struct Edge* next;
}edge;
typedef struct Node{
int value;
struct edge* edge_next;
struct Node* next;
}node;
typedef struct list{
node* head;
int n_vertex;
}List;
void inicializa(List* l){
l->head = NULL;
}
node * create_node(int value){
node *new;
new = (node *) malloc (sizeof(node));
if(new == NULL) return NULL;
new->value = value;
new->edge_next = NULL;
new->next = NULL;
return new;
}
edge * create_edge(int value){
edge *new;
new = (edge *) malloc (sizeof(edge));
if(new == NULL) return NULL;
new->value = value;
new->next = NULL;
return new;
}
int isvoid(List*l){
//Verifica se lista está vazia para fins de add
//Retorna 0 se vazia e 1 se contem elementos
if (l->head == NULL) return 0;
return 1;
}
int procurarVertice(List *l, int value){
//Retorna 1 caso ja exista o valor do nó
//E 0 caso não seja encontrado
node* aux;
aux = l->head;
while(aux!=NULL){
if(aux->value == value) return 1;
aux = aux->next;
}
if(aux == NULL) return 0;
return 1;
}
void addV(List *l, int value){
//add no fim da lista
node* new = create_node(value);
if(l->head==NULL){
l->head = new;
return;
}
node* aux = l->head;
while(aux->next!=NULL){
//não permite criação de valores repetidos
if(aux->value == value) return;
aux = aux->next;
}
if(aux->value == value) return;
aux->next = new;
return;
}
int removerV(List *l, int value){
node * aux = l->head;
//remove cabeça
if(aux->value == value){
l->head = l->head->next;
free(aux);
return 1;
}
while(aux->next!=NULL && aux->next->value!= value){
aux = aux->next;
}
//remove resto
node *erase;
erase = aux->next;
aux->next = erase->next;
free(erase);
return 1;
}
void w_removeE(List *l, int V, int E){
node* aux;
aux = l->head;
while(aux->value!=V){
aux = aux->next;
}
edge* edge_aux = aux->edge_next;
if(edge_aux->value == E){
edge *erase;
erase = edge_aux->next;
edge_aux->next = erase->next;
free(erase);
}
while(edge_aux->next!=NULL && edge_aux->next->value!=E){
edge_aux = edge_aux->next;
}
if(edge_aux->next->value == E){
edge *erase;
erase = edge_aux->next;
edge_aux->next = erase->next;
free(erase);
}
}
int removerE(List *l, int v1, int v2){
if(procurarVertice(l,v1)==1 && procurarVertice(l,v2)==1){
w_removeE(l,v1,v2);
w_removeE(l,v2,v1);
}
}
void imprimirGrafo(List *l){
node * aux = l->head;
while(aux!=NULL){
//Imprime o Vertice
printf(" %d: ",aux->value);
//Imprime lista de arestas
node* edge_aux;
edge_aux = aux->edge_next;
while (edge_aux!=NULL) {
printf(" %d",edge_aux->value);
edge_aux = edge_aux->next;
}
aux = aux->next;
printf("\n");
}
}
void w_add(List *l, int V, int A){
node* aux = l->head;
edge* new = create_edge(A);
//Busca vertice
while(aux->value!=V){
aux = aux->next;
}
//Add ini
if(aux->edge_next == NULL){
aux->edge_next = new;
return;
}
//Add fim
edge* edge_aux = aux->edge_next;
while(edge_aux->next!=NULL){
edge_aux = edge_aux->next;
}
edge_aux->next = new;
return;
}
void addA(List *l, int v1, int v2){
//Função chama wrapper
if(procurarVertice(l,v1)==1 && procurarVertice(l,v2)==1){
w_add(l,v1,v2);
w_add(l,v2,v1);
}
}
/*
int liberar_lista(List *l){
node *aux;
while(isvoid(l)!=0){
aux = l->head;
l->head = l->head->next;
free(aux);
}
return 1;
}*/
int main(void){
List *L = (List*) malloc (sizeof(List));
inicializa(L);
addV(L,1);
addV(L,2);
addV(L,3);
addV(L,4);
addV(L,5);
addV(L,6);
addV(L,7);
addV(L,8);
addV(L,9);
addV(L,10);
addV(L,11);
addA(L,1,2);
addA(L,1,3);
addA(L,2,3);
imprimirGrafo(L);
removerE(L,1,2);
imprimirGrafo(L);
//free(L);
return 0;
}