俞晨曦 发表于 2017-6-21 19:12:03

一些基础算法

#include <STDIO.H>
int visited = {0};

int graph =
{
        0,1,1,0,0,
        1,0,0,1,1,
        0,0,0,0,1,
        0,1,0,0,1,
        0,1,1,1,0
};

void DFS(int x)
{
        printf("%d\n",x);
        visited = 1;
        int y;
       
        for(y = 0;y < 5;++y)
                if(graph && !visited)
                        DFS(y);
}

#define MAX_SIZE 0x40

typedef struct
{
        int queue;
        int front,rear;
}Queue,*PQueue;

void InitQueue(PQueue PQ)
{
        PQ->rear = PQ->front = 0;
}

void InQueue(PQueue PQ,int data)
{
        PQ->queue = data;
        PQ->rear = (PQ->rear + 1)%MAX_SIZE;
}

int OutQueue(PQueue PQ)
{
        int data = PQ->queue;
        PQ->front = (PQ->front + 1)%MAX_SIZE;
        return data;
}

int IsQueueEmpty(PQueue PQ)
{
        if(PQ->rear == PQ->front)
                return 1;
        return 0;
}

void BFS(int x)
{
        Queue q;
        int i;
       
        InitQueue(&q);
       
        InQueue(&q,x);
        visited = 1;               

        while(!IsQueueEmpty(&q))
        {
                x = OutQueue(&q);
                printf("%d\n",x);
       
                for (i = 0;i < 5;++i)
                {
                        if(!visited && graph)
                        {
                                InQueue(&q,i);
                                visited = 1;        //标记访问
                        }
                }
               
        }
}

int main()
{
        return 0;
}

知表不言 发表于 2017-7-8 23:43:00

{:10_254:}{:10_257:}{:10_256:}
页: [1]
查看完整版本: 一些基础算法