用c语言编一段图的创建与遍历的代码

Python014

用c语言编一段图的创建与遍历的代码,第1张

//#include <stdafx.h>

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 20

typedef char VertexType//顶点数据类型

bool visited[MAX_SIZE]

typedef struct ArcNode{

int AdjVex

struct ArcNode *nextarc

}ArcNode

typedef struct VNode{

VertexType data

ArcNode *firstarc

}VNode,AdjList[MAX_SIZE]

typedef struct{

int vexnum,arcnum

AdjList Vertices

}ALGraph

int LocateVex(ALGraph G,char v)

{

int i

for(i=0i<G.vexnumi++)

{

if(G.Vertices[i].data==v) return i

}

return -1

}

void CreatG(ALGraph &G)

{

int i,a,b

char m,n

ArcNode *p,*q

scanf("%d %d",&G.vexnum,&G.arcnum)

printf("请依次输入顶点:\n")

for(i=0i<G.vexnumi++)

{

flushall()

scanf("%c",&G.Vertices[i].data)

G.Vertices[i].firstarc=NULL

}

printf("请依次输入边:\n")

for(i=0i<G.arcnumi++)

{

flushall()

scanf("%c %c",&m,&n)

a=LocateVex(G,m)

b=LocateVex(G,n)

p=(ArcNode *)malloc(sizeof(ArcNode))

p->AdjVex=b

p->nextarc=G.Vertices[a].firstarc

G.Vertices[a].firstarc=p

q=(ArcNode *)malloc(sizeof(ArcNode))

q->AdjVex=a

q->nextarc=G.Vertices[b].firstarc

G.Vertices[b].firstarc=q

}

}

int FirstAdjVex(ALGraph G,int v)

{

ArcNode *p

p=G.Vertices[v].firstarc

return p->AdjVex

}

int NextAdjVex(ALGraph G,int v,int w)

{

ArcNode *p

p=G.Vertices[v].firstarc

while(p->nextarc)

{

return p->nextarc->AdjVex

}

}

void DFS(ALGraph G,int v)

{

int w

visited[v]=true

printf("%c ",G.Vertices[v].data)

for(w=FirstAdjVex(G,v)w>=0w=NextAdjVex(G,v,w))

if(!visited[w])

DFS(G,w)

}

void DFSTraverse(ALGraph G){

int v

for(v=0v<G.vexnumv++)

visited[v]=false

for(v=0v<G.vexnumv++)

if(!visited[v])

DFS(G,v)

}

void main()

{

ALGraph G

printf("请输入图的顶点数和边数:\n")

CreatG(G)

printf("深度优先遍历结果:\n")

DFSTraverse(G)

}

希望对你有用

framebuffer(帧缓冲)。

帧的最低数量为24(人肉眼可见)(低于24则感觉到画面不流畅)。

显卡与帧的关系:由cpu调节其数据传输速率来输出其三基色的配比。

三基色:RGB(红绿蓝)。

在没有桌面和图形文件的系统界面,可以通过C语言的编程来实现在黑色背景上画图!

用下面的代码,在需要的地方(有注释)适当修改,就能画出自己喜欢的图形!

PS:同样要编译运行后才能出效果。

#include <stdio.h>

#include <sys/mman.h>

#include <fcntl.h>

#include <linux/fb.h>

#include <stdlib.h>

#define RGB888(r,g,b) ((r &0xff) <<16 | (g &0xff) <<8 | (b &0xff))

#define RGB565(r,g,b) ((r &0x1f) <<11 | (g &0x3f) <<5 | (b &0x1f))

int main()

{

int fd = open("/dev/fb0", O_RDWR)

if(fd <0){

perror("open err. \n")

exit(EXIT_FAILURE)

printf("xres: %d\n", info.xres)

printf("yres: %d\n", info.yres)

printf("bits_per_pixel: %d\n", info.bits_per_pixel)

size_t len = info.xres*info.yres*info.bits_per_pixel >>3

unsigned long* addr = NULL

addr = mmap(NULL, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0)

if(addr == (void*)-1){

perror("mmap err. \n")