Exemple #1
0
//BFS algorithm
int shortestpath(Node *array, int Vnum, int root, int des)
{
    int i,out,ss;
    //create a queue
    Queue *head=Initqueue();
    Queue *path=Initqueue();
    Node *travel,*p;
    int *color = (int *)malloc(Vnum*sizeof(int));
    int *father = (int *)malloc(Vnum*sizeof(int));
    for(i=0;i<Vnum;i++)father[i]=i;
    for(i=0;i<Vnum;i++)color[i]=white;  //initialize colar
    Enqueue(head,root);
    color[root]=grey;
    father[root]=root;
    while(Isempty(head)!=1)
    {
        out = Dequeue(head);
        travel=array+out;
        for(p=travel->next;p!=NULL;p=p->next)
        {
            if(color[p->ID]==white)
            {
                Enqueue(head,p->ID);
                color[p->ID]=grey;
                father[p->ID]=out;
            }
        }
        color[out]=black;
    }
    if(color[des]==white)
    {
        printf("Error: there is no path between %d and %d.",root,des);
        ss=false;
    }
    else
    {
        //if there is a path, then print it out
        //for(i=0;i<Vnum;i++)printf("%d is father of %d\n",father[i],i);
        PrintPath(father,root,des);
        ss=true;     
    }
    Clearqueue(head);
    Destroyqueue(head);
    free(color);
    free(father);
    return ss;
}
Exemple #2
0
int main()
{
    LinkQueue q;
    Initqueue(&q);        //取q的地址
    EnQueue(&q,'A');      //入队
    printf("%c\n",DeQueue(&q));     //出队并返回出队的值
    return 0;

}