What is the problem in my code?

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
#include<iostream>
using namespace std;

class list {
public:
int value;
class list* next;
};

typedef class list node;
typedef node* link;
class list* head[9];
void dfs(int);
int run[9];

int main()
{
link ptr,newnode;
int data[20][2] =
{{1,2},{2,1},
{1,3},{3,1},
{2,4},{4,2},
{2,5},{5,2},
{3,6},{6,3},
{3,7},{7,3},
{4,5},{5,4},
{6,7},{7,6},
{5,8},{8,5},
{6,8},{8,6}};

for (int i=1;i<=8;i++)
{
run[i]=0;
head[i]=new node;
head[i]->value=i;
head[i]->next=NULL;
ptr=head[i];
cout<<"Vertex "<<i<<"=>";
for(int j=0;j<20;j++)
{
if (data[j][0]==i)
{
newnode=new node;
newnode->value=data[j][1];
newnode->next=NULL;
while(ptr!=NULL)
ptr=ptr->next;
ptr=newnode;
cout<<"["<<ptr->value<<"]";
}
}
cout<<endl;
}

cout<<"Depth-first search:"<<endl;
dfs(1);
cout<<endl; 
system("pause");
}

void dfs(int current)
{
link ptr;
run[current]=1;
cout<<"["<<current<<"]";
ptr=head[current]->next;
while(ptr!=NULL)
{
if (run[ptr->value]==0)
dfs(ptr->value);
ptr=ptr->next;
}
}


The problem is when I run dfs(1) (Line 56), it always show "[1]" only, instead of showing the whole route.
To be precise, when I check by debugger and go through run[1]=1 in Line 64, then in line 66, the ptr (ptr=head[1]->next, which I supposed it should be pointing to "2"), it just become a NULL pointer...and I am clueless why it happens
Thank you for answering..
Last edited on
Topic archived. No new replies allowed.