Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:04:44+00:00 2026-06-06T10:04:44+00:00

I am trying to implement depth first search algorithm using adjacency list representation of

  • 0

I am trying to implement depth first search algorithm using adjacency list representation of graphs. Here is my code:

#include<stdio.h>
#include<stdlib.h>
struct edge
{
int vertexIndex;
struct edge *edgePtr;
}edge;

struct vertex
{
int vertexKey;
int visited;
struct edge *edgePtr;
}vertex;


void insertVertex(struct vertex *g, int vertexKey, int *vertexCount)
{
g[*vertexCount].vertexKey=vertexKey;
g[*vertexCount].edgePtr=NULL;
g[*vertexCount].visited=0;
(*vertexCount)++;
}

void insertEdge(struct vertex *g,int vertex1, int vertex2)
{
struct edge *e,*e1,*e2;
e=g[vertex1].edgePtr;
while(e&& e->edgePtr)
{
    e=e->edgePtr;
}
e1=(struct edge *)malloc(sizeof(*e1));
e1->vertexIndex=vertex2;
e1->edgePtr=NULL;
if(e)
e->edgePtr=e1;
else
g[vertex1].edgePtr=e1;

e=g[vertex2].edgePtr;
while(e&& e->edgePtr)
{
    e=e->edgePtr;
}
e2=(struct edge *)malloc(sizeof(*e2));
e2->vertexIndex=vertex1;
e2->edgePtr=NULL;
if(e)
e->edgePtr=e2;
else
g[vertex2].edgePtr=e2;
}

void printGraph(struct vertex *g, int *vertexCount)
{
int i;
struct edge *e;
for(i=0;i<*vertexCount;i++)
{
    printf("%d(%d)",i,g[i].vertexKey);
    e=g[i].edgePtr;
    while(e)
    {
        printf("->%d",e->vertexIndex);
        e=e->edgePtr;
    }
    printf("\n");
}
}

void dfs(struct vertex *g,int vertex1)
{
g[vertex1].visited=1;
printf("%d ",vertex1);
struct edge *e;
e=g[vertex1].edgePtr;

while(e)
{
    if(!(g[e->vertexIndex].visited))
    dfs(g,e->vertexIndex);

    e=e->edgePtr;
}
}
main()
{
struct vertex *g1[10];
int vc1=0;
insertVertex(g1,5,&vc1);
insertVertex(g1,3,&vc1);
insertVertex(g1,4,&vc1);
insertVertex(g1,2,&vc1);
insertVertex(g1,9,&vc1);
insertEdge(g1,0,1);
insertEdge(g1,0,2);
insertEdge(g1,1,3);
insertEdge(g1,1,4);
dfs(g1, 0);
return 0;
}

The thing is that when I run this code, at the end of printing ‘dfs’ output, the program stops unexpectedly, I don’t understand why. Can you help me with this please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:04:46+00:00Added an answer on June 6, 2026 at 10:04 am

    I got it!
    There is a small mistake..

    In the main function, the declaration of g1 should be

    struct vertex g1[10];
    

    instead of

    struct vertex *g1[10];
    

    More explanation here: http://iamsoftwareengineer.blogspot.ie/2012/06/c-graph-implementation-with-adjacency.html

    🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement the depth first search algorithm into my game. I
I am trying to implement a modified parallel depth-first search algorithm in Erlang (let's
Trying to implement a search similar to here .This searches properties based on city,locality,property
I am trying to implement a camera controlled scene using the camera code from:
I am trying to implement the adjacency list for a non-weighted graph and a
I am trying implement the Data transformation using Reflection 1 example in my code.
I am trying to implement a recursive depth first traversal on a weighted digraph(,
Im trying to implement a destructor for the objects of linked-list Iv created. I
Found a really nice code for Accordion nav and am trying implement on our
I am getting an error when trying to compile my code in g++ using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.