【C++数据结构实验】图论相关内容,链式实现图的存储及基本操作,深度优先和广度优先图的遍历及拓扑排序!

数据结构图存储相关程序

前面部分为实验说明部分,最终代码查看文末即可!

**任务一:**基于邻接表存储结构实现有向图的典型操作(构造、析构、增加顶点、删除顶点、增加弧、删除弧,查找一个顶点、判空、判满、图中顶点个数、邻接表中指定顶点的第一个邻接顶点、深度优先遍历、广度优先遍历),测试和调试程序。
邻接表示意图如下所示:
图示
根据以上的示意图,声明本实验所用到的图类Graph,请仔细阅读下面所给的graph.h 文件,按照注释分析各个方法的作用。
自己编写graph.cpp 文件,实现Graph 类中各个方法(构造、析构、增加顶点、删除顶点、增加弧、删除弧,查找一个顶点、判空、判满、图中顶点个数、邻接表中指定顶点的第一个邻接顶点、深度优先遍历、广度优先遍历)。

---------------------
graph.h 文件
--------------------
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
/* This header file contains the declarations and prototype functions for the graph ADT
class. It also contains a debugging function to print a vertex.
*/
#define DEBUG 1 //On includes printVertex
template <class TYPE> struct Vertex;//存储每个顶点的信息
template <class TYPE> struct Arc;//先简单声明,存储弧的信息
template <class TYPE> struct Vertex //存图中各顶点
{
   
Vertex<TYPE> *pNextVertex;//指向下一个头顶点
TYPE data;//顶点有效信息
int inDegree;//入度
int outDegree;//出度
short processed;//标识该顶点被处理的程度或状态
Arc<TYPE> *pArc; //指向第一条弧,这里用到了Arc,所以在前面要先简单声明下
}; // Vertex
template <class TYPE> struct Arc //存图中各弧
{
   
Vertex<TYPE> *destination;//注意这里是指针,指向弧头顶点
Arc<TYPE> *pNextArc;//指向下一条弧
}; // Arc
template <class TYPE, class KTYPE> //KTYPE 表示各顶点中关键字的类型(关键字用于查询)
class Graph
{
   
private:
int count;
Vertex<TYPE> *first;
public:
Graph(void);
~Graph(void);
int insertVertex(TYPE dataIn);//插入顶点,数据域为dataIn
int deleteVertex(KTYPE dltKey);//删除顶点,其数据域中关键字为dltKey
int insertArc(KTYPE fromKey, KTYPE toKey);//插入弧,该弧从fromKey 指向toKey
int deleteArc(KTYPE fromKey, KTYPE toKey);//删除弧,该弧从fromKey 指向toKey
int retrieveVertex (KTYPE key, TYPE& DataOut);//查关键字为key 的顶点,数据存入DataOut
int firstArc(KTYPE key, TYPE& DataOut);//查关键字为key 的顶点所指的首弧,数据存DataOut
bool emptyGraph(void);//叛图是否为空
bool graphFull(void);//叛图是否为满
int graphCount(void); //返回图顶点个数
void depthFirst (void (*process)(TYPE dataProc));//深度优先遍历
void breadthFirst (void (*process)(TYPE dataProc));/广度优先遍历
#ifdef DEBUG
// The following function is used only for debugging
void printVertex (KTYPE key);//该方法仅在调试时使用,输出该顶点关键字
#endif
}; // Graph
#endif // GRAPH_H_INCLUDED

完成上述程序后,可以执行下面的测试程序,检验自己的图类Graph 是否运行正常。

