本文目录一览:
用C语言编程实现图的遍历算法
图的遍历是指按某条搜索路径访问图中每个结点,使得每个结点均被访问一次,而且仅被访问一次。图的遍历有深度遍历算法和广度遍历算法,最近阿杰做了关于图的遍历的算法,下面是图的遍历深度优先的算法(C语言程序):
#includestdio.h
#includemalloc.h
#define MaxVertexNum 5
#define m 5
#define TRUE 1
#define NULL 0
typedef struct node
{
int adjvex;
struct node *next;
}JD;
typedef struct EdgeNode
{
int vexdata;
JD *firstarc;
}TD;
typedef struct
{
TD ag[m];
int n;
}ALGRAPH;
void DFS(ALGRAPH *G,int i)
{
JD *p;
int visited[80];
printf("visit vertex:%d-",G-ag[i].vexdata);
visited[i]=1;
p=G-ag[i].firstarc;
while(p)
{
if (!visited[p-adjvex])
DFS(G,p-adjvex);
p=p-next;
}
}
void creat(ALGRAPH *G)
{
int i,m1,j;
JD *p,*p1;
printf("please input the number of graph\n");
scanf("%d",G-n);
for(i=0;iG-n;i++)
{
printf("please input the info of node %d",i);
scanf("%d",G-ag[i].vexdata);
printf("please input the number of arcs which adj to %d",i);
scanf("%d",m1);
printf("please input the adjvex position of the first arc\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",p-adjvex);
p-next=NULL;
G-ag[i].firstarc=p;
p1=p;
for(j=2 ;j=m1;j++)
{
printf("please input the position of the next arc vexdata\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",p-adjvex);
p-next=NULL;
p1-next=p;
p1=p;
}
}
}
int visited[MaxVertexNum];
void DFSTraverse(ALGRAPH *G)
{
int i;
for(i=0;iG-n;i++)
visited[i]=0;
for(i=0;iG-n;i++)
if(!visited[i])
DFS(G,i);
}
int main()
{
ALGRAPH *G;
printf("下面以临接表存储一个图;\n");
creat(G);
printf("下面以深度优先遍历该图 \n");
DFSTraverse(G);
getchar();
}
C语言的遍历算法
思路1:
写出所有24种4个数的排列,存到一个数组里,假如数组是P[24][4];
那么可以
for (i = 0; i 24; i++)
for (j = 0; j 24; j++)
for (k = 0; k 24; k++)
三层循环,P[i],P[j],P[k]分别是矩阵的三个列
思路2:
利用dfs递归枚举
int used[3][4];/*这个数组存放三个列中0~3这四个数是否已在这一列中出现过,需要提前清零*/
int mat[3][4];/*要枚举的矩阵*/
void dfs(int col, int row)/*col表示现在已经搜索到哪一列(从0开始编号),row表示这一列已经填了几行*/
{
int i;
if (col == 2 row == 4)
{
....../*运行到这里的时候,mat就是枚举到的一个矩阵*/
return;
}
if (row == 4)
{row = 0; col++;}
for (i = 0; i 4; i++)
if (!used[col][i])
{
used[col][i] = 1;
mat[col][row] = i;
dfs(col, row + 1);
used[col][i] = 0;
}
return;
}
调用的时候调用dfs(0,0)
怎么用C语言遍历文件啊?
#include stdio.h #include dos.h #include errno.h #include io.h #include dirent.h #include dir.h #include string.h #include sys\stat.h #include "pm03a.h" void main(int argc,char* argv[]) { //printf("Number %d\n",ConfirmFileAttrib(argv[1])); GetDirectory(argv[1]); printf("\nSearch Over.\n"); } //------------------------------------------------------------------ //pm03a.h //------------------------------------------------------------------ void GetDirectory(char *DirectoryName); int ConfirmFileAttrib(char* filename); char *GetCurrentPath(); char* GetFullFileName(char *filename); char *WillDeleteFile(char *FileName); //-------------------------------------------------------- //--------------- 获得文件属性 --------------------------- //-------------------------------------------------------- int ConfirmFileAttrib(char* filename) { int temp=0; int attrib=(_rtl_chmod(filename,0)); if(attrib==-1) { switch(errno) { case ENOENT: //printf("%s Path or file not found.\n",filename); temp=0; break; case EACCES: //printf("Permission denied.\n"); temp=-1; break; default: //printf("Error number: %d", errno); temp=-2; break; } } else { if(attrib FA_RDONLY) { temp=1; //printf("%s is read-only.\n", filename); } if(attrib FA_HIDDEN) { temp=2; //printf("%s is hidden.\n", filename); } if(attrib FA_SYSTEM) { temp=3; //printf("%s is a system file.\n", filename); } if(attrib FA_DIREC) { temp=4; //printf("%s is a directory.\n", filename); } if (attrib FA_ARCH) { temp=5; //printf("%s is an archive file.\n", filename); } } return temp; } //-------------------------------------------------------- //-------------------------------------------------------- //--------------- 获取目录流 ----------------------------- //-------------------------------------------------------- void GetDirectory(char *DirectoryName) { DIR* Directory_Point; struct dirent *entry; bool DirControl; if((Directory_Point=opendir(DirectoryName))==NULL) { printf("Error opening directory!\n"); return; } else { if(strcmp(DirectoryName,"..")==0) { return; } if(strcmp(DirectoryName,".")==0) DirControl=true; else DirControl=false; chdir(DirectoryName); //char *filename=DirectoryName; //int k=creat(strcat(filename,".txt"),S_IWRITE); while(bool(entry=readdir(Directory_Point))) { if(ConfirmFileAttrib(entry-d_name)==5) // 确定为文件属性 { // 文件过滤 WillDeleteFile(entry-d_name); } if(ConfirmFileAttrib(entry-d_name)==4) // 确定为目录属性 { if(strcmpi(entry-d_name,"..")==0||strcmpi(entry-d_name,".")==0) { continue; } else { //printf("\n%s is direct\n\n",entry-d_name); GetDirectory(entry-d_name); } } } if(!DirControl==true) chdir(".."); closedir(Directory_Point); } } //-------------------------------------------------------- //-------------------------------------------------------- //---------- 判断文件类型以备过滤 ------------------------ //-------------------------------------------------------- char* GetFullFileName(char *filename) { char *FullFilename=GetCurrentPath(); if(strlen(FullFilename)=3) { strcat(FullFilename,filename); } else { strcat(FullFilename,"\\"); strcat(FullFilename,filename); } return FullFilename; } char *GetCurrentPath() { char path[1024]=""; strcpy(path, "X:\\"); /* fill string with form of response: X:\ */ path[0] = 'A' + getdisk(); /* replace X with current drive letter */ getcurdir(0, path+3); /* fill rest of string with current directory */ return path; } char *WillDeleteFile(char *FileName) { int len; for(len=strlen(FileName);len=0;len--) { if(FileName[len]=='.') break; } char* Retname; int s=-1; for(int i=len;i=strlen(FileName);i++) { Retname[s+=1]=FileName[i]; } int i=-1; if(Retname[1]=='~')i=0; if(strcmpi(Retname,".bak")==0)i=0; if(strcmpi(Retname,".obj")==0)i=0; if(strcmpi(Retname,".tds")==0)i=0; if(strcmpi(Retname,".dcu")==0)i=0; if(strcmpi(Retname,".tmp")==0)i=0; if(strcmpi(Retname,".ilk")==0)i=0; if(strcmpi(Retname,".pch")==0)i=0; if(strcmpi(Retname,".pdb")==0)i=0; if(strcmpi(Retname,".tlb")==0)i=0; if(strcmpi(Retname,".idb")==0)i=0; if(strcmpi(Retname,".pdb")==0)i=0; if(strcmpi(Retname,".r$p")==0)i=0; if(strcmpi(Retname,".OBR")==0)i=0; if(strcmpi(Retname,".mbt")==0)i=0; if(strcmpi(Retname,".mrt")==0)i=0; if(strcmpi(Retname,".csm")==0)i=0; if(i==0) { remove(GetFullFileName(FileName)); printf("%s delete\n",GetFullFileName(FileName)); } return Retname; }
C语言编程 图的创建与遍历
在C语言编程中,图的创建和遍历:
#includestdio.h
#define N 20
#define TRUE 1
#define FALSE 0
int visited[N];
typedef struct /*队列的定义*/
{
int data[N];
int front,rear;
}queue;
typedef struct /*图的邻接矩阵*/
{
int vexnum,arcnum;
char vexs[N];
int arcs[N][N];
}
graph;
void createGraph(graph *g); /*建立一个无向图的邻接矩阵*/
void dfs(int i,graph *g); /*从第i个顶点出发深度优先搜索*/
void tdfs(graph *g); /*深度优先搜索整个图*/
void bfs(int k,graph *g); /*从第k个顶点广度优先搜索*/
void tbfs(graph *g); /*广度优先搜索整个图*/
void init_visit(); /*初始化访问标识数组*/
void createGraph(graph *g) /*建立一个无向图的邻接矩阵*/
{ int i,j;
char v;
g-vexnum=0;
g-arcnum=0;
i=0;
printf("输入顶点序列(以#结束):
");
while((v=getchar())!='#')
{
g-vexs[i]=v; /*读入顶点信息*/
i++;
}
g-vexnum=i; /*顶点数目*/
for(i=0;ig-vexnum;i++) /*邻接矩阵初始化*/
for(j=0;jg-vexnum;j++)
g-arcs[i][j]=0;
printf("输入边的信息:
");
scanf("%d,%d",i,j); /*读入边i,j*/
while(i!=-1) /*读入i,j为-1时结束*/
{
g-arcs[i][j]=1;
g-arcs[j][i]=1;
scanf("%d,%d",i,j);
}
}
void dfs(int i,graph *g) /*从第i个顶点出发深度优先搜索*/
{
int j;
printf("%c",g-vexs[i]);
visited[i]=TRUE;
for(j=0;jg-vexnum;j++)
if((g-arcs[i][j]==1)(!visited[j]))
dfs(j,g);
}
void tdfs(graph *g) /*深度优先搜索整个图*/
{
int i;
printf("
从顶点%C开始深度优先搜索序列:",g-vexs[0]);
for(i=0;ig-vexnum;i++)
if(visited[i]!=TRUE)
dfs(i,g);
}
void bfs(int k,graph *g) /*从第k个顶点广度优先搜索*/
{
int i,j;
queue qlist,*q;
q=qlist;
q-rear=0;
q-front=0;
printf("%c",g-vexs[k]);
visited[k]=TRUE;
q-data[q-rear]=k;
q-rear=(q-rear+1)%N;
while(q-rear!=q-front)
{
i=q-data[q-front];
q-front=(q-front+1)%N;
for(j=0;jg-vexnum;j++)
if((g-arcs[i][j]==1)(!visited[j]))
{
printf("%c",g-vexs[j]);
visited[j]=TRUE;
q-data[q-rear]=j;
q-rear=(q-rear+1)%N;
}
}
}
void tbfs(graph *g) /*广度优先搜索整个图*/
{
int i;
printf("
从顶点%C开始广度优先搜索序列:",g-vexs[0]);
for(i=0;ig-vexnum;i++)
if(visited[i]!=TRUE)
bfs(i,g);
printf("
");
}
void init_visit() /*初始化访问标识数组*/
{
int i;
for(i=0;iN;i++)
visited[i]=FALSE;
}
int main()
{
graph ga;
int i,j;
createGraph(ga);
printf("无向图的邻接矩阵:
");
for(i=0;iga.vexnum;i++)
{
for(j=0;jga.vexnum;j++)
printf("%3d",ga.arcs[i][j]);
printf("
");
}
init_visit();
tdfs(ga);
init_visit();
tbfs(ga);
return 0;
}
C语言编程,顾名思义,就是用C语言来进行计算机编程工作。
C语言是国际上广泛流行的,很有发展前途的计算机高级语言.它适合作为系统描述语言,即可用来编写系统软件,也可用来编写应用软件.
C语言是一种引用广泛,并且实现灵活的一种计算机编程语言,用C语言编出来的程序,可以在很多平台上运行,可移植性强。具体的C语言编程内容请参加C或者C++等。