-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadj_list.c
More file actions
84 lines (77 loc) · 1.54 KB
/
adj_list.c
File metadata and controls
84 lines (77 loc) · 1.54 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "adj_list.h"
int length_al(node* list)
{
int cnt = 0;
node* p = list;
while (p) {
p = p->next;
cnt++;
}
return cnt;
}
void print_list_al(node* list, void (*disp_func)(int))
{
node* aux = list;
while(aux!=NULL){
disp_func(aux->node_num);
aux = aux->next;
}
}
static node* new_node(int node_num, double cost)
{
node* aux = (node*)malloc(sizeof(*aux));
aux->node_num = node_num;
aux->cost = cost;
aux->next = NULL;
return aux;
}
void push_al(node** list, int node_num, double cost)
{
node* new = new_node(node_num, cost);
new->next = *list;
*list = new;
}
void free_list_al(node** list)
{
node* ls = *list, *aux;
while(ls!=NULL){
aux = ls->next;
free(ls);
ls = aux;
}
}
double get_cost(node* list, int to)
{
node* iter = list;
while(iter!=NULL){
if(iter->node_num == to){
return iter->cost;
}
iter=iter->next;
}
return INT_MAX;
}
node* remove_node(node** list, int node_nr)
{
if ((*list)==NULL) {
fprintf(stderr, "empty list, cannot remove\n");
return NULL;
}
node* p, * q;
p = q = *list;
if ((*list)->node_num == node_nr) {
(*list) = (*list)->next;
return p;
}
for (;(p&&p->node_num!=node_nr);q = p, p = p->next);
if(p==NULL){
fprintf(stderr, "node not found\n");
return NULL;
}
q->next = p->next;
return p;
}