c語言 圖的遍歷

給指針g開辟一個空間 直接給指針賦值不用開辟空間 創建一個指針對象的時候要開辟空間

❷ tc語言循環找圖怎麼寫,

空間 依次找圖

整型 線程編號1

功能 邏輯型 線程方法1()

循環(真)

輔助.等待(1000)

鍵盤.按鍵(65,1) //A

輔助.等待(500)

整型 x,y,圖I

圖I=圖像.找圖(0,0,1440,900,"rc:I.bmp",#102030,0.8,0,x,y) //找圖I

如果(x>0)

鍵盤.按鍵(66,1) //B

輔助揣叮編顧妝該表雙勃晶.等待(500)

鍵盤.按鍵(67,1) //C

輔助.等待(500)

整型 x,y,圖II
x = 0 //我不懂你這個語言,用就是在這里給x賦初值0就是
循環( x <= 0) //同樣,改成你這個語言的判斷
圖II=圖像.找圖(0,0,1440,900,"rc:II.bmp",#102030,0.8,0,x,y) //找圖II

如果(x>0)

鍵盤.按鍵(68,1) //D

輔助.等待(500)
如果結束
循環結束
如果結束
循環結束

返回 真
功能結束

❸ 數據結構代碼(用C語言) 圖的遍歷操作

#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等*/
#include<limits.h> /* INT_MAX 等*/
#include<stdio.h> /* EOF(=^Z 或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函數結果狀態代碼*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因為在math.h 中已定義OVERFLOW 的值為3,故去掉此行*/
typedef int Status; /* Status 是函數的類型,其值是函數結果狀態代碼,如OK 等*/
typedef int Boolean; Boolean 是布爾類型,其值是TRUE 或FALSE */

/* .........................*/
#define MAX_VERTEX_NUM 20
typedef enumGraphKind; /* */
typedef struct ArcNode
{
int adjvex; /* 該弧所指向的頂點的位置*/
struct ArcNode *nextarc; /* 指向下一條弧的指針*/
InfoType *info; /* 網的權值指針) */
}ArcNode; /* 表結點*/
typedef struct
{
VertexType data; /* 頂點信息*/
ArcNode *firstarc; /* 第一個表結點的地址,指向第一條依附該頂點的弧的指針*/
}VNode,AdjList[MAX_VERTEX_NUM]; /* 頭結點*/
typedef struct
{
AdjList vertices;
int vexnum,arcnum; /* 圖的當前頂點數和弧數*/
int kind; /* 圖的種類標志*/
}ALGraph;
/* .........................*/

/* .........................*/
/*ALGraphAlgo.cpp 圖的鄰接表存儲(存儲結構由ALGraphDef.h 定義)的基本操作*/
int LocateVex(ALGraph G,VertexType u)
{ /* 初始條件: 圖G 存在,u 和G 中頂點有相同特徵*/
/* 操作結果: 若G 中存在頂點u,則返回該頂點在圖中位置;否則返回-1 */
int i;
for(i=0;i<G.vexnum;++i)
if(strcmp(u,G.vertices[i].data)==0)
return i;
return -1;
}
Status CreateGraph(ALGraph &G)
{ /* 採用鄰接表存儲結構,構造沒有相關信息的圖G(用一個函數構造4 種圖) */
int i,j,k;
int w; /* 權值*/
VertexType va,vb;
ArcNode *p;
printf("請輸入圖的類型(有向圖:0,有向網:1,無向圖:2,無向網:3): ");
scanf("%d",&(G.kind));
printf("請輸入圖的頂點數,邊數: ");
scanf("%d,%d",&(G.vexnum),&(G.arcnum));
printf("請輸入%d 個頂點的值(<%d 個字元):\n",G.vexnum,MAX_NAME);
for(i=0;i<G.vexnum;++i) /* 構造頂點向量*/
{
scanf("%s",G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
if(G.kind==1||G.kind==3) /* 網*/
printf("請順序輸入每條弧(邊)的權值、弧尾和弧頭(以空格作為間隔):\n");
else /* 圖*/
printf("請順序輸入每條弧(邊)的弧尾和弧頭(以空格作為間隔):\n");
for(k=0;k<G.arcnum;++k) /* 構造表結點鏈表*/
{
if(G.kind==1||G.kind==3) /* 網*/
scanf("%d%s%s",&w,va,vb);
else /* 圖*/
scanf("%s%s",va,vb);
i=LocateVex(G,va); /* 弧尾*/
j=LocateVex(G,vb); /* 弧頭*/
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
if(G.kind==1||G.kind==3) /* 網*/
{
p->info=(int *)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; /* 圖*/
p->nextarc=G.vertices[i].firstarc; /* 插在表頭*/
G.vertices[i].firstarc=p;
if(G.kind>=2) /* 無向圖或網,產生第二個表結點*/
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
if(G.kind==3) /* 無向網*/
{
p->info=(int*)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; /* 無向圖*/
p->nextarc=G.vertices[j].firstarc; /* 插在表頭*/
G.vertices[j].firstarc=p;
}
}
return OK;
}
void DestroyGraph(ALGraph &G)
{ /* 初始條件: 圖G 存在。操作結果: 銷毀圖G */
int i;
ArcNode *p,*q;
G.vexnum=0;
G.arcnum=0;
for(i=0;i<G.vexnum;++i)
{
p=G.vertices[i].firstarc;
while(p)
{
q=p->nextarc;
if(G.kind%2) /* 網*/
free(p->info);
free(p);
p=q;
}
}
}
VertexType* GetVex(ALGraph G,int v)
{ /* 初始條件: 圖G 存在,v 是G 中某個頂點的序號。操作結果: 返回v 的值*/
if(v>=G.vexnum||v<0)
exit(ERROR);
return &G.vertices[v].data;
}
int FirstAdjVex(ALGraph G,VertexType v)
{ /* 初始條件: 圖G 存在,v 是G 中某個頂點*/
/* 操作結果: 返回v 的第一個鄰接頂點的序號。若頂點在G 中沒有鄰接頂點,則返回-1 */
ArcNode *p;
int v1;
v1=LocateVex(G,v); /* v1 為頂點v 在圖G 中的序號*/
p=G.vertices[v1].firstarc;
if(p)
return p->adjvex;
else
return -1;
}
int NextAdjVex(ALGraph G,VertexType v,VertexType w)
{ /* 初始條件: 圖G 存在,v 是G 中某個頂點,w 是v 的鄰接頂點*/
/* 操作結果: 返回v 的(相對於w 的)下一個鄰接頂點的序號。*/
/* 若w 是v 的最後一個鄰接點,則返回-1 */
ArcNode *p;
int v1,w1;
v1=LocateVex(G,v); /* v1 為頂點v 在圖G 中的序號*/
w1=LocateVex(G,w); /* w1 為頂點w 在圖G 中的序號*/
p=G.vertices[v1].firstarc;
while(p&&p->adjvex!=w1) /* 指針p 不空且所指表結點不是w */
p=p->nextarc;
if(!p||!p->nextarc) /* 沒找到w 或w 是最後一個鄰接點*/
return -1;
else /* p->adjvex==w */
return p->nextarc->adjvex; /* 返回v 的(相對於w 的)下一個鄰接頂點的序號*/
}
Boolean visited[MAX_VERTEX_NUM]; /* 訪問標志數組(全局量) */
void(*VisitFunc)(char* v); /* 函數變數(全局量) */
void DFS(ALGraph G,int v)
{ /* 從第v 個頂點出發遞歸地深度優先遍歷圖G。演算法7.5 */
int w;
VertexType v1,w1;
strcpy(v1,*GetVex(G,v));
visited[v]=TRUE; /* 設置訪問標志為TRUE(已訪問) */
VisitFunc(G.vertices[v].data); /* 訪問第v 個頂點*/
for(w=FirstAdjVex(G,v1);w>=0;w=NextAdjVex(G,v1,strcpy(w1,*GetVex(G,w))))
if(!visited[w])
DFS(G,w); /* 對v 的尚未訪問的鄰接點w 遞歸調用DFS */
}
void DFSTraverse(ALGraph G,void(*Visit)(char*))
{ /* 對圖G 作深度優先遍歷。演算法7.4 */
int v;
VisitFunc=Visit; /* 使用全局變數VisitFunc,使DFS 不必設函數指針參數*/
for(v=0;v<G.vexnum;v++)
visited[v]=FALSE; /* 訪問標志數組初始化*/
for(v=0;v<G.vexnum;v++)
if(!visited[v])
DFS(G,v); /* 對尚未訪問的頂點調用DFS */
printf("\n");
}
typedef int QElemType; /* 隊列類型*/
#include"LinkQueueDef.h"
#include"LinkQueueAlgo.h"
void BFSTraverse(ALGraph G,void(*Visit)(char*))
{/*按廣度優先非遞歸遍歷圖G。使用輔助隊列Q 和訪問標志數組visited。演算法7.6 */
int v,u,w;
VertexType u1,w1;
LinkQueue Q;
for(v=0;v<G.vexnum;++v)
visited[v]=FALSE; /* 置初值*/
InitQueue(Q); /* 置空的輔助隊列Q */
for(v=0;v<G.vexnum;v++) /* 如果是連通圖,只v=0 就遍歷全圖*/
if(!visited[v]) /* v 尚未訪問*/
{
visited[v]=TRUE;
Visit(G.vertices[v].data);
EnQueue(Q,v); /* v 入隊列*/
while(!QueueEmpty(Q)) /* 隊列不空*/
{
DeQueue(Q,u); /* 隊頭元素出隊並置為u */
strcpy(u1,*GetVex(G,u));
for(w=FirstAdjVex(G,u1);w>=0;w=NextAdjVex(G,u1,strcpy(w1,*GetVex(G,w))))
if(!visited[w]) /* w 為u 的尚未訪問的鄰接頂點*/
{
visited[w]=TRUE;
Visit(G.vertices[w].data);
EnQueue(Q,w); /* w 入隊*/
}
}
}
printf("\n");
}
void Display(ALGraph G)
{ /* 輸出圖的鄰接矩陣G */
int i;
ArcNode *p;
switch(G.kind)
{ case DG: printf("有向圖\n"); break;
case DN: printf("有向網\n"); break;
case AG: printf("無向圖\n"); break;
case AN: printf("無向網\n");
}
printf("%d 個頂點:\n",G.vexnum);
for(i=0;i<G.vexnum;++i)
printf("%s ",G.vertices[i].data);
printf("\n%d 條弧(邊):\n",G.arcnum);
for(i=0;i<G.vexnum;i++)
{
p=G.vertices[i].firstarc;
while(p)
{
if(G.kind<=1) /* 有向*/
{
printf("%s→%s ",G.vertices[i].data,G.vertices[p->adjvex].data);
if(G.kind==DN) /* 網*/
printf(":%d ",*(p->info));
}
else /* 無向(避免輸出兩次) */
{
if(i<p->adjvex)
{
printf("%s-%s ",G.vertices[i].data,G.vertices[p->adjvex].data);
if(G.kind==AN) /* 網*/
printf(":%d ",*(p->info));
}
}
p=p->nextarc;
}
printf("\n");
}
}

/* .........................*/

/* .........................*/
#include "pubuse.h"
#define MAX_NAME 3 /* 頂點字元串的最大長度+1 */
typedef int InfoType; /* 存放網的權值*/
typedef char VertexType[MAX_NAME]; /* 字元串類型*/
#include"ALGraphDef.h"
#include"ALGraphAlgo.h"
void print(char *i)
{
printf("%s ",i);
}

void main()
{
int i,j,k,n;
ALGraph g;
VertexType v1,v2;
printf("請選擇有向圖\n");
CreateGraph(g);
Display(g);
printf("深度優先搜索的結果:\n");
DFSTraverse(g,print);
printf("廣度優先搜索的結果:\n");
BFSTraverse(g,print);
DestroyGraph(g); /* 銷毀圖*/
}

❹ C語言循環輸出圖形

說下什麼編程環境?

TC還是mc++6.

這可以做,簡單把你己有的*號圖形寫成獨立的輸出函數,

我想你也是用printf加空格試出來的吧,在主程序調用就行.

#include<stdio.h>

#include<windows.h>//TC不用這頭文件

#include<conio.h>

voidyourprintf1(void){

/*

這里加你自己的*號圖形代碼

*/

printf("* ");

printf("*** ");

printf("***** ");

}

voidyourprintf2(void){

/*

這里加你自己的*號圖形代碼

*/

printf("****** ");

printf("** ");

printf("** ");

printf("** ");

printf("****** ");

}

voidyourprintf3(void){

/*

這里加你自己的*號圖形代碼

*/

}

voidyourprintf4(void){

/*

這里加你自己的*號圖形代碼

*/

}

main(){

do{

system("cls");

yourprintf1();

Sleep(1000);//vc用這個

delay(1000);//tc用這個

getch();

system("cls");

yourprintf2();

Sleep(1000);//vc用這個

delay(1000);//tc用這個

system("cls");

yourprintf3();

Sleep(1000);//vc用這個

delay(1000);//tc用這個

system("cls");

yourprintf4();

Sleep(1000);//vc用這個

delay(1000);//tc用這個

}while(getch()!='q');//按q退出圖形循環,不然一直循環顯示.

}

我只簡單畫了三角形和方形,

只要你自己有空用printf試好另外再復雜的圖形加入上面4個自定義的列印函數里就行.

看下效果:

❺ C語言編寫程序實現圖的遍歷操作

樓主你好,下面是源程序!

/*/////////////////////////////////////////////////////////////*/
/* 圖的深度優先遍歷 */
/*/////////////////////////////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
struct node /* 圖頂點結構定義 */
{
int vertex; /* 頂點數據信息 */
struct node *nextnode; /* 指下一頂點的指標 */
};
typedef struct node *graph; /* 圖形的結構新型態 */
struct node head[9]; /* 圖形頂點數組 */
int visited[9]; /* 遍歷標記數組 */

/********************根據已有的信息建立鄰接表********************/
void creategraph(int node[20][2],int num)/*num指的是圖的邊數*/
{
graph newnode; /*指向新節點的指針定義*/
graph ptr;
int from; /* 邊的起點 */
int to; /* 邊的終點 */
int i;
for ( i = 0; i < num; i++ ) /* 讀取邊線信息,插入鄰接表*/
{
from = node[i][0]; /* 邊線的起點 */
to = node[i][1]; /* 邊線的終點 */

/* 建立新頂點 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 建立頂點內容 */
newnode->nextnode = NULL; /* 設定指標初值 */
ptr = &(head[from]); /* 頂點位置 */
while ( ptr->nextnode != NULL ) /* 遍歷至鏈表尾 */
ptr = ptr->nextnode; /* 下一個頂點 */
ptr->nextnode = newnode; /* 插入節點 */
}
}

/********************** 圖的深度優先搜尋法********************/
void dfs(int current)
{
graph ptr;
visited[current] = 1; /* 記錄已遍歷過 */
printf("vertex[%d]\n",current); /* 輸出遍歷頂點值 */
ptr = head[current].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
if ( visited[ptr->vertex] == 0 ) /* 如過沒遍歷過 */
dfs(ptr->vertex); /* 遞回遍歷呼叫 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
}

/****************************** 主程序******************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 邊線數組 */
{1, 3}, {3, 1},
{1, 4}, {4, 1},
{2, 5}, {5, 2},
{2, 6}, {6, 2},
{3, 7}, {7, 3},
{4, 7}, {4, 4},
{5, 8}, {8, 5},
{6, 7}, {7, 6},
{7, 8}, {8, 7} };
int i;
clrscr();
for ( i = 1; i <= 8; i++ ) /* 頂點數組初始化 */
{
head[i].vertex = i; /* 設定頂點值 */
head[i].nextnode = NULL; /* 指針為空 */
visited[i] = 0; /* 設定遍歷初始標志 */
}
creategraph(node,20); /* 建立鄰接表 */
printf("Content of the gragh's ADlist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf("vertex%d ->",head[i].vertex); /* 頂點值 */
ptr = head[i].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
printf(" %d ",ptr->vertex); /* 印出頂點內容 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
printf("\n"); /* 換行 */
}
printf("\nThe end of the dfs are:\n");
dfs(1); /* 列印輸出遍歷過程 */
printf("\n"); /* 換行 */
puts(" Press any key to quit...");
getch();
}


/*//////////////////////////////////////////*/
/* 圖形的廣度優先搜尋法 */
/* ///////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
#define MAXQUEUE 10 /* 隊列的最大容量 */
struct node /* 圖的頂點結構定義 */
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph; /* 圖的結構指針 */
struct node head[9]; /* 圖的頂點數組 */
int visited[9]; /* 遍歷標記數組 */
int queue[MAXQUEUE]; /* 定義序列數組 */
int front = -1; /* 序列前端 */
int rear = -1; /* 序列後端 */

/***********************二維數組向鄰接表的轉化****************************/
void creategraph(int node[20][2],int num)
{
graph newnode; /* 頂點指針 */
graph ptr;
int from; /* 邊起點 */
int to; /* 邊終點 */
int i;
for ( i = 0; i < num; i++ ) /* 第i條邊的信息處理 */
{
from = node[i][0]; /* 邊的起點 */
to = node[i][1]; /* 邊的終點 */
/* 建立新頂點 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 頂點內容 */
newnode->nextnode = NULL; /* 設定指針初值 */
ptr = &(head[from]); /* 頂點位置 */
while ( ptr->nextnode != NULL ) /* 遍歷至鏈表尾 */
ptr = ptr->nextnode; /* 下一個頂點 */
ptr->nextnode = newnode; /* 插入第i個節點的鏈表尾部 */
}
}

/************************ 數值入隊列************************************/
int enqueue(int value)
{
if ( rear >= MAXQUEUE ) /* 檢查佇列是否全滿 */
return -1; /* 無法存入 */
rear++; /* 後端指標往前移 */
queue[rear] = value; /* 存入佇列 */
}

/************************* 數值出隊列*********************************/
int dequeue()
{
if ( front == rear ) /* 隊列是否為空 */
return -1; /* 為空,無法取出 */
front++; /* 前端指標往前移 */
return queue[front]; /* 從隊列中取出信息 */
}

/*********************** 圖形的廣度優先遍歷************************/
void bfs(int current)
{
graph ptr;
/* 處理第一個頂點 */
enqueue(current); /* 將頂點存入隊列 */
visited[current] = 1; /* 已遍歷過記錄標志置疑1*/
printf(" Vertex[%d]\n",current); /* 列印輸出遍歷頂點值 */
while ( front != rear ) /* 隊列是否為空 */
{
current = dequeue(); /* 將頂點從隊列列取出 */
ptr = head[current].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
if ( visited[ptr->vertex] == 0 ) /*頂點沒有遍歷過*/
{
enqueue(ptr->vertex); /* 獎定點放入隊列 */
visited[ptr->vertex] = 1; /* 置遍歷標記為1 */
printf(" Vertex[%d]\n",ptr->vertex);/* 印出遍歷頂點值 */
}
ptr = ptr->nextnode; /* 下一個頂點 */
}
}
}

/*********************** 主程序 ************************************/
/*********************************************************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 邊信息數組 */
{6, 3}, {3, 6},
{2, 4}, {4, 2},
{1, 5}, {5, 1},
{3, 7}, {7, 3},
{1, 7}, {7, 1},
{4, 8}, {8, 4},
{5, 8}, {8, 5},
{2, 8}, {8, 2},
{7, 8}, {8, 7} };
int i;
clrscr();
puts("This is an example of Width Preferred Traverse of Gragh.\n");
for ( i = 1; i <= 8; i++ ) /*頂點結構數組初始化*/
{
head[i].vertex = i;
head[i].nextnode = NULL;
visited[i] = 0;
}
creategraph(node,20); /* 圖信息轉換,鄰接表的建立 */
printf("The content of the graph's allist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf(" vertex%d =>",head[i].vertex); /* 頂點值 */
ptr = head[i].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
printf(" %d ",ptr->vertex); /* 列印輸出頂點內容 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
printf("\n"); /* 換行 */
}
printf("The contents of BFS are:\n");
bfs(1); /* 列印輸出遍歷過程 */
printf("\n"); /* 換行 */
puts(" Press any key to quit...");
getch();
}


❻ 求大神幫寫一個c語言圖的深度優先遍歷,和廣度優先遍歷

/*深度優先*/
#include<stdio.h>
#include<stdlib.h>
struct node/*圖的頂點結構*/
{
int vertex;
int flag;
struct node *nextnode;
};
typedef struct node *graph;
struct node vertex_node[10];
void creat_graph(int *node,int n)
{
graph newnode,p;/*定義一個新結點及指針*/
int start,end,i;
for(i=0;i<n;i++)
{
start=node[i*2];/*邊的起點*/
end=node[i*2+1];/*邊的終點*/
newnode=(graph)malloc(sizeof(struct node));
newnode->vertex=end;/*新結點的內容為邊終點處頂點的內容*/
newnode->nextnode=NULL;
p=&(vertex_node[start]);/*設置指針位置*/
while(p->nextnode!=NULL)
p=p->nextnode;/*尋找鏈尾*/
p->nextnode=newnode;/*在鏈尾處插入新結點*/
}
}
void dfs(int k)
{
graph p;
vertex_node[k].flag=1;/*將標志位置1,證明該結點已訪問過*/
printf("vertex[%d]",k);
p=vertex_node[k].nextnode;/*指針指向下個結點*/
while(p!=NULL)
{
if(vertex_node[p->vertex].flag==0)/*判斷該結點的標志位是否為0*/
dfs(p->vertex);/*繼續遍歷下個結點*/
p=p->nextnode;/*若已遍歷過p指向下一個結點*/
}
}
main()
{
graph p;
int node[100],i,sn,vn;
printf("please input the number of sides:\n");
scanf("%d",&sn);/*輸入無向圖的邊數*/
printf("please input the number of vertexes\n");
scanf("%d",&vn);
printf("please input the vertexes which connected by the sides:\n");
for(i=0;i<4*sn;i++)
scanf("%d",&node[i]);/*輸入每個邊所連接的兩個頂點,起始及結束位置不同,每邊輸兩次*/
for(i=1;i<=vn;i++)
{
vertex_node[i].vertex=i;/*將每個頂點的信息存入數組中*/
vertex_node[i].nextnode=NULL;
}
creat_graph(node,2*sn);/*調用函數創建鄰接表*/
printf("the result is:\n");
for(i=1;i<=vn;i++)/*將鄰接表內容輸出*/
{
printf("vertex%d:",vertex_node[i].vertex);/*輸出頂點內容*/
p=vertex_node[i].nextnode;
while(p!=NULL)
{
printf("->%3d",p->vertex);/*輸出鄰接頂點的內容*/
p=p->nextnode;/*指針指向下個鄰接頂點*/
}
printf("\n");
}
printf("the result of depth-first search is:\n");
dfs(1);/*調用函數進行深度優先遍歷*/
printf("\n");
}
/***************************廣度優先*******************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph;
struct node vertex_node[10];
#define MAXQUEUE 100
int queue[MAXQUEUE];
int front = - 1;
int rear = - 1;
int visited[10];
void creat_graph(int *node, int n)
{
graph newnode, p; /*定義一個新結點及指針*/
int start, end, i;
for (i = 0; i < n; i++)
{
start = node[i *2]; /*邊的起點*/
end = node[i *2+1]; /*邊的終點*/
newnode = (graph)malloc(sizeof(struct node));
newnode->vertex = end; /*新結點的內容為邊終點處頂點的內容*/
newnode->nextnode = NULL;
p = &(vertex_node[start]); /*設置指針位置*/
while (p->nextnode != NULL)
p = p->nextnode;
/*尋找鏈尾*/
p->nextnode = newnode; /*在鏈尾處插入新結點*/
}
}

