关闭

图的邻接表操作

标签: graphnullstructfloatvc++input
1333人阅读 评论(0) 收藏 举报
分类:

图的邻接表操作,可以进行基本的创建、深度遍历等……也有广度遍历程序,有待提高,有兴趣的你可以试一下…………

/*win-tc
如果你是用VC++的请你去掉getch()和clrscr()涵数*/
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"malloc.h"
#define NULL 0
#define VertexNum 5     /*用户自定义顶点的大小*/

int visited[VertexNum];

typedef int VertexType;
typedef enum{ERROR=0,OK=1}Status;
typedef struct aduNode
{
    int vertexPos;
    float weight;
    struct adjNode *next;
}AdjNode,*pAdjList;

/**/
typedef struct VertexNode
{
    VertexType vertex;
    pAdjList firstAdj;
}VertexNode,Graph;

/*插入边*/
void InsertEdge(Graph *graph,int flag,VertexType source,VertexType destination,float weight)
{
    int sourcePos,desPos;
    AdjNode* newnode;
    AdjNode *pvertex=NULL,*p=NULL;
    /*求与边关联的两个顶点在顶点表中的位置*/
    sourcePos=GetPos(graph,source);
    desPos=GetPos(graph,destination);
    if(sourcePos==-1||desPos==-1)
    {
        printf("/n***Erroe***:Out of range!/n");
        return;
    }
    else
    {
        newnode=(AdjNode*)malloc(sizeof(AdjNode));
        newnode->vertexPos=desPos;
        newnode->weight=weight;
        newnode->next=NULL;
        pvertex=graph[sourcePos].firstAdj;
        /*生成顶点的第一个邻接点*/
        if(pvertex==NULL)
            graph[sourcePos].firstAdj=newnode;
        else
        {
            /*生成顶点的其他邻接点*/
            while(pvertex!=NULL)
            {
                p=pvertex;
                pvertex=pvertex->next;
            }
            p->next=newnode;
        }
        /*无向图插入边(a,b)的同时也插入边(b,c)*/
        if(flag==1)
        {
            newnode=(AdjNode*)malloc(sizeof(AdjNode));
            newnode->vertexPos=sourcePos;
            newnode->next=NULL;
            pvertex=graph[desPos].firstAdj;
            /*生成顶点的第一个邻接点*/
            if(pvertex==NULL)
                graph[desPos].firstAdj=newnode;
            else
            {
                /*生成顶点的其他邻接点*/
                while(pvertex!=NULL)
                {
                    p=pvertex;
                    pvertex=pvertex->next;
                }
                p->next=newnode;
            }
        }
    }
}


/*图的生成*/
void create_graph(Graph* graph,int flag)
{
    VertexType vertex;
    VertexType vertex1,vertex2;
    int num=VertexNum;
    int i;
    float weight=0;

    clrscr();         /*清屏涵数*/
    printf("/nTotal number of vertex is %d (0....%d).",num,num-1);
    printf("/nplease input the vertex:");
    /*输入顶点,生成顶点表*/
    for(i=0;i<VertexNum;i++)
    {
        scanf("%d",&vertex);
        graph[i].vertex=vertex;
        graph[i].firstAdj=NULL;
    }

    printf("/nplease input the edge(s),-1,-1 to finish!/n");
    /*输入边,生成邻接表*/
    while(1)
    {
        scanf("%d,%d",&vertex1,&vertex2);
        if(vertex1==-1||vertex2==-1)
            break;
        InsertEdge(graph,flag,vertex1,vertex2,weight);
    }
}

/*顶点定位*/
int GetPos(Graph* graph,VertexType vertex)
{
    int i;
    for(i=0;i<VertexNum;i++)
    {
        if(graph[i].vertex==vertex)
            return i;
    }
    return -1;
}

/*取顶点*/
Status GetVertex(Graph* graph,int pos,VertexType* vertex)
{
    if(pos>=VertexNum)
        return ERROR;
    *vertex=graph[pos].vertex;
    return OK;
}

/*求第一个邻接点*/
Status FirstAdj(Graph* graph,VertexType vertex,VertexType* firstAdj)
{
    int pos;
    AdjNode *pvertex=NULL;
    pos=GetPos(graph,vertex);
    if(pos==-1)
        return ERROR;
    pvertex=graph[pos].firstAdj;
    if(pvertex!=NULL)
    {
        return (GetVertex(graph,pvertex->vertexPos,firstAdj));
    }
    return ERROR;
}

