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

  • SEARCH
  • Home
  • 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 9276077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:44:54+00:00 2026-06-18T16:44:54+00:00

In the function Dijkstra, I initialize a priority queue nperm, which has a info

  • 0

In the function Dijkstra, I initialize a priority queue nperm, which has a info field;that tells whether a node is a member of the set notpermanent or not.Then I have a ‘i’ field which actually stores the pointer (array index) to that graph node and a next pointer.Initially every node is a member of notpermanent set.This is How initialization is done:

for (i = graph; hnode[i].next >= 0;) 
{
    insertnperm(&nperm, 1, i);
    distance[i] = 500000;
    i = hnode[i].next;
}

Distance[i] is basically the distance of node i from the root node known so far,so as the algorithm proceeds , the distance is updated and so should the be order of the priority queue nperm,which is based of distance.
Now in function order,i pass a pointer to the priority queue’s pointer,and store that pointer’s value in another variable of the same type,notperm p; p = *pq;,since every node in the priority queue has a field ‘i’ which contains the index to the graph node, i traverse the list upto the node whose distance has been recently updated and remove and place that node in it’s proper place in the ascending priority queue.

I use the statement while ((p->i) != s) to traverse upto the node,but a segmentation fault arises ,of which i have no idea. It seems that a segmentation fault occurs when i access “i” using p->i.

Can anyone explain to me why does this occur?

This is the full code:

#include<stdio.h>
#include<conio.h>
#define MAXNODES 50

struct header 
{   
    int info;     
    int epoint;     
    int tpoint;     
    int next;     
};

struct header hnode[MAXNODES];     
int i = 0;

struct arcs 
{     
    int info;     
    int epoint;     
    int tpoint;     
    int enext;     
    int tnext;     
};     
int j = 0;     
struct arcs anode[MAXNODES]; 

struct node 
{     
    int info;     
    int i;     
    struct node *next;     
};     
typedef struct node *notperm;     

notperm npgetnode(void)
{     
    notperm p;     
    p = (notperm) (malloc(sizeof(struct node)));     
    return p;     
}          

int hgetnode()
{     
    int k;     
    k = i++;     
    return k;     
}   

int agetnode()
{     
    int k;     
    k = j++;     
    return k;     
}  

void hfreenode(int r)
{     
    --i;     
}     
void afreenode(int r)
{     
    --j;     
}

int addnode(int *pgraph, int x)
{     
    int p;     
    p = hgetnode();     
    hnode[p].info = x;     
    hnode[p].epoint = -1;     
    hnode[p].tpoint = -1;     
    hnode[p].next = *pgraph;     
    *pgraph = p;     
    return p;     
} 

void joinwt(int p, int q, int wt)
{    
    int r, r2;     
    int t, t2;     
    r2 = -1;     
    r = hnode[p].epoint; 

    while (r >= 0 && anode[r].epoint != q) 
    {     
        r2 = r;     
        r = anode[r].enext;     
    }        
    if (r >= 0) 
    {     
        anode[r].info = wt;     
    }         
    if (r < 0) 
    {    
        r = agetnode();     
        anode[r].epoint = q;     
        anode[r].tpoint = -1;     
        anode[r].enext = -1;     
        anode[r].tnext = -1;     
        anode[r].info = wt;     
        (r2 < 0) ? (hnode[p].epoint = r) : (anode[r2].enext = r);   
    }

    /*updation to q */

    t = hnode[q].tpoint;     
    t2 = -1;     
    while (t >= 0 && anode[r].tpoint != p) 
    {     
        t2 = t;     
        t = anode[t].tnext;     
    }     
    if (t >= 0) 
    {     
        anode[t].info = wt;     
    }   
    if (t < 0) 
    {     
        t = agetnode();     
        anode[t].tpoint = p;     
        anode[t].epoint = -1;   
        anode[t].tnext = -1;     
        anode[t].enext = -1;     
        anode[t].info = wt;     
        (t2 < 0) ? (hnode[q].tpoint = t) : (anode[t2].tnext = t);     
    }   
}

void insertnperm(notperm * pq, int x, int currep)
{     
    notperm p;     
    if (*pq == NULL)     
    {     
        *pq = (notperm) (malloc(sizeof(struct node)));     
        (*pq)->info = x;     
        (*pq)->i = currep;     
        (*pq)->next = NULL;     
        return;     
    }     
    p = npgetnode();     
    p->info = x;     
    p->i = currep;     
    p->next = (*pq);     
    *pq = p;     
}       

