图的创建以及遍历

#include<iostream>
typedef int Boolean;
using namespace std;

#define MAXVEX 11
#define TRUE 1
#define FALSE 0
Boolean visited[MAXVEX];

typedef int VertexType;
typedef int EdgeType;

/*Edge struct*/
typedef struct EdgeNode
{
struct EdgeNode *next;
} EdgeNode;

/*Vertex struct*/
typedef struct VertexNode
{
VertexType data;
EdgeNode * firstedge;

/*Graph struct*/
typedef struct
{
int numVertexes, numEdges;
}GraphList;

/*quickSort*/
void quickSort(int * a, int low, int high)
{
if (low >= high) return;
int first = low;
int last = high;
int key = a[first];
while (first < last)
{
while (first < last && a[last] >= key)
last--;
a[first] = a[last];
while (first < last && a[first] <= key)
first++;
a[last] = a[first];
}
a[first] = key;
quickSort(a, low, first - 1);
quickSort(a, first + 1, high);
}

/*插入调整后的数据*/
void insertIndex(GraphList *g,int m,int index)
{
EdgeNode *e;
e = (EdgeNode *)malloc(sizeof(EdgeNode));
}

/*调整图的连接顺序（从小到大）*/
void adjustGraph(GraphList *g, int m)
{
EdgeNode *e = g->adjList[m].firstedge;
int p = 0;
int array[MAXVEX];
while (e)
{
array[p++] = e -> adjvex;
e = e->next;
}
quickSort(array, 0, p - 1);
for (int i = p-1; i >= 0; i--)
{
insertIndex(g, m, array[i]);
}
}

/*创建图*/
void createGraph(GraphList *g)
{
EdgeNode *e;
EdgeNode *f;
cin >> g->numVertexes >> g->numEdges;
/*init vertex*/
for (int i = 0; i < g->numVertexes; i++)
{
}
for (int i = 0; i < g->numEdges; i++)
{
int m, n;
cin >> m >> n;
e = (EdgeNode *)malloc(sizeof(EdgeNode));

f = (EdgeNode *)malloc(sizeof(EdgeNode));
}
}

/*打印图（邻接表形式）*/
void printGraph(GraphList *g)
{
int i = 0;

while (i < g->numVertexes)
{
cout << "index:" << i << " data:" <<g->adjList[i].data <<"->";
EdgeNode *e = NULL;
while (e != NULL)
{
cout << e->adjvex << " ";
e = e->next;
}
i++;
cout << endl;
}
}

/*深度优先搜索*/
void DFS(GraphList *g, int i)
{
EdgeNode *p;
visited[i] = TRUE;
cout << g->adjList[i].data<<" ";
while (p)
{
{
}
p = p->next;
}
}

void DFSTraverse(GraphList* g)
{
int i;
for (i = 0; i < g->numVertexes; i++)
{
visited[i] = FALSE;
}
for ( i = 0; i < g->numVertexes; i++)
{
if (!visited[i])
{
cout << "{ ";
DFS(g, i);
cout << "}" << endl;
}
}
}

typedef struct {
int a[MAXVEX];
int f;
int r;
} Quence;

Quence * createQuence()
{
Quence * q = (Quence *)malloc(sizeof(Quence));
q->f = -1;
q->r = -1;
return q;
}

void addQuence(Quence *q, int np)
{
if ((q->r + 1) % MAXVEX == q->f)
return;
q->r = (q->r + 1) % MAXVEX;
q->a[q->r] = np;
}

int deleteQuence(Quence *q)
{
if (q->r == q->f)
{
return NULL;
}
else
{
q->f = (q->f + 1) % MAXVEX;
return q->a[q->f];
}
}

bool empty(Quence *q)
{
return q->r == q->f;
}

/*广度优先搜索*/
void BFSTraverse(GraphList *g)
{
EdgeNode *p;
Quence *q = createQuence();
for (int i = 0; i < g->numVertexes; i++)
{
visited[i] = FALSE;
}
for (int i = 0; i < g->numVertexes; i++)
{
if (!visited[i])
{
visited[i] = TRUE;
cout <<"{ "<<g->adjList[i].data << " ";
while (!empty(q))
{
int m;
m = deleteQuence(q);
while (p)
{
{
}
p = p->next;
}
}
cout << "}"<<endl;
}
}
}

int main()
{
GraphList g;
createGraph(&g);
//printGraph(&g);
DFSTraverse(&g);
BFSTraverse(&g);
return 0;
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
05-21 4735
12-25
11-26 1万+
01-02
11-19
04-25 139
05-26 4494
07-11 2786
11-24 6378
07-26 685

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。