-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path그래프표현_1.c
172 lines (147 loc) · 2.88 KB
/
그래프표현_1.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable : 4996)
typedef struct Edge {
int weight;
int v1, v2;
struct Edge* next;
}Edge;
typedef struct IncidentVertex {
int adjVertex;
Edge* e;
struct IncidentVertex* next;
}IncidentVertex;
typedef struct Vertex {
int vName;
IncidentVertex* iHead;
struct Vertex* next;
}Vertex;
typedef struct {
Edge* eHead;
Vertex* vHead;
}Graph;
void init(Graph* G) {
G->eHead = NULL;
G->vHead = NULL;
}
void makeVertex(Graph* G, int vName) {
Vertex* v = (Vertex*)malloc(sizeof(Vertex));
v->vName = vName;
v->iHead = NULL;
v->next = NULL;
Vertex* p = G->vHead;
if (p == NULL)
G->vHead = v;
else {
while (p->next != NULL)
p = p->next;
p->next = v;
}
}
Vertex* findVertex(Graph* G, int vName) {
Vertex* p = G->vHead;
while (p != NULL && p->vName != vName)
p = p->next;
return p;
}
void insertIncidentVertex(Vertex* v, int av, Edge* e) {
IncidentVertex* i = (IncidentVertex*)malloc(sizeof(IncidentVertex));
i->adjVertex = av;
i->e = e;
i->next = NULL;
IncidentVertex* p = v->iHead;
if (p == NULL)
v->iHead = i;
else {
while (p->next != NULL)
p = p->next;
p->next = i;
}
}
void insertEdge(Graph* G, int w, int v1, int v2) {
Edge* e = (Edge*)malloc(sizeof(Edge));
e->v1 = v1;
e->v2 = v2;
e->weight = w;
e->next = NULL;
Edge* q = G->eHead;
if (q == NULL)
G->eHead = e;
else {
while (q->next != NULL)
q = q->next;
q->next = e;
}
Vertex* p = findVertex(G, v1);
insertIncidentVertex(p, v2, e);
p = findVertex(G, v2);
insertIncidentVertex(p, v1, e);
}
void modifyWeight(Graph* G, int v1, int v2, int w) {
Vertex* p = findVertex(G, v1);
IncidentVertex* r = p->iHead;
while (r != NULL && r->adjVertex != v2) {
r = r->next;
}
if (r == NULL)
insertEdge(G, w, v1, v2);
else
r->e->weight = w;
}
void printVertex(Graph* G, int vName) {
Vertex* p = G->vHead;
while (p->vName != vName)
p = p->next;
IncidentVertex* r = p->iHead;
int* arr = (int*)calloc(7, sizeof(int));
for (; r != NULL; r = r->next)
arr[r->adjVertex] = r->e->weight;
for (int i = 1; i <= 6; i++) {
if (arr[i] != 0)
printf(" %d %d", i, arr[i]);
}
printf("\n");
}
int main()
{
Graph G;
init(&G);
for (int i = 1; i <= 6; i++)
makeVertex(&G, i);
insertEdge(&G, 1, 1, 2);
insertEdge(&G, 1, 1, 3);
insertEdge(&G, 1, 1, 4);
insertEdge(&G, 2, 1, 6);
insertEdge(&G, 1, 2, 3);
insertEdge(&G, 4, 3, 5);
insertEdge(&G, 4, 5, 5);
insertEdge(&G, 3, 5, 6);
char order;
int v1, v2, w;
while (1) {
scanf("%c", &order);
switch (order) {
case 'a':
scanf("%d", &v1);
if (v1 >= 1 && v1 <= 6)
printVertex(&G, v1);
else
printf("-1\n");
break;
case 'm':
scanf("%d %d %d", &v1, &v2, &w);
if ((v1 >= 1 && v1 <= 6) && (v2 >= 1 && v2 <= 6))
modifyWeight(&G, v1, v2, w);
else
printf("-1\n");
break;
case 'q':
return 0;
default:
break;
}
getchar();
}
return 0;
}