第12周、项目2—操作用邻接表存储的图

问题及代码:

(1)测试函数:main.cpp,完成相关的测试工作;

/*
 *Copyright(c) 2015,烟台大学计算机与控制工程学院
 *All rights reserved.
 *文件名称:test.cpp
 *作    者:焦梦真
 *完成日期:2015年11月16日
 *版 本 号;v1.0
 *
 *问题描述:假设图G采用邻接表存储,分别设计实现以下要求的算法: 
           (1)输出出图G中每个顶点的出度; 
           (2)求出图G中出度最大的一个顶点,输出该顶点编号; 
           (3)计算图G中出度为0的顶点数; 
           (4)判断图G中是否存在边<i,j> 。 
            利用下图作为测试用图,输出结果。 
<img src="https://img-blog.csdn.net/20151116164015972?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 *输入描述:
 *程序输出:
 */
#include <stdio.h>
#include <malloc.h>
#include "graph.h"

//返回图G中编号为v的顶点的出度
int OutDegree(ALGraph *G,int v)
{
    ArcNode *p;
    int n=0;
    p=G->adjlist[v].firstarc;
    while (p!=NULL)
    {
        n++;
        p=p->nextarc;
    }
    return n;
}

//输出图G中每个顶点的出度
void OutDs(ALGraph *G)
{
    int i;
    for (i=0; i<G->n; i++)
        printf("  顶点%d:%d\n",i,OutDegree(G,i));
}

//输出图G中出度最大的一个顶点
void OutMaxDs(ALGraph *G)
{
    int maxv=0,maxds=0,i,x;
    for (i=0; i<G->n; i++)
    {
        x=OutDegree(G,i);
        if (x>maxds)
        {
            maxds=x;
            maxv=i;
        }
    }
    printf("顶点%d,出度=%d\n",maxv,maxds);
}
//输出图G中出度为0的顶点数
void ZeroDs(ALGraph *G)
{
    int i,x;
    for (i=0; i<G->n; i++)
    {
        x=OutDegree(G,i);
        if (x==0)
            printf("%2d",i);
    }
    printf("\n");
}

//返回图G中是否存在边<i,j>
bool Arc(ALGraph *G, int i,int j)
{
    ArcNode *p;
    bool found = false;
    p=G->adjlist[i].firstarc;
    while (p!=NULL)
    {
        if(p->adjvex==j)
        {
            found = true;
            break;
        }
        p=p->nextarc;
    }
    return found;
}

int main()
{
    ALGraph *G;
    int A[7][7]=
    {
        {0,1,1,1,0,0,0},
        {0,0,0,0,1,0,0},
        {0,0,0,0,1,1,0},
        {0,0,0,0,0,0,1},
        {0,0,0,0,0,0,0},
        {0,0,0,1,1,0,1},
        {0,1,0,0,0,0,0}
    };
    ArrayToList(A[0], 7, G);
    printf("(1)各顶点出度:\n");
    OutDs(G);
    printf("(2)最大出度的顶点信息:");
    OutMaxDs(G);
    printf("(3)出度为0的顶点:");
    ZeroDs(G);
    printf("(4)边<2,6>存在吗?");
    if(Arc(G,2,6))
        printf("是\n");
    else
        printf("否\n");
    printf("\n");
    return 0;
}
(2)头文件:graph.h,包含定义图数据结构的代码、宏定义、要实现算法的函数的声明;
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED

#define MAXV 100                //最大顶点个数
#define INF 32767       //INF表示∞
typedef int InfoType;

//以下定义邻接矩阵类型
typedef struct
{
    int no;                     //顶点编号
    InfoType info;              //顶点其他信息,在此存放带权图权值
} VertexType;                   //顶点类型

typedef struct                  //图的定义
{
    int edges[MAXV][MAXV];      //邻接矩阵
    int n,e;                    //顶点数,弧数
    VertexType vexs[MAXV];      //存放顶点信息
} MGraph;                       //图的邻接矩阵类型