int enqueue(int value) /*元素入隊列*/
{
if (rear >= MAXQUEUE)
return - 1;
rear++; /*移動隊尾指針*/
queue[rear] = value;
}

int dequeue() /*元素出隊列*/
{
if (front == rear)
return - 1;
front++; /*移動隊頭指針*/
return queue[front];
}

void bfs(int k) /*廣度優先搜索*/
{
graph p;
enqueue(k); /*元素入隊*/
visited[k] = 1;
printf("vertex[%d]", k);
while (front != rear)
/*判斷是否對空*/
{
k = dequeue(); /*元素出隊*/
p = vertex_node[k].nextnode;
while (p != NULL)
{
if (visited[p->vertex] == 0)
/*判斷其是否被訪問過*/
{
enqueue(p->vertex);
visited[p->vertex] = 1; /*訪問過的元素置1*/
printf("vertex[%d]", p->vertex);
}
p = p->nextnode; /*訪問下一個元素*/
}
}
}

main()
{
graph p;
int node[100], i, sn, vn;
printf("please input the number of sides:\n");
scanf("%d", &sn); /*輸入無向圖的邊數*/
printf("please input the number of vertexes\n");
scanf("%d", &vn);
printf("please input the vertexes which connected by the sides:\n");
for (i = 0; i < 4 *sn; i++)
scanf("%d", &node[i]);
/*輸入每個邊所連接的兩個頂點,起始及結束位置不同,每邊輸兩次*/
for (i = 1; i <= vn; i++)
{
vertex_node[i].vertex = i; /*將每個頂點的信息存入數組中*/
vertex_node[i].nextnode = NULL;
}
creat_graph(node, 2 *sn); /*調用函數創建鄰接表*/
printf("the result is:\n");
for (i = 1; i <= vn; i++)
/*將鄰接表內容輸出*/
{
printf("vertex%d:", vertex_node[i].vertex); /*輸出頂點內容*/
p = vertex_node[i].nextnode;
while (p != NULL)
{
printf("->%3d", p->vertex); /*輸出鄰接頂點的內容*/
p = p->nextnode; /*指針指向下個鄰接頂點*/
}
printf("\n");
}
printf("the result of breadth-first search is:\n");
bfs(1); /*調用函數進行深度優先遍歷*/
printf("\n");
}

