Please correct my code below: class Node: def __init__(self, data): self.data =
ID: 3789399 • Letter: P
Question
Please correct my code below:
class Node:
def __init__(self, data):
self.data = data
self.adj = []
self.visited = False
def adjNode(self, new_adj_node):
self.adj.append(new_adj_node)
r = Node('r')
u = Node('u')
y = Node('y')
t = Node('t')
x = Node('x')
z = Node('z')
q = Node('q')
w = Node('w')
s = Node('s')
v = Node('v')
r.adjNode(u)
r.adjNode(y)
u.adjNode(y)
y.adjNode(t)
y.adjNode(q)
t.adjNode(x)
x.adjNode(z)
t.adjNode(y)
z.adjNode(q)
q.adjNode(w)
q.adjNode(s)
q.adjNode(t)
w.adjNode(s)
s.adjNode(v)
def dfs(r):
stack = []
stack.append(r)
while(len(stack>0)):
r=stack.pop()
if r=None:
stack.append(u)
if y!=None:
stack.append(t)
dfs(r)
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node *next;
int ver;
}node;
node *G[20];
int visitednode[20];
int n;
void read_graph();
void insert(int,int);
void DFS(int);
void main()
{
int i;
read_graph();
for(i=0;i<n;i++)
visitednode[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf(" %d",i);
p=G[i];
visitednode[i]=1;
while(p!=NULL)
{
i=p->ver;
if(!visitednode[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
G[i]=NULL;
printf("Enter number of edges:");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}
void insert(int vi,int vj)
{
node *p,*q;
q=(node*)malloc(sizeof(node));
q->ver=vj;
q->next=NULL;
if(G[vi]==NULL)
G[vi]=q;
else
{
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.