void order(notperm * pq, int s, int distance[])
{     
    notperm p, q, j;     
    q = NULL;     
    p = NULL;     
    p = *pq;     
    while ((p->i/*this statement always ends up with a segmentation fault*/) != s) 
    {     
        q = p;     
        p = p->next;     
    }     
    if (q == NULL)     
        *pq = p->next;     
    else     
        q->next = p->next;     
    q = NULL;     
    for (j = *pq; distance[(j->i)] < distance[(p->i)]; j = j->next)     
    q = j;     
    if (q == NULL) {     
        p->next = *pq;     
        *pq = p;     
    }     
    else     
    {     
        p->next = q->next;     
        q->next = p;     
    }     
}  

void chngstozero(notperm * pq, int s)
{     
    notperm p;     
    for (p = *pq; (p->i) != s; p = p->next);    
         p->info = 0;     
    }     
    int checkstat(notperm * pq, int s)
    {     
        notperm p;   
        for (p = *pq; (p->i) != s; p = p->next);     
        return (p->info);     
    }          
    int newminel(notperm * pq)
    {     
        notperm p;     
        for (p = *pq; (p->info) != 1; p = p->next);     
        p->info = 0;     
        return (p->i);     
    }        

    void Dijkstra(int graph, int s, int t, int *pd)
    {     
        unsigned int distance[MAXNODES], precede[MAXNODES];     
        int current, i, k, dc, currep;     
        unsigned int smalldist, newdist;     
        notperm p;     
        notperm nperm;    
        nperm = NULL;
        for (i = graph; hnode[i].next >= 0;) 
        {    
            insertnperm(&nperm, 1, i);    
            distance[i] = 500000;    
            i = hnode[i].next;
        }     
        distance[s] =0; 
        order(&nperm, s, distance);    
        chngstozero(&nperm, s);
        current = s;
        while (current != t) 
        {
            smalldist = 500000;
            dc = distance[current];
            for (currep = hnode[current].epoint; anode[currep].enext >= 0; currep = anode[currep].enext) 
            {
                if (checkstat(&nperm, currep)) 
                {
                    newdist = dc + anode[currep].info;
                    if (newdist < distance[currep]) 
                    {
                        distance[currep] = newdist;
                        order(&nperm, currep, distance);
                        precede[currep] = current;
                    }
                }
            }

main()
{
    int graph,a,b,c,d,e,wt,r,t,pd;
    graph=-1;
    int wt1=5;
    int wt2=3;
    int wt3=6;
    int wt4=2;
    int wt5=7;
    a=addnode(&graph,4);
    b=addnode(&graph,6);
    c=addnode(&graph,8);
    d=addnode(&graph,9);
    e=addnode(&graph,5);
    joinwt(a,b,wt);
    joinwt(a,c,wt1);
    joinwt(a,d,wt2);
    joinwt(a,e,wt3);
    joinwt(d,b,wt4);
    joinwt(c,e,wt5); 
    Dijkstra(graph,a,e,&pd);
    printf("%d",pd);

    getch();
    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-06-18T16:44:56+00:00Added an answer on June 18, 2026 at 4:44 pm
    while ((p->i/*this statement always ends up with a segmentation fault*/) != s) {
        q = p;
        p = p->next;
    }
    

    It looks like you’re running off the end of the list pointed to by p. The last element will have a NULL for p->next; you then set p = p->next and try to dereference the NULL p pointer, causing the segmentation fault.

    The solution is to check for a NULL p as well before checking against p->i.

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

Sidebar

Related Questions

I have a program that has to find the shortest path (Dijkstra's algorithm), and
I'm new to ruby and I'm trying to write a dijkstra function but my
I'm writing an application that uses Dijkstra algorithm to find minimal paths in the
Im making an map with different markers which should have different info windows. But
The pseudocode as taken from Wikipedia: function Dijkstra(Graph, source): 2 for each vertex v
function move() { pos = pos+1; t = setTimeout(move, 100); } Can that be
function filterRows(statusName) { $(#mainTable tbody tr.+statusName).hide(); } It hides the rows that have the
I am having an error that I don't understand in my Dijkstra Algorithm code
function A(int a[]) { SemLock() //Some Code.... SemUnlock() } Suppose some other thread has
Dijkstra(G,w,s) { ISS(G,s); let S be an empty set let Q be a priority

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.