❼ 用C語言編程實現圖的遍歷演算法

圖的遍歷是指按某條搜索路徑訪問圖中每個結點,使得每個結點均被訪問一次,而且僅被訪問一次。圖的遍歷有深度遍歷演算法和廣度遍歷演算法,最近阿傑做了關於圖的遍歷的演算法,下面是圖的遍歷深度優先的演算法(C語言程序):
#include<stdio.h>
#include<malloc.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;i<G->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;i<G->n;i++)
visited[i]=0;
for(i=0;i<G->n;i++)
if(!visited[i])
DFS(G,i);
}
int main()
{
ALGRAPH *G;
printf("下面以臨接表存儲一個圖;\n");
creat(G);
printf("下面以深度優先遍歷該圖 \n");
DFSTraverse(G);
getchar();
}

❽ c語言的for循環見圖片求解釋

這明顯是vb嘛,這么搞到C語言來問呢,答案是D,詳見如下:

#include<stdio.h>
voidmain(){
inti,j,k,x;
for(i=1;i<=4;i++){
x=4;
for(j=1;j<=3;j++){
x=3;
for(k=1;k<=2;k++){
x+=6;
printf("i=%d,j=%d,k=%d,x=%d ",i,j,k,x);
}
}
}
printf("x=%d ",x);
}