/*求下一个邻接点*/
Status NextAdj(Graph* graph,VertexType vertex,VertexType adj,VertexType* nextAdj)
{
    int vertexPos;
    AdjNode *pvertex=NULL;
    VertexType adjNode;
    Status GetVertex(Graph* graph,int pos,VertexType* vertex);

    vertexPos=GetPos(graph,vertex);

    pvertex=graph[vertexPos].firstAdj;
    while(pvertex!=NULL)
    {
        GetVertex(graph,pvertex->vertexPos,&adjNode);
        if(adjNode==adj&& pvertex->next!=NULL)
        {
            vertexPos=pvertex->next->vertexPos;
            return (GetVertex(graph,vertexPos,nextAdj));
        }
        pvertex=pvertex->next;
    }
    return ERROR;
}

/*图的输出*/
void print_graph(Graph* graph)
{
    int i;
    AdjNode *pvertex=NULL;
    VertexType adj;
    printf("/nGraph's adjcency list is:");
    for(i=0;i<VertexNum;i++)
    {
        printf("/nVertex[%d]:%d-->",i,graph[i].vertex);
        pvertex=graph[i].firstAdj;
        while(pvertex!=NULL)
        {
            GetVertex(graph,pvertex->vertexPos,&adj);
            printf("%d  ",adj);
            pvertex=pvertex->next;

        }
    }
}


/*释放图*/
void free_graph(Graph* graph)
{
    AdjNode *pvertex=NULL,*p=NULL;
    int i;
    for(i=0;i<VertexNum;i++)
    {
        pvertex=graph[i].firstAdj;
        while(pvertex!=NULL)
        {
            p=pvertex;
            pvertex=pvertex->next;
            free(p);
            p=NULL;
        }
    }
}


/*深度优先遍历*/
void dfs(Graph* graph,VertexType vertex)
{
    AdjNode* pointer;
    int pos;
    VertexType v;
    VertexType adjVertex,nextAdj;
    Status s;

    pos=GetPos(graph,vertex);
    visited[pos]=1;
    /*输出顶点*/
    printf("%d  ",vertex);
    s=FirstAdj(graph,vertex,&adjVertex);
    while(s!=ERROR)
    {
        pos=GetPos(graph,adjVertex);
        if(visited[pos]==0)
            /*递归调用,深度优先搜索邻接点*/
            dfs(graph,adjVertex);
        s=NextAdj(graph,vertex,adjVertex,&nextAdj);
        adjVertex=nextAdj;
    }
}

/*         要完成它还需要添加队列的相关涵数,如入队和出队,以及要给队列做定义方可完成功能。
           有兴趣的你可以做一下        
/ 广度优先遍历/
void bfs(Graph *graph,int vertex)
{
    AdjNode* pointer;
    int pos;
    VertexType v;
    VertexType adjVertex,nextAdj;
    Status s;
    / 搜索起始点入队 /
    EnQueue(vertex);
    pos=GetPos(graph,vertex);
    / 置入队标志/
    visited[pos]=1;
    / 判断队列是否为空/
    while(front!=rear)
    {
        vertex=DeQueue();
        / 输出顶点/
        printf("%d",vertex);
        s=FirstAdj(graph,vertex,&adjVertex);
        while(s!=ERROR)
        {
            pos=GetPos(graph,adjVertex);
            / 如果该邻接点没有入队,将其入队/
            if(visited[pos]==0)
            {
                EnQueue(adjVertex);
                visited[pos]=1;
            }
            s=NextAdj(graph,vertex,adjVertex,&nextAdj);
            adjVertex=nextAdj;
        }
    }
}
*/

/**/
void main()
{
    int i;
    Graph graph[VertexNum];

    /*生成图*/
    create_graph(graph,1);
    print_graph(graph);
    /*记录顶点是否被访问过*/
    for(i=0;i<VertexNum;i++)
        visited[i]=0;
    /*深度优先搜索*/
    printf("/nDepth-first-search:/n");
    for(i=0;i<VertexNum;i++)
    {
        if(visited[i]==0)
            dfs(graph,graph[i].vertex);
    }
    free_graph(graph);
    getch();             /*让屏幕停顿涵数*/
}

结果如下:

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29959次
    • 积分:546
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:0篇
    • 译文:0篇
    • 评论:9条
    文章分类
    文章存档
    最新评论