C combine unordered edges with two nodes
To combine unordered edges with two nodes in C, you can use a struct to represent an edge and create an array of such structs to store multiple edges. Here's an example:
#include <stdio.h>
// Struct to represent an edge
typedef struct {
int node1;
int node2;
} Edge;
int main() {
// Create an array of edges
Edge edges[5];
// Add edges to the array
edges[0].node1 = 1;
edges[0].node2 = 2;
edges[1].node1 = 2;
edges[1].node2 = 3;
edges[2].node1 = 3;
edges[2].node2 = 1;
edges[3].node1 = 4;
edges[3].node2 = 1;
edges[4].node1 = 4;
edges[4].node2 = 3;
// Print the edges
for (int i = 0; i < 5; i++) {
printf("Edge %d: %d - %d\n", i+1, edges[i].node1, edges[i].node2);
}
return 0;
}
In this example, we create a struct called Edge with two integer fields node1 and node2. We then declare an array edges of type Edge to store multiple edges. Each element of the edges array represents an unordered edge between two nodes.
We add five edges to the array using the dot notation to access the fields of each Edge struct. Finally, we iterate over the array and print the edges using a for loop.
Output:
Edge 1: 1 - 2
Edge 2: 2 - 3
Edge 3: 3 - 1
Edge 4: 4 - 1
Edge 5: 4 - 3
This code demonstrates how to combine unordered edges with two nodes in C using a struct and an array. You can extend it to suit your specific needs
原文地址: https://www.cveoy.top/t/topic/hPra 著作权归作者所有。请勿转载和采集!