/* This program tests the graph ADT */
#include <iostream.h>
#include <iomanip.h>
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>
struct DATA
{
   
int key;
} ;
#include "graph.h"
//Prototype Functions
void doInsertVertex(Graph<DATA, int>& graph);
void doInsertArc(Graph<DATA, int>& graph);
void doDeleteVertex(Graph<DATA, int>& graph);
void doDeleteArc(Graph<DATA, int>& graph);
void search(Graph<DATA, int>& graph);
void doRetrieveVertex(Graph<DATA, int>& graph);
void doFirstArc(Graph<DATA, int>& graph);
void testUtilties(Graph<DATA, int>& graph);
void buildAGraph(Graph<DATA, int>& graph);
char doMenu(void);
void process(DATA data);
int main (void)
{
    // Local Declarations
char option;
int key;
DATA datum;
Graph<DATA, int> graph;
// Statements
cout << "Start Explore Graphs\n";
while ((option = doMenu ()) != 'q')
{
   
switch (option)
{
   
case 'i' : doInsertVertex(graph); break;
case 'd' : doDeleteVertex(graph); break;
case 'a' : doInsertArc(graph); break;
case 'b' : doDeleteArc(graph); break;
case 'p' : cout << "Enter vertex key: ";
cin >> key;
graph.printVertex(key);
break;
case 'r' : doRetrieveVertex(graph); break;
case 's' : doFirstArc(graph); break;
case 't' : datum.key = INT_MIN;
process (datum);//Set count
cout << "\nBegin depth first traversal\n";
graph.depthFirst (process);
cout << "\nEnd depth first traversal\n";
cout << "\nBegin breadth first traversal\n";
process (datum); //Set count
graph.breadthFirst (process);
cout << "\nEnd breadth first traversal\n";
break;
case 'x' : testUtilties (graph); break;
case '!' : buildAGraph (graph); break;
default : cout << "\a\a\nInvalid option. Please try again.\n";
} // switch
} // while
return 0;
cout << "End Explore Graphs\n";
} // main
/* ============================= doMenu =============================
This function prints a menu and reads the user selection.
Pre Nothing.
Post option returned.
*/
char doMenu(void)
{
    // Local Declarations
charoption;
// Statements
cout << "\n============ Menu =============\n" ;
cout << " i : Insert new item \n";
cout << " d : Delete a vertex \n";
cout << " a : Insert an arc \n";
cout << " b : Delete an arc \n";
cout << " p : Print Vertex Adjacency \n";
cout << " r : Retrieve Vertex \n";
cout << " s : Retrieve First Arc \n";
cout << " t : Traverse graph \n";
cout << " x : Test ADT utilities \n";
cout << " q : Quit \n\n";
cout << "=============================== \n";
cout << "Please enter your choice: ";
cin >> option;
option = tolower (option);
return option;
} // doMenu
/* =================== doRetrieveVertex ===================
This function locates a vertex in the graph.
Pre graph exists.
Post vertex retrieved and printed.
*/
void doRetrieveVertex (Graph<DATA, int>& graph)
{
    // Local Declarations
int key;
int success;
DATA dataOut;
// Statements
cout << "Please enter key to be found: ";
cin >> key;
success = graph.retrieveVertex (key, dataOut);
if (success == 1)
cout << "\nKey found. Contains " << dataOut.key << endl;
else
cout << "\n\aKey " << key << " not found\n";
return;
} // doRetrieveVertex
/* =================== doFirstArc ===================
This function locates the first arc of a vertex.
Pre graph exists.
Post vertex first arc retrieved and printed.
*/
void doFirstArc(Graph<DATA, int>& graph)
{
    // Local Declarations
int key;
int success;
DATA dataOut;
// Statements
cout << "Please enter key of first arc: ";
cin >> key;
success = graph.firstArc ( key, dataOut);
if (success == 1)
cout << "\nFirst arc of " << key << " contains " << dataOut.key;
else if (success == -2)
cout << "\n\aVertex Key " << key << " not found\n";
else
cout << key << " has no arcs\n";
return;
} // doFirstArc
/* ===================== doInsertVertex =====================
This function gets input from the user and passes it to
vertex insert vertex function.
Pre graph exists.
Post vertex has been inserted.
*/
void doInsertVertex (Graph<DATA, int>& graph)
{
    // Prototype Statements
// Local Declarations
DATA newData;
int success;
// Statements
cout << "\nPlease enter interger to be inserted: ";
cin >> newData.key;
success = graph.insertVertex (newData);
if (success)
cout << newData.key << 
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

輕塵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值