C-我已经使用堆栈和相邻列表编写了用于图形的DFS,它的工作原理不如应有


typedef struct nodeTAG{
    int data;
    struct nodeTAG *next;
}nodeT;
typedef struct listTAG{
    nodeT *front;
    nodeT *end;
}listT;

void listinit (listT *plist)
{
    plist -> front = NULL;
    plist -> end = NULL;
}
int isempty (listT *plist)
{
    if (plist -> front == NULL)
        return 1;
    else
        return 0;
}
void addfront (listT *plist, int x)
{
    nodeT *temp;
    temp = (nodeT*)malloc(sizeof(nodeT));
    temp -> data =x;
    if (plist -> front == NULL)
    {
        temp -> next = NULL;
        plist -> front = temp;
        plist -> end = temp;
    }
    else
    {
        temp -> next = plist -> front;
        plist -> front = temp;
    }
}
void removefront (listT *plist, int *x)
{
    if (isempty(plist) == 0)
    {
        nodeT *temp;
        *x = plist -> front -> data;
        temp = plist -> front;
        plist -> front = temp -> next;
        free(temp);
    }
}
void DFS(int wierz){
    int v;
    nodeT *p;
    addfront(&Stos, wierz);//adding element on stack
    tabzaznaczen[wierz]==1;//array of visited elements
    while(Stos){
        removefront(&Stos, &v);
        printf("%d", v);
        for(p = tabwierz[v].front; p; p = p->next){//tabwierz is array of pointers to adjecent lists
            if(tabzaznaczen[p->data]==0){
                addfront(&Stos, p->data);
                tabzaznaczen[p->data] = 1;
            }
    }
}}

我在堆栈上使用了Lisinit并将其宣布为全局。

它不像应有的那样工作,添加了一些无用的整数。这是我上学的项目。因此,这对我来说很重要,我认为这就像一个小错误,但我看不到它。

好吧,我修复了它。我用过==而不是在一个地方。

最新更新