在函數Dijkstra中,我初始化一個優先級隊列nperm,它具有一個信息字段;它告訴節點是否是非永久性的集合的成員。我有一個的「i」的字段,它實際存儲指針(數組索引),以該圖形節點和下一pointer.Initially每個節點是notpermanent set.This的構件是如何初始化完成:試圖實現Dijkstra,但有分段錯誤沒有意義
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
訪問「我」時,似乎發生分段錯誤。
任何人都可以向我解釋爲什麼會發生這種情況?
這是全碼:
#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;
}
你能解決格式嗎?縮進代碼並擺脫所有的雙間距? – Barmar
@JoachimPileborg謝謝,但我已經知道了。那我怎麼知道分段錯誤在哪裏。 – 10111