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 883805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:39:09+00:00 2026-05-15T12:39:09+00:00

This Code is a code I built from the algorithm design manual book but

  • 0

This Code is a code I built from the algorithm design manual book but I can’t make it compile cause I’ve got little experience with pointers I think that’s the main reason I think I can’t compile it:

And if someone can change a little bit in the djikstra to make it through heap with the current configuration.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int MAXV=1000;
const int MAXINT=99999;

typedef struct{
    int y;
    int weight;
    struct edgenode *next;
}edgenode;
typedef struct{
    edgenode *edges[MAXV+1];
    int degree[MAXV+1];
    int nvertices;
    int nedges;
    bool directed;
}graph;

void add_edge(graph *g,int x,int y,int weight,bool directed);

void read_graph(graph *g,bool directed){
    int x,y,weight,m;
    g->nvertices=0;
    g->nedges=0;
    g->directed=directed;
    for(int i=1;i<MAXV;++i) g->degree[i]=0;
    for(int i=1;i<MAXV;++i) g->edges[i]=NULL;
    scanf("%d %d",&(g->nvertices),&m);
    for(int i=1;i<=m;++i){
        scanf("%d %d %d",&x,&y,&weight);
        add_edge(g,x,y,weight,directed);
    }
}

void add_edge(graph *g,int x,int y,int weight,bool directed){
    edgenode *p;
    p=malloc(sizeof(edgenode));
    p->weight=weight;
    p->y=y;
    p->next=g->edges[x];

    g->edges[x]=p;
    g->degree[x]++;
    if(directed==false) add_edge(g,y,x,weight,true);
    else g->nedges++;
}

int dijkstra(graph *g,int start,int end){
    edgenode *p;
    bool intree[MAXV+1];
    int distance[MAXV+1];
    for(int i=1;i<=g->nvertices;++i){
        intree[i]=false;
        distance[i]=MAXINT;
    }
    distance[start]=0;
    int v=start;
    while(intree[v]==false){
        intree[v]=true;
        p=g->edges[v];
        while(p!=NULL){
            int cand=p->y;
            int weight=p->weight;
            if(distance[cand] > distance[v]+weight) distance[cand]=distance[v]+weight;
            p=p->next;
        }
        v=1;
        int dist=MAXINT;
        for(int i=1;i<=g->nvertices;++i)
            if((intree[i]==false) && (dist > distance[i])){
                dist=distance[i];
                v=i;
            }
    }
    return distance[end];
}

int main(){
    graph g;
    read_graph(&g,false);
    int x=1,y,shortest;
    while(x!=0){
        scanf("%d %d",&x,&y);
        shortest=dijkstra(&g,x,y);
        printf("The shortest path from %d to %d is %d",x,y,shortest);
    }
    return 0;
}
  • 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-05-15T12:39:10+00:00Added an answer on May 15, 2026 at 12:39 pm

    Change the definition of the struct, and it would compile.

    struct edgenode_tag 
    {
       int y;
       int weight;
       struct edgenode_tag *next;
    };
    typedef edgenode_tag edgenode; 
    

    While this will solve your problem, don’t trust my answer below until someone better than me comments on it.


    What was wrong in your code ?

    You are using the typedef-ed type before the compiler knows about that type. Instead, you need to use the structure_tag to define the member pointer of type itself.

    typedef struct 
    {
      ...
      my_struct* pS;
      ...        
    } my_struct;  // at this point compiler will know about *my_struct* type
                  // Hence, you can not use that name until after this line.
    
                  // To define the member pointer of type itself you need to 
                  // to use the struct_tag, as I did in your example.
                  // where, struct_tag is *edgenode_tag*
    

    EDIT:

    Also, malloc returns *void**, which you need to cast to the type you are assigning it to.
    So, inside function add_edges, make this correction (please read more about this in book, it is important to understand this):

                      p = (edgenode*)malloc(sizeof(edgenode));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Registry of intents protocols =) UPDATE:: Intents List: Invoking Google… May 15, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer IF ((SELECT count(*) FROM mytable WHERE userid = 'myid' AND… May 15, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer Yeah, because that's not valid HTML... So the code is… May 15, 2026 at 1:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.