C 试图实现Dijkstra,但存在毫无意义的分段错误

C 试图实现Dijkstra,但存在毫无意义的分段错误,c,linked-list,segmentation-fault,dijkstra,C,Linked List,Segmentation Fault,Dijkstra,在函数Dijkstra中,我初始化了一个优先级队列nperm,它有一个info字段;这表明一个节点是否为集合notpermanent的成员。然后我有一个“I”字段,它实际存储指向该图形节点的指针(数组索引)和下一个指针。最初每个节点都是notpermanent集合的成员。初始化过程如下: for (i = graph; hnode[i].next >= 0;) { insertnperm(&nperm, 1, i); distance[i] = 500000;

在函数Dijkstra中,我初始化了一个优先级队列nperm,它有一个info字段;这表明一个节点是否为集合notpermanent的成员。然后我有一个“I”字段,它实际存储指向该图形节点的指针(数组索引)和下一个指针。最初每个节点都是notpermanent集合的成员。初始化过程如下:

for (i = graph; hnode[i].next >= 0;) 
{
    insertnperm(&nperm, 1, i);
    distance[i] = 500000;
    i = hnode[i].next;
}
距离[i]基本上是节点i与到目前为止已知的根节点之间的距离,因此,随着算法的进行,距离会更新,优先级队列nperm的顺序也会更新,这是基于距离的。 现在按照函数顺序,我传递一个指向优先级队列指针的指针,并将该指针的值存储在另一个相同类型的变量中,
notperm p;p=*pq,因为优先级队列中的每个节点都有一个字段“i”,其中包含图形节点的索引,所以我将列表遍历到距离最近更新的节点,并移除该节点,并将其放置在提升优先级队列中的适当位置

我使用语句
while((p->I)!=s)
遍历到节点,但是出现了一个分段错误,对此我一无所知。当我使用
p->i
访问“i”时,似乎出现了分段错误

谁能解释一下为什么会发生这种情况

这是完整的代码:

#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;
}
#包括
#包括
#定义最大节点50
结构头
{   
国际信息;
点-点;
内点;
int-next;
};
结构头hnode[MAXNODES];
int i=0;
结构弧
{     
国际信息;
点-点;
内点;
int-enext;
int tnext;
};     
int j=0;
结构弧阳极[MAXNODES];
结构节点
{     
国际信息;
int i;
结构节点*下一步;
};     
typedef结构节点*notperm;
notperm npgetnode(无效)
{     
诺珀姆p;
p=(notperm)(malloc(sizeof(struct node));
返回p;
}          
int hgetnode()
{     
int k;
k=i++;
返回k;
}   
int agetnode()
{     
int k;
k=j++;
返回k;
}  
void hfreenode(int r)
{     
--一,;
}     
void afreenode(int r)
{     
--j;
}
int addnode(int*pgraph,int x)
{     
INTP;
p=hgetnode();
hnode[p].info=x;
hnode[p].epoint=-1;
hnode[p].tpoint=-1;
hnode[p].next=*pgraph;
*pgraph=p;
返回p;
} 
void joinwt(int p,int q,int wt)
{    
int r,r2;
int t,t2;
r2=-1;
r=hnode[p].epoint;
while(r>=0&&阳极[r].epoint!=q)
{     
r2=r;
r=阳极[r].enext;
}        
如果(r>=0)
{     
阳极[r].info=wt;
}         
if(r<0)
{    
r=agetnode();
阳极[r].epoint=q;
阳极[r].t点=-1;
阳极[r].enext=-1;
阳极[r].tnext=-1;
阳极[r].info=wt;
(r2<0)?(hnode[p].epoint=r):(阳极[r2].enext=r);
}
/*更新到q*/
t=hnode[q].t点;
t2=-1;
而(t>=0&&r.t点!=p)
{     
t2=t;
t=阳极[t].t下一步;
}     
如果(t>=0)
{     
阳极[t].info=wt;
}   
if(t<0)
{     
t=agetnode();
阳极[t].t点=p;
阳极[t].epoint=-1;
阳极[t].tnext=-1;
阳极[t].enext=-1;
阳极[t].info=wt;
(t2<0)?(hnode[q].tpoint=t):(阳极[t2].tnext=t);
}   
}
无效插入项(notperm*pq、int x、int currep)
{     
诺珀姆p;
如果(*pq==NULL)
{     
*pq=(notperm)(malloc(sizeof(struct node));
(*pq)->info=x;
(*pq)->i=电流;
(*pq)->next=NULL;
返回;
}     
p=npgetnode();
p->info=x;
p->i=电流;
p->next=(*pq);
*pq=p;
}       
无效顺序(notperm*pq,整数s,整数距离[])
{     
非perm p,q,j;
q=零;
p=零;
p=*pq;
而((p->i/*此语句总是以分段错误结束*/)!=s)
{     
q=p;
p=p->next;
}     
if(q==NULL)
*pq=p->next;
其他的
q->next=p->next;
q=零;
对于(j=*pq;距离[(j->i)]i)];j=j->next)
q=j;
如果(q==NULL){
p->next=*pq;
*pq=p;
}     
其他的
{     
p->next=q->next;
q->next=p;
}     
}  
void chngstozero(notperm*pq,int s)
{     
诺珀姆p;
对于(p=*pq;(p->i)!=s;p=p->next);
p->info=0;
}     
int checkstat(notperm*pq,int s)
{     
诺珀姆p;
对于(p=*pq;(p->i)!=s;p=p->next);
返回(p->info);
}          
int newminel(非永久性*pq)
{     
诺珀姆p;
对于(p=*pq;(p->info)!=1;p=p->next);
p->info=0;
返回(p->i);
}        
void Dijkstra(int图,int s,int t,int*pd)
{     
无符号整数距离[MAXNODES],在[MAXNODES]之前;
int电流,i,k,dc,currep;
无符号整数smalldist,newdist;
诺珀姆p;
非永久性nperm;
nperm=NULL;
对于(i=graph;hnode[i]。下一步>=0;)
{    
插入nperm(&nperm,1,i);
距离[i]=500000;
i=hnode[i]。下一步;
}     
距离[s]=0;
订单(nperm、s、距离(&N);
chngstozero&nperm,s;
电流=s;
while(当前!=t)
{
while ((p->i/*this statement always ends up with a segmentation fault*/) != s) {
    q = p;
    p = p->next;
}