//以下定义邻接表类型
typedef struct ANode            //弧的结点结构类型
{
    int adjvex;                 //该弧的终点位置
    struct ANode *nextarc;      //指向下一条弧的指针
    InfoType info;              //该弧的相关信息,这里用于存放权值
} ArcNode;

typedef int Vertex;

typedef struct Vnode            //邻接表头结点的类型
{
    Vertex data;                //顶点信息
    int count;                  //存放顶点入度,只在拓扑排序中用
    ArcNode *firstarc;          //指向第一条弧
} VNode;

typedef VNode AdjList[MAXV];    //AdjList是邻接表类型

typedef struct
{
    AdjList adjlist;            //邻接表
    int n,e;                    //图中顶点数n和边数e
} ALGraph;                      //图的邻接表类型

//功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图
//参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)
//      n - 矩阵的阶数
//      g - 要构造出来的邻接矩阵数据结构
void ArrayToMat(int *Arr, int n, MGraph &g); //用普通数组构造图的邻接矩阵
void ArrayToList(int *Arr, int n, ALGraph *&); //用普通数组构造图的邻接表
void MatToList(MGraph g,ALGraph *&G);//将邻接矩阵g转换成邻接表G
void ListToMat(ALGraph *G,MGraph &g);//将邻接表G转换成邻接矩阵g
void DispMat(MGraph g);//输出邻接矩阵g
void DispAdj(ALGraph *G);//输出邻接表G

#endif // GRAPH_H_INCLUDED

(3)源文件:graph.cpp,包含实现各种算法的函数的定义;
#include <stdio.h>
#include <malloc.h>
#include "graph.h"

//功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图
//参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)
//      n - 矩阵的阶数
//      g - 要构造出来的邻接矩阵数据结构
void ArrayToMat(int *Arr, int n, MGraph &g)
{
    int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数
    g.n=n;
    for (i=0; i<g.n; i++)
        for (j=0; j<g.n; j++)
        {
            g.edges[i][j]=Arr[i*n+j]; //将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j],计算存储位置的功夫在此应用
            if(g.edges[i][j]!=0)
                count++;
        }
    g.e=count;
}

void ArrayToList(int *Arr, int n, ALGraph *&G)
{
    int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数
    ArcNode *p;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    G->n=n;
    for (i=0; i<n; i++)                 //给邻接表中所有头节点的指针域置初值
        G->adjlist[i].firstarc=NULL;
    for (i=0; i<n; i++)                 //检查邻接矩阵中每个元素
        for (j=n-1; j>=0; j--)
            if (Arr[i*n+j]!=0)      //存在一条边,将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j]
            {
                p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p
                p->adjvex=j;
                p->info=Arr[i*n+j];
                p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p
                G->adjlist[i].firstarc=p;
            }

    G->e=count;
}

void MatToList(MGraph g, ALGraph *&G)
//将邻接矩阵g转换成邻接表G
{
    int i,j;
    ArcNode *p;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    for (i=0; i<g.n; i++)                   //给邻接表中所有头节点的指针域置初值
        G->adjlist[i].firstarc=NULL;
    for (i=0; i<g.n; i++)                   //检查邻接矩阵中每个元素
        for (j=g.n-1; j>=0; j--)
            if (g.edges[i][j]!=0)       //存在一条边
            {
                p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p
                p->adjvex=j;
                p->info=g.edges[i][j];
                p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p
                G->adjlist[i].firstarc=p;
            }
    G->n=g.n;
    G->e=g.e;
}

void ListToMat(ALGraph *G,MGraph &g)
//将邻接表G转换成邻接矩阵g
{
    int i,j;
    ArcNode *p;
    for (i=0; i<g.n; i++)   //先初始化邻接矩阵
        for (j=0; j<g.n; j++)
            g.edges[i][j]=0;
    for (i=0; i<G->n; i++)  //根据邻接表,为邻接矩阵赋值
    {
        p=G->adjlist[i].firstarc;
        while (p!=NULL)
        {
            g.edges[i][p->adjvex]=p->info;
            p=p->nextarc;
        }
    }
    g.n=G->n;
    g.e=G->e;
}

