图的具体操作(C++ 数据结构)
2015-01-01 20:58
211 查看
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE 100
using namespace std;
#ifndef GRAPHICS_H
#define GRAPHICS_H
struct EdgeNode
{
int
adjvex;
EdgeNode
*next;
};//定义邻接表的边节点类型
//定义邻接表类型
typedef EdgeNode **ADJLIST;
//对图操作的主菜单
void MainMenue();
//初始化邻接表
void InitialAdjList(ADJLIST &GL, int n);
//以文件方式输入图
//bool InputGraphics();
//建立图的邻接表
void CreatAdjList(ADJLIST &GL, int
&n);
//建立图的邻接矩阵
void CreatAdjMatrix(ADJLIST &GL, int
&n);
//从初始点出发深度优先搜索由邻接表GL表示的图
void DFSAdjList(ADJLIST GL, bool *&visited, int i,
int n);
//从初始点出发广度优先搜索由邻接表GL表示的图
void BFSAdjList(ADJLIST GL, bool *&visited, int i,
int n);
#endif
//部分函数原型声明
void Check(int n, int &i, int
&j);
void InitialAdjList(ADJLIST &GL, int n);
//全局变量,控制递归函数中提示符的输出
int flag = 1;
void MainMenue()
//对图操作的主菜单
{
cout<<"\n***************************************"<<endl;
cout<<"**
T923-1
Lily
**"<<endl;
cout<<"**
1.建立图的邻接表。
**"<<endl;
cout<<"**
2.建立图的邻接矩阵。
**"<<endl;
cout<<"**
3.深度优先遍历图。
**"<<endl;
cout<<"**
4.广度优先遍历图。
**"<<endl;
cout<<"**
5.退出。
**"<<endl;
cout<<"**
**"<<endl;
cout<<"************ fulme
********************"<<endl;
}
void InitialAdjList(ADJLIST &GL, int n)
//初始化图的邻接表
{
GL = new
EdgeNode*
;
for (int i =
1; i <= n; i++)
GL[i] = NULL;
}
void CreatAdjList(ADJLIST &GL, int
&n)
//建立图的邻接表
{
FILE
*in1;
FILE
*out1;
int
i,j,k,b;
char
ch;
int flag =
1;
//建立无向图的邻接表
if ((in1 = fopen("T923-1-888-Lily-Graphics.txt", "rb")) ==
NULL)
{
cout<<"打开
T923-1-888-Lily-Graphics.txt
失败!"<<endl;
exit(1);
}
if ((out1 = fopen("T923-1-888-Lily-adjlist1.txt", "wb")) ==
NULL)
{
cout<<"打开
T923-1-888-Lily-adjlist1.txt
失败!"<<endl;
flag = 1;
}
//读入顶点的个数, ",", 边数
fscanf(in1, "%d", &n);
fscanf(in1, "%c", &ch);
fscanf(in1, "%d", &b);
for (k = 0; k < b; k++)
{
fscanf(in1, "%d", &i);
fscanf(in1, "%c", &ch);
fscanf(in1, "%d", &j);
Check(n, i, j);
//向序号为i的单链表的表头插入一个边结点
EdgeNode *p = new EdgeNode;
p -> adjvex = j;
p -> next = GL[i];
GL[i] = p;
//向序号为j的单链表的表头插入一个节点
p = new EdgeNode;
p -> adjvex = i;
p -> next = GL[j];
GL[j] = p;
}
//输出邻接表,并保存到adjlist1.txt中
cout<<endl<<"\n====================="<<endl;
cout<<"无向图的邻接表为:"<<endl;
for (i = 1; i <= n; i++)
{
EdgeNode *p = GL[i];
cout<<i -
1<<"
|"<<"V"<<i;
fprintf(out1, "%c", 'V');
fprintf(out1, "%d", i);
for (p = GL[i]; p != NULL; p = p -> next)
{
cout<<"|-|->|"<<p
-> adjvex;
fprintf(out1, "%s", "|-|->|");
fprintf(out1, "%d", p -> adjvex);
}
cout<<"|^|"<<endl;
fprintf(out1, "%s", "|^|");
fprintf(out1, "\r\n");
}
if (flag)
cout<<"无向图的邻接表已保存到T923-1-888-Lily-adjlist1.txt中"<<endl;
fclose(in1);
fclose(out1);
}
void CreatAdjMatrix(ADJLIST &GL, int
&n)
//建立图的邻接矩阵
{
int
matrix[MAX_SIZE + 1][MAX_SIZE + 1];
FILE
*in1;
FILE
*out1;
int
i,j,k,b;
char
ch;
int flag =
0;
//初始化图的邻接矩阵
for (i = 1;
i <MAX_SIZE; i++)
{
for (j = 1; j <MAX_SIZE; j++)
{
matrix[i][j] = 0;
}
}
//建立无向图的邻接矩阵
if ((in1 = fopen("T923-1-888-Lily-Graphics.txt", "rb")) ==
NULL)
{
cout<<"打开
T923-1-888-Lily-Graphics.txt
失败!"<<endl;
exit(1);
}
if ((out1 = fopen("T923-1-888-Lily-adjlmatrix1.txt", "wb")) ==
NULL)
{
cout<<"打开
T923-1-888-Lily-adjmatrix1.txt
失败!"<<endl;
flag = 1;
}
//读入顶点的个数, ",", 边数
fscanf(in1, "%d", &n);
fscanf(in1, "%c", &ch);
fscanf(in1, "%d", &b);
for (k = 0; k < b; k++)
{
fscanf(in1, "%d", &i);
fscanf(in1, "%c", &ch);
fscanf(in1, "%d", &j);
Check(n, i, j);
matrix[i][j] = matrix[j][i] = 1;
}
cout<<"\n================================="<<endl;
cout<<"无向图的邻接矩阵为:\n";
for (i = 1; i <=n; i++)
{
for (j = 1; j <=n; j++)
{
cout<<matrix[i][j]<<'
';
fprintf(out1, "%d ", matrix[i][j]);
}
cout<<endl;
fprintf(out1, "\r\n");
}
cout<<"无向图的邻接矩阵已保存到T923-1-888-Lily-adjmatrix1.txt中!"<<endl;
fclose(in1);
fclose(out1);
}
void DFSAdjList(ADJLIST GL, bool *&visited, int i,
int n)
//从初始点出发递归深度优先搜索邻接表GL表示的图
{
if
(flag)
{
flag = 0;
cout<<"\n================================="<<endl;
cout<<"\n图的深度优先遍历序列:"<<endl;
}
cout<<i<<'
';
visited[i] =
true;
EdgeNode *p
= GL[i];
while (p
!= NULL)
{
int j = p -> adjvex; //j为Vi的一个邻接点的序号
if (!visited[j])
DFSAdjList(GL, visited, j, n);
p = p -> next;
}
}
void BFSAdjList(ADJLIST GL, bool *&visited, int i,
int n)
//从初始点开始广度优先搜索邻接表GL表示的图
{
int j;
const int
MaxLength = 100;
//定义一个队列q, 其元素类型为整型
int
q[MaxLength] = {0};
//定义队首和对尾指针
int front =
0, rear = 0;
cout<<"\n================================="<<endl;
cout<<"\n图的广度优先遍历序列:"<<endl;
for (j = 1;
j <= n; j++)
visited[j] = false;
cout<<i<<'
';//访问Vi
visited[i] =
true;
q[++rear] =
i;
while
(front != rear)
//当队列非空时进行循环处理
{
//删除队首元素,第一次执行时k的值为i
front = (front + 1) % MaxLength;
int k = q[front];
//取邻接表的表头指针
EdgeNode *p = GL[k];
while (p != NULL)
//依次搜索Vk的每个节点
{
//Vj为Vk的一个邻接节点
int j = p -> adjvex;
if (!visited[j])
{
cout<<j<<'
';
visited[j] = true;
rear = (rear + 1) % MaxLength;
q[rear] = j;
}
p = p -> next;
}
}
cout<<endl;
}
void Check(int n, int &i, int
&j)
//检查输入的边序号是否越界,如越界择重输入
{
while
(1)
{
if (!(i >= 0 && j
<= n && j
>= 0 && j
<= n))
{
cout<<"\n输入有误!"<<endl;
exit(1);
}
break;
}
}
int main()
{
int i;
int n;
int ch;
bool
*visited = new bool[MAX_SIZE];
ADJLIST
gl;
//初始化邻接表
InitialAdjList(gl, MAX_SIZE);
while
(1)
{
MainMenue();
cout<<"请输入你的选择: ";
cin>>ch;
switch (ch)
{
case 1:
system("cls");
CreatAdjList(gl, n);
break;
case 2:
system("cls");
CreatAdjMatrix(gl, n);
break;
case 3:
system("cls");
for (i = 1; i <= n; i++)
visited[i] = false;
DFSAdjList(gl, visited, 1, n);
break;
case 4:
system("cls");
BFSAdjList(gl, visited, 1, n);
break;
case 5:
exit(1);
default:
cout<<"输入有误!"<<endl;
break;
}
}
return
0;
}
相关文章推荐
- ADO+Visual&nbsp;C++数据库连接和操作
- 把Python嵌入C++的具体操作方案的介绍
- C/C++中对日期和时间操作所用到的数据结构和函数
- c++日期和时间相关操作-<time.h>(ctime)
- 在c++中&nbsp;struct&nbsp;和class
- C++学习笔记之对文件的操作<1>
- 图的相关操作【C++数据结构】
- C++&nbsp;:&nbsp;Complex&nbsp;numbers&nbsp;library
- [C++]数据结构:有序链表SortedChain的基本实现与操作
- mysql&nbsp;操作总结&nbsp;INSERT和REPLACE
- c++&nbsp;try&nbsp;catch&nbsp;throw&nbsp;用法
- 数据结构_队列_用链表动态建立释放节点实现队列各种操作_C++实现
- C++ 文件操作 &检查文件尾 &读取模式
- [C++ 学习] C++ Primer 第4版 习题 4.30 字符串操作 & 我对堆内存 和 常量区内存的一些理解
- 用 Eclipse 平台进行 C/C++ 开发
- 链表的相关操作查找插入删除(c++ 数据结构)
- char, wchar_t, TCHAR 字符全总结 & c++字符串操作经验集
- [原]eclipse 编译c/c++环境搭建问…
- hadoop&nbsp;streaming的单词统计C++版
- python&nbsp;中用SWIG包装C和C++和接口…