You are suggested to use the teaching servers burrow.soic.indiana.edu or hulk.soic.indiana.edu or tank.soic.indiana.edu for practicing C programs.
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vectorList; // Creating a vector of int for (int i=1; i<10; i++) vectorList.push_back(i); // inserting values at the end of the vector cout << "vectorList contains:"; for (vector<int>::iterator it = vectorList.begin() ; it != vectorList.end(); ++it) // iterating through the vector array cout << ' ' << *it; cout << endl; return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <vector> using namespace std; typedef struct edge_struct { int u; int v; } edge; int main() { int numEdge = 10; int u, v; vector<int> graph[50]; edge listOfEdges[10] = {{0,1},{1,2},{2,3},{1,3},{7,6},{6,8},{8,9},{9,6},{5,4},{4,3}}; for(int i = 0; i < numEdge; ++i) { u = listOfEdges[i].u; v = listOfEdges[i].v; graph[u].push_back(v); // For a birectional edge, we should insert both (u,v) and (v,u) as an edge graph[v].push_back(u); } for(int i = 0; i < 50; ++i) { if (graph[i].size() == 0) // There is no edge for node i continue; cout << i << ": "; for(vector<int>::iterator vi = graph[i].begin(); vi != graph[i].end(); ++vi) { cout << *vi << " "; } cout << endl; } return 0; }We could use C linked list for graph representation instead of vector, but vector does the same task as linked list and much easier to implement.
//To check whether node u and v are connected Build Graph Fill up array visited[100] with zeros Call DFS(u) Check for visited[v] global array visited[100]; DFS(u) { if (visited[u] == 1) // This node has already been visited return; visited[u] = 1; // Mark this node as visited For all edge (u, x) DFS(x) }