void DispMat(MGraph g)
//输出邻接矩阵g
{
    int i,j;
    for (i=0; i<g.n; i++)
    {
        for (j=0; j<g.n; j++)
            if (g.edges[i][j]==INF)
                printf("%3s","∞");
            else
                printf("%3d",g.edges[i][j]);
        printf("\n");
    }
}

void DispAdj(ALGraph *G)
//输出邻接表G
{
    int i;
    ArcNode *p;
    for (i=0; i<G->n; i++)
    {
        p=G->adjlist[i].firstarc;
        printf("%3d: ",i);
        while (p!=NULL)
        {
            printf("-->%d/%d ",p->adjvex,p->info);
            p=p->nextarc;
        }
        printf("\n");
    }
}

运行结果:
<img src="https://img-blog.csdn.net/20151116164031559?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "graphics.h" #include "stdio.h" #include "dos.h" #include "conio.h" #include "bios.h" #include "stdlib.h" #include "math.h" #ifdef __cplusplus #define __CPPARGS ... #else #define __CPPARGS #endif #define VK_LEFT 0x4b00 #define VK_RIGHT 0x4d00 #define VK_DOWN 0x5000 #define VK_UP 0x4800 #define VK_HOME 0x4700 #define VK_END 0x4f00 #define VK_SPACE 0x3920 #define VK_ESC 0x011b #define VK_ENTER 0x1c0d #define VK_PGUP 0x4900 #define VK_PGDOWN 0x5100 #define TIMER 0x1c void drawblock(int,int,int); void nedr(void); void fk(int,int,int,int); void rewr(int,int,int,int); void rrwf(int,int); int is_bd(int,int,int); int is_line(void); void interrupt ( *oldhandler)(__CPPARGS); void KillTimer(void); void SetTimer(void interrupt (*IntProc)(__CPPARGS)); void rscore(int); int fkt[]={0,4,8,12,14,16,18}; int cj[]={0,100,300,900,1500}; long grc[]={0,3000,60000,90000,120000,150000,180000,210000,240000,270000}; int cjb[]={0,0,0,0,0,0,0,0,0,0}; char cjc[]="score: 0"; char grad[]="grade:0"; struct shape { short xy[8]; short color; short next; }; struct shape shapes[19]= { /* {x1,y1,x2,y3,x3,y3,x4,y4,color,next} */ {1,1,2,1,3,1,3,2,LIGHTBLUE,1}, /* □□□□ */ {2,2,3,0,3,1,3,2,LIGHTBLUE,1}, /* □■□□ */ {1,1,1,2,2,2,3,2,LIGHTBLUE,1}, /* □■□□ */ {2,0,2,1,2,2,3,0,LIGHTBLUE,-3}, /* □■■□ */ {1,1,1,2,2,1,3,1,LIGHTCYAN,1}, /* □□□□ */ {2,0,3,0,3,1,3,2,LIGHTCYAN,1}, /* □■■□ */ {1,2,2,2,3,1,3,2,LIGHTCYAN,1}, /* □■□□ */ {2,0,2,1,2,2,3,2,LIGHTCYAN,-3}, /* □■□□ */ {2,1,3,0,3,1,3,2,LIGHTRED,1}, /* □□□□ */ {1,2,2,1,2,2,3,2,LIGHTRED,1}, /* □□□□ */ {1,0,1,1,1,2,2,1,LIGHTRED,1}, /* □■□□ */ {1,0,2,0,2,1,3,0,LIGHTRED,-3}, /* ■■■□ */ {2,1,2,2,3,0,3,1,DARKGRAY,1}, /* □□□□ */ {1,0,2,0,2,1,3,1,DARKGRAY,-1}, /* □□□□ */ /* □■■□ */ /* ■■□□ */ {2,0,2,1,3,1,3,2,MAGENTA,1}, /* □□□□ */ {1,2,2,1,2,2,3,1,MAGENTA,-1}, /* □□□□ */ /* ■■□□ */ /* □■■□ */ {0,1,1,1,2,1,3,1,BROWN,1}, /* □■□□ */ {1,0,1,1,1,2,1,3,BROWN,-1}, /* □■□□ */ /* □■□□ */ /* □■□□ */ {2,1,2,2,3,1,3,2,YELLOW,0}, /* □□□□ */ /* □□□□ */ /* □■■□ */ /* □■■□ */ }; int stx=5,sty=8,sdx=24,sdy=18,shx=7,shy=22,adx=4,ady=1,nexti,TimerCounter=0,grade=0,end=0; unsigned long score=0; short board[25][12]= /* 方块空间表示,1表示有方块 */ { {1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, /* 0 */ {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, /* 5 */ {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0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1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1}, /* 20 */ }; void interrupt newhandler(__CPPARGS) /* 新的时钟中断处理函数 */ { TimerCounter++; oldhandler(); } void SetTimer(void interrupt (*IntProc)(__CPPARGS)) /* 设置新的时钟中断 */ { oldhandler=getvect(TIMER); disable(); setvect(TIMER,IntProc); enable(); } void KillTimer() /* 恢复原有的时钟中断处理过程 */ { disable(); setvect(TIMER,oldhandler); enable(); } void rscore(int line) /* 成绩更新函数 */ { int i,j,k=1; unsigned long s,f; score+=cj[line]; setfillstyle(1,GREEN); bar(0,5,150,30); s=score; if(s>=grc[grade]) { grade++; grad[6]=grade+48; bar(180,5,300,30); moveto(200,20); outtext(grad); } for(i=9;i>0;i--) { f=pow(10,i); j=s/f;s=s%f; if((!j)&&k); else k=0,cjc[15-i]=j+48; } moveto(10,20); outtext(cjc); } int is_bd(int x1,int y1,int i) /* 判断是否有方块 */ { int j,x2,y2; for(j=0;j<4;j++) { x2=shapes[i].xy[2*j]; y2=shapes[i].xy[2*j+1]; if(board[x1+x2+adx][y1+y2+ady])return 0; } return 1; } void fk(int re,int x1,int y1,int i) /* 方块的擦除、重写,并判断是否结束 有问题 */ { int j,x2,y2,color; for(j=0;j<4;j++) { x2=shapes[i].xy[2*j]; y2=shapes[i].xy[2*j+1]; if(!re) color=getbkcolor(); else color=shapes[i].color; if(re==2) { if((x1+x2)<=-1) { printf("Game is OVER!"); getch(); end=1; } break; } else { if((x1+x2)<=-1) continue; } board[x1+x2+adx][y1+y2+ady]=re; colable[x1+x2+adx][y1+y2+ady]=color; drawblock(stx+x1+x2,sty+y1+y2,color); } /* getch(); */ } void drawblock(int x,int y,int color) /* 写1个相对单位点的函数 */ { int i,j; for(i=0;i<16;i++) for(j=0;j<16;j++) putpixel(y*16+j,x*16+i,color); } void nedr(void) /* 产生并写下一个方块的函数 */ { int j,x2,y2,i,color; setfillstyle(1,GREEN); nexti=i=random(19); bar(shy*16,shx*16,(shy+4)*16,(shx+4)*16); for(j=0;j4;i--) { for(j=1;j4;i--) for(j=1;j<12;j++) colable[i][j]=colable[i-k][j],board[i][j]=board[i-k][j]; setfillstyle(1,BLUE); bar(sty*16,stx*16,(sdy)*16-1,(sdx+1)*16-1); for(i=5;i<24;i++) for(j=1;j9)) { if(TimerCounter>9)TimerCounter=0; fk(0,x1,y1,i); if(is_bd((k=x1+1),y1,i)) fk(re,x1=k,y1,i); else { fk(re,x1,y1,i); is_line(); fk(2,x1,y1,i); if(end)break; i=nexti;x1=-4;y1=3; fk(re,x1,y1,i); nedr(); } } } KillTimer(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值