提示:该blog仅为完成作业,大佬请绕路
老师的代码
/**
* Adjacency list for directed graph.
*
* @author Fan Min minfanphd@163.com.
*/
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
/*************** Copied code begins *****************/
int* visitedPtr;
/**
* The structure of a graph.
*/
typedef struct Graph{
int** connections;
int numNodes;
} *GraphPtr;
/**
* Initialize a graph.
*/
GraphPtr initGraph(int paraSize, int** paraData) {
int i, j;
GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct 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
/**
* 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){
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;
}//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
/*************** Copied code ends *****************/
/**
* Aajacent node.
*/
typedef struct AdjacencyNode {
int column;
AdjacencyNode* next;
}AdjacencyNode, *AdjacentNodePtr;
/**
* Aajacent list.
*/
typedef struct AdjacencyList {
int numNodes;
AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;
/**
* Construct an adjacent list.
*/
AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr) {
//Allocate space.
int i, j, tempNum;
AdjacentNodePtr p, q;
tempNum = paraPtr->numNodes;
AdjacencyListPtr resultPtr = (AdjacencyListPtr)malloc(sizeof(struct AdjacencyList));
resultPtr->numNodes = tempNum;
resultPtr->headers = (AdjacencyNode*)malloc(tempNum * sizeof(struct AdjacencyNode));
//Fill the data.
for (i = 0; i < tempNum; i ++) {
//Initialize headers.
p = &(resultPtr->headers[i]);
p->column = -1;
p->next = NULL;
for (j = 0; j < tempNum; j ++) {
if (paraPtr->connections[i][j] > 0) {
//Create a new node.
q = (AdjacentNodePtr)malloc(sizeof(struct AdjacencyNode));
q->column = j;
q->next = NULL;
//Link.
p->next = q;
p = q;
}//Of if
}//Of for j
}//Of for i
return resultPtr;
}//Of graphToAdjacentList
/**
* Print an adjacent list.
*/
void printAdjacentList(AdjacencyListPtr paraPtr) {
int i;
AdjacentNodePtr p;
int tempNum = paraPtr->numNodes;
printf("This is the graph:\r\n");
for (i = 0; i < tempNum; i ++) {
p = paraPtr->headers[i].next;
while (p != NULL) {
printf("%d, ", p->column);
p = p->next;
}//Of while
printf("\r\n");
}//Of for i
}//Of printAdjacentList
/**
* Width first tranverse.
*/
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart){
printf("width first \r\n");
//Use a queue to manage the pointers
int i, j, tempNode;
AdjacentNodePtr p;
i = 0;
//Initialize data
visitedPtr = (int*) malloc(paraListPtr->numNodes * sizeof(int));
for (i = 0; i < paraListPtr->numNodes; i ++) {
visitedPtr[i] = 0;
}//Of for i
QueuePtr tempQueuePtr = initQueue();
printf("%d\t", paraStart);
visitedPtr[paraStart] = 1;
enqueue(tempQueuePtr, paraStart);
// printf("After enqueue\r\n");
while (!isQueueEmpty(tempQueuePtr)) {
// printf("First while\r\n");
tempNode = dequeue(tempQueuePtr);
for (p = &(paraListPtr->headers[tempNode]); p != NULL; p = p->next) {
j = p->column;
// printf("j = %d \r\n", j);
if (visitedPtr[j])
continue;
printf("%d\t", j);
visitedPtr[j] = 1;
enqueue(tempQueuePtr, j);
}//Of for
}//Of while
printf("\r\n");
}//Of widthFirstTranverse
/**
* Test graph tranverse.
*/
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);
AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);
printAdjacentList(tempListPtr);
widthFirstTranverse(tempListPtr, 4);
}//Of testGraphTranverse
/**
* Entrance.
*/
int main(){
testGraphTranverse();
return 1;
}//Of main
我的代码
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 7 //代表最大顶点数
#define INFINITY 65535 //代表无穷大
typedef struct GraphNode
{
int index; //下标
struct GraphNode *next; //指向下一个的指针
}GraphNode;
typedef struct Gra
{
char c; //顶点元素
GraphNode *first ;
}Gra;
typedef struct Graph
{
Gra vex[MAXVEX];
int numvex,edge; //顶点元素和边的数量
}Graph;
//创建一个队列,用来广度优先遍历
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*Queueprt;
typedef struct
{
Queueprt front,rear;
}LinkQueue;
//队列的初始化
void InitQueue(LinkQueue *q)
{
q->front=q->rear=(Queueprt)malloc(sizeof(QNode));
q->front->next=NULL;
}
//入队列
void EnQueue(LinkQueue *q,int e)
{
Queueprt p;
p=(Queueprt)malloc(sizeof(QNode));
p->data=e;
q->rear->next=p;
p->next=NULL;
q->rear=p;
}
//出队列
void DeQueue(LinkQueue *q,int *e)
{
if(q->front==q->rear)
{
return;
}
Queueprt p;
p=q->front->next;
*e = p->data;
q->front->next=p->next;
if(p==q->rear)
{
q->rear=q->front;
}
free(p);
}
//判断队列是否为空
int QueueEmpty(LinkQueue *q)
{
if(q->front==q->rear)
{
return 1;
}
else
{
return 0;
}
}
//创建图
void create(Graph *g)
{
int i,j,m,n;
GraphNode *p,*q;
printf("输入顶点元素的数量和边的数量:\n");
scanf("%d %d",&(g->numvex),&(g->edge));
printf("输入顶点元素:\n");
for(i=0;i<g->numvex;i++)
{
getchar();
scanf("%c",&(g->vex[i].c) );
g->vex[i].first=NULL;
}
//建立边集
printf("输入边两边顶点的下标:\n");
for(j=0;j<g->edge;j++)
{
scanf("%d %d",&m,&n);
p=(GraphNode *)malloc(sizeof(GraphNode));
p->index=m;
p->next=g->vex[n].first;
g->vex[n].first=p; //这里是把first移到p
q=(GraphNode *)malloc(sizeof(GraphNode));
q->index=n;
q->next=g->vex[m].first;
g->vex[m].first=q;
}
}
void print(Graph *g)
{
int i;
GraphNode *p;
for(i=0;i<g->numvex;i++)
{
p=g->vex[i].first;
while(p)
{
printf("(%c,%c)",g->vex[i].c,g->vex[p->index].c);
p=p->next;
}
printf("\n");
}
}
//DFS遍历
void DFS(Graph *g,int i,int *visited)
{
GraphNode *p;
visited[i]=1;
printf("%c ",g->vex[i].c);
p=g->vex[i].first;
while( p )
{
if(!visited[p->index])
{
DFS(g,p->index,visited);
}
p=p->next;
}
}
void TraDFS(Graph *g)
{
int i;
int visited[MAXVEX];
for(i=0;i<MAXVEX;i++)
{
visited[i]=0;
}
for(i=0;i<g->numvex;i++)
{
if(!visited[i])
{
DFS(g,i,visited);
}
}
}
//BFS遍历
void TraBFS(Graph *g)
{
int i,j;
LinkQueue q;
int visited[MAXVEX];
for(i=0;i<MAXVEX;i++)
{
visited[i]=0;
}
InitQueue(&q);
for(i=0;i<g->numvex;i++)
{
if(!visited[i])
{
printf("%c ",g->vex[i].c);
visited[i]=1;
EnQueue(&q, i);
while(!QueueEmpty(&q))
{
DeQueue(&q,&i);
GraphNode *p = g->vex[i].first;
while( p )
{
if(!visited[p->index])
{
printf("%c ",g->vex[p->index].c);
visited[p->index]=1;
EnQueue(&q,p->index);
}
p=p->next;
}
}
}
}
}
int main(){
Graph g;
create(&g);
print(&g);
printf("DFS遍历结果:\n");
TraDFS(&g);
printf("\nBFS遍历结果为:\n");
TraBFS(&g);
printf("\n");
return 0;
}