数据结构之图_Graph_List

本文介绍了图数据结构的链表实现,包括顶点和边的定义,以及图的创建、权值获取、插入和删除边、深度优先搜索、广度优先搜索等操作。此外,还提供了Dijkstra最短路径算法的实现。
摘要由CSDN通过智能技术生成
#ifndef GRAPH_LIST_H_INCLUDED
#define GRAPH_LIST_H_INCLUDED
#include<iostream>
#include<queue>
#include<stack>
using std::cout;
using std::cin;
using std::endl;

struct Edge
{
 friend class Graph_List;
 Edge(int ver, int w, Edge* e = NULL){ VerAdj = ver; cost = w; link = e; }
 int VerAdj;/*链接顶点序列,从0开始编号*/
 int cost;/*边的权值*/
 Edge* link;/*指向下一个节点的指针*/

};

struct Vertex
{
 friend class Graph_List;
 int VerName;/*顶点的头指针*/
 Edge* adjacent;/*边链表的头指针*/
};


class Graph_List
{
private:
 Vertex *Head;/*顶点链表的头指针*/
 int graphsize;/*图中当前顶点的个数*/
 int MaxGraphSize;
public:
 Graph_List(int MaxSize = 100) :MaxGraphSize(MaxSize),Head(NULL){};
 ~Graph_List();
 void CreatGraph();/*图的构建*/
 int GetWeight(const int& v1, const int& v2);/*返回权值*/
 void InsertEdge(const int& v1, const int& v2);/*插入边*/
 void DeleEdge(const int& v1, const int& v2);/*删除边*/
 int GetFirstNeighbor(const int &v);/*顶点v的第一个链接顶点*/
 int GetNextNeighbor(const int& v1, const int& v2);/*顶点v的下一个个链接顶点*/
 void REDF(const int& v, int* visited);/*深度遍历的递归实现*/
 void DepthFirstSearch();/*深度遍历*/
 void BFS(const int& s);/*广度遍历*/
 void Show();/*测试函数,show图的边及顶点*/
 void DShortestPath(const int& v);/*Djkstra算法*/

};

void Graph_List::CreatGraph()
{
 int e;
 int from, to, weight;
 Head = new Vertex[MaxGraphSize];
 cout << "请输入顶点个数:";
 cin >> graphsize;
 for (int i = 0; i<graphsize; i++)
 {
  Head[i].VerName = i;
  Head[i].adjacent = NULL;
 }
 cout << "请输入边的条数:";
 cin >> e;
 for (int i = 0; i<e; i++)
 {
  cout << "请输入新边的始点ÿ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值