提示:该blog仅为完成作业,大佬请绕路
老师的代码
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
int* visitedPtr;
/**
* A queue with a number of indices.
*/
typedef struct GraphNodeQueue{
int* nodes;
int front;
int rear;
}GraphNodeQueue, *QueuePtr;
/**
* Initialize the queue.
*/
QueuePtr initQueue(){
QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct GraphNodeQueue));
resultQueuePtr->nodes = (int*)malloc(QUEUE_SIZE * sizeof(int));
resultQueuePtr->front = 0;
resultQueuePtr->rear = 1;
return resultQueuePtr;
}//Of initQueue
/**
* Is the queue empty?
*/
bool isQueueEmpty(QueuePtr paraQueuePtr){
if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
return true;
}//Of if
return false;
}//Of isQueueEmpty
/**
* Add a node to the queue.
*/
void enqueue(QueuePtr paraQueuePtr, int paraNode){
//printf("front = %d, rear = %d.\r\n", paraQueuePtr->front, paraQueuePtr->rear);
if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
printf("Error, trying to enqueue %d. queue full.\r\n", paraNode);
return;
}//Of if
paraQueuePtr->nodes[paraQueuePtr->rear] = paraNode;
paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
//printf("enqueue %d ends.\r\n", paraNode);
}//Of enqueue
/**
* Remove an element from the queue and return.
*/
int dequeue(QueuePtr paraQueuePtr){
if (isQueueEmpty(paraQueuePtr)) {
printf("Error, empty queue\r\n");
return NULL;
}//Of if
paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
//printf("dequeue %d ends.\r\n", paraQueuePtr->nodes[paraQueuePtr->front]);
return paraQueuePtr->nodes[paraQueuePtr->front];
}//Of dequeue
/**
* The structure of a graph.
*/
typedef struct Graph{
int** connections;
int numNodes;
} *GraphPtr;
//void deepFirst(GraphPtr paraGraphPtr, int paraNode);
GraphPtr initGraph(int paraSize, int** paraData) {
int i, j;
GraphPtr resultPtr = (GraphPtr)malloc(sizeof(Graph));
resultPtr -> numNodes = paraSize;
//resultPtr -> connections = (int**)malloc(paraSize * paraSize * sizeof(int));
resultPtr -> connections = (int**)malloc(paraSize * sizeof(int*));
for (i = 0; i < paraSize; i ++) {
resultPtr -> connections[i] = (int*)malloc(paraSize * sizeof(int));
for (j = 0; j < paraSize; j ++) {
resultPtr -> connections[i][j] = paraData[i][j];
}//Of for j
}//Of for i
return resultPtr;
}//Of initGraph
/**
* Initialize the tranverse.
*/
void initTranverse(GraphPtr paraGraphPtr) {
int i;
//Initialize data
visitedPtr = (int*) malloc(paraGraphPtr -> numNodes * sizeof(int));
for (i = 0; i < paraGraphPtr -> numNodes; i ++) {
visitedPtr[i] = 0;
}//Of for i
}//Of initTranverse
/**
* Depth first tranverse.
*/
void depthFirstTranverse(GraphPtr paraGraphPtr, int paraNode) {
int i;
visitedPtr[paraNode] = 1;
printf("%d\t", paraNode);
for (i = 0; i < paraGraphPtr -> numNodes; i ++) {
if (!visitedPtr[i]){
if (paraGraphPtr -> connections[paraNode][i]) {
depthFirstTranverse(paraGraphPtr, i);
}//Of if
}//Of if
}//Of for i
}//Of depthFirstTranverse
/**
* Width first tranverse.
*/
void widthFirstTranverse(GraphPtr paraGraphPtr, int paraStart){
//Use a queue to manage the pointers
int i, j, tempNode;
i = 0;
QueuePtr tempQueuePtr = initQueue();
printf("%d\t", paraStart);
visitedPtr[paraStart] = 1;
enqueue(tempQueuePtr, paraStart);
while (!isQueueEmpty(tempQueuePtr)) {
tempNode = dequeue(tempQueuePtr);
visitedPtr[tempNode] = 1;
//For output.
i ++;
for (j = 0; j < paraGraphPtr->numNodes; j ++) {
if (visitedPtr[j])
continue;
if (paraGraphPtr->connections[tempNode][j] == 0)
continue;
printf("%d\t", j);
visitedPtr[j] = 1;
enqueue(tempQueuePtr, j);
}//Of for j
}//Of while
}//Of widthFirstTranverse
void testGraphTranverse() {
int i, j;
int myGraph[5][5] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 1},
{1, 0, 1, 0, 0},
{0, 1, 1, 0, 0}};
int** tempPtr;
printf("Preparing data\r\n");
tempPtr = (int**)malloc(5 * sizeof(int*));
for (i = 0; i < 5; i ++) {
tempPtr[i] = (int*)malloc(5 * sizeof(int));
}//Of for i
for (i = 0; i < 5; i ++) {
for (j = 0; j < 5; j ++) {
//printf("i = %d, j = %d, ", i, j);
//printf("%d\r\n", tempPtr[i][j]);
tempPtr[i][j] = myGraph[i][j];
//printf("i = %d, j = %d, %d\r\n", i, j, tempPtr[i][j]);
}//Of for j
}//Of for i
printf("Data ready\r\n");
GraphPtr tempGraphPtr = initGraph(5, tempPtr);
printf("num nodes = %d \r\n", tempGraphPtr -> numNodes);
printf("Graph initialized\r\n");
printf("Depth first visit:\r\n");
initTranverse(tempGraphPtr);
depthFirstTranverse(tempGraphPtr, 4);
printf("\r\nWidth first visit:\r\n");
initTranverse(tempGraphPtr);
widthFirstTranverse(tempGraphPtr, 4);
}//Of testGraphTranverse
int main(){
testGraphTranverse();
return 1;
}//Of main
我的代码
#include<stdio.h>
#include<stdlib.h>
#define max 20
//边表节点
typedef struct node{
int adjvex;
struct node *next;
}eNode;
//头节点
typedef struct headnode{
char vertex;
eNode *firstedge;
}hNode;
//邻接表
typedef struct{
hNode adjlist[max];
int n,e; //顶点数,边数
}linkG;
//创建(邻接表)
linkG *creat(linkG *g,int c) //c为0表示无向图
{
int i,j,k;
eNode *s;
int n1,e1;
char ch;
g=(linkG *)malloc(sizeof(linkG));
printf("请输入顶点数及边数: ");
scanf("%d%d",&n1,&e1);
g->n=n1;g->e=e1;
printf("请输入顶点信息:");
getchar();
for(i=0;i<n1;i++)
{
scanf("%c",&ch);
g->adjlist[i].vertex=ch;
g->adjlist[i].firstedge=NULL;
}
getchar();
int i1,j1;
for(k=0;k<e1;k++)
{
printf("请输入对(i,j): ");
scanf("%d%d",&i1,&j1);
s=(eNode *)malloc(sizeof(eNode));
s->adjvex=j1;
s->next=g->adjlist[i1].firstedge;
g->adjlist[i1].firstedge=s;
if(c==0)
{
s=(eNode *)malloc(sizeof(eNode));
s->adjvex=i1;
s->next=g->adjlist[j1].firstedge;
g->adjlist[j1].firstedge=s;
}
}
return g;
}
int visited[max]; //标记是否访问
//深度优先遍历DFS
void dfs(linkG *g,int i) //顶点i
{
eNode *p;
printf("%c ",g->adjlist[i].vertex);
visited[i]=1;
p=g->adjlist[i].firstedge;
while(p)
{
if(!visited[p->adjvex])
dfs(g,p->adjvex);
p=p->next;
}
}
void dfstravel(linkG *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);
}
//广度优先遍历BFS
void bfs(linkG *g,int i)
{
int j;
eNode *p;
int q[max],front,rear;
front=rear=0;
printf("%c ",g->adjlist[i].vertex);
visited[i]=1;
q[rear++]=i;
while(rear>front)
{
j=q[front++];
p=g->adjlist[j].firstedge;
while(p)
{
if(visited[p->adjvex]==0)
{
printf("%c ",g->adjlist[p->adjvex].vertex);
q[rear++]=p->adjvex;
visited[p->adjvex]=1;
}
p=p->next;
}
}
}
void bfstravel(linkG *g)
{
int i,count=0;
for(i=0;i<g->n;i++)
visited[i]=0;
for(i=0;i<g->n;i++)
if(!visited[i])
bfs(g,i);
}
//主函数
int main()
{
linkG *g;
g=creat(g,0);
printf("DFS:");
dfstravel(g);
printf("\n");
printf("BFS:");
bfstravel(g);
printf("\n");
}