運行結果:

i=1,j=1,k=1,x=9

i=1,j=1,k=2,x=15

i=1,j=2,k=1,x=9

i=1,j=2,k=2,x=15

i=1,j=3,k=1,x=9

i=1,j=3,k=2,x=15

i=2,j=1,k=1,x=9

i=2,j=1,k=2,x=15

i=2,j=2,k=1,x=9

i=2,j=2,k=2,x=15

i=2,j=3,k=1,x=9

i=2,j=3,k=2,x=15

i=3,j=1,k=1,x=9

i=3,j=1,k=2,x=15

i=3,j=2,k=1,x=9

i=3,j=2,k=2,x=15

i=3,j=3,k=1,x=9

i=3,j=3,k=2,x=15

i=4,j=1,k=1,x=9

i=4,j=1,k=2,x=15

i=4,j=2,k=1,x=9

i=4,j=2,k=2,x=15

i=4,j=3,k=1,x=9

i=4,j=3,k=2,x=15

x=15

❾ C語言編程 圖的創建與遍歷

在C語言編程中,圖的創建和遍歷:

#include<stdio.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;i<g->vexnum;i++) /*鄰接矩陣初始化*/

for(j=0;j<g->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;j<g->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;i<g->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;j<g->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;i<g->vexnum;i++)

if(visited[i]!=TRUE)

bfs(i,g);

printf("

");

}

void init_visit() /*初始化訪問標識數組*/

{

int i;

for(i=0;i<N;i++)

visited[i]=FALSE;

}

int main()

{

graph ga;

int i,j;

createGraph(&ga);

printf("無向圖的鄰接矩陣:

");

for(i=0;i<ga.vexnum;i++)

{

for(j=0;j<ga.vexnum;j++)

printf("%3d",ga.arcs[i][j]);

printf("

");

}

init_visit();

tdfs(&ga);

init_visit();

tbfs(&ga);

return 0;

}