mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-23 04:02:44 +08:00
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
50 lines
982 B
C
50 lines
982 B
C
/**
|
|
* File: vertex.h
|
|
* Created Time: 2023-10-28
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#ifndef VERTEX_H
|
|
#define VERTEX_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* Vertex structure */
|
|
typedef struct {
|
|
int val;
|
|
} Vertex;
|
|
|
|
/* Constructor: initialize a new vertex */
|
|
Vertex *newVertex(int val) {
|
|
Vertex *vet;
|
|
vet = (Vertex *)malloc(sizeof(Vertex));
|
|
vet->val = val;
|
|
return vet;
|
|
}
|
|
|
|
/* Convert an array of values to an array of vertices */
|
|
Vertex **valsToVets(int *vals, int size) {
|
|
Vertex **vertices = (Vertex **)malloc(size * sizeof(Vertex *));
|
|
for (int i = 0; i < size; ++i) {
|
|
vertices[i] = newVertex(vals[i]);
|
|
}
|
|
return vertices;
|
|
}
|
|
|
|
/* Convert an array of vertices to an array of values */
|
|
int *vetsToVals(Vertex **vertices, int size) {
|
|
int *vals = (int *)malloc(size * sizeof(int));
|
|
for (int i = 0; i < size; ++i) {
|
|
vals[i] = vertices[i]->val;
|
|
}
|
|
return vals;
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // VERTEX_H
|