'Implementing Depth First search using Adjacency linked list graph

I've been trying to implement this algorithm but the code doesn't seem to print all the results for some reason. Please note that I've represented the graph using linked list. I've changed the "visted" array to a boolean variable inside the node.

#include <iostream>
using namespace std;

struct node{
    int x;
    node *link;
    bool marked = false;
} *p1, *p2;



void DFS(node *startPoint){
    
    cout<<startPoint->x<<" ";
    startPoint->marked = true;
    while(startPoint->link!= NULL)
    {
        startPoint = startPoint->link;
        if(startPoint->marked == false)
        {
            DFS(startPoint);
        }
    }
}

int main() {
    int size;
    int depth_first;
    cout<<"Enter number of nodes: ";
    cin>>size;
    node *N[size];
    int edges;
    for(int i = 0; i < size;i++ ){
        N[i] = new node;
        cout<<"Enter node #"<<i+1<<":";
        cin>>N[i]->x;
        cout<<"How many edges in node("<<N[i]->x<<")? = ";
        cin>>edges;
        p1 = N[i];
        for(int j =0; j< edges; j++){
            p2 = new node;
            cout<<"Enter edge #"<<j+1<<":";
            cin>>p2->x;
            p1->link = p2;
            p1 = p1->link;
        }
        p2 = NULL;
    }
    
    cout<<"Enter the start of the depth first search: ";
    cin>>depth_first;
    DFS(N[depth_first]);
    
    return 0;
}

Input:

node 0 = edges 1 2

node 1 = edges 0 1 3

node 2 = edges 0 1

node 3 = edges 1

depth_first = 0

Output:

0 1 2

c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source