邻接表
来源:互联网 发布:python split去掉空格 编辑:程序博客网 时间:2024/05/16 07:49
#include <iostream>
#include <string>
#include <new>
#include <deque>
using namespace std;
typedef char POINTTYPE;
#define SAFE_DELETE(a) {delete a;a = NULL;}
#define SAFE_DELETEARRAY(a) {delete[] a;a = NULL;}
#define MAXPOINTSIZE 512
struct Edge
{
int index;
Edge *next;
Edge(){CleanUp();}
~Edge()
{
while (next)
{
Edge * tempPoint = next;
next = next->next;
delete tempPoint;
tempPoint = NULL;
}
}
void CleanUp()
{
memset(this,0,sizeof(Edge));
}
};
struct Point
{
POINTTYPE info;
Edge * first;
Edge * last;
Point(){CleanUp();}
~Point(){
SAFE_DELETE(first);
last = NULL;
}
void CleanUp()
{
memset(this,0,sizeof(Point));
}
};
struct Graph
{
Point* m_Point;
int PointSize;
int EdgeSize;
bool HasDirected;
bool HasWeight;
Graph(){CleanUp();}
~Graph(){
if (PointSize >1)
{
SAFE_DELETEARRAY(m_Point);
}else if (PointSize == 1)
{
SAFE_DELETE(m_Point);
}
}
void CleanUp()
{
memset(this,0,sizeof(Graph));
}
};
Graph * CreateGraph(bool hasDirected =false,bool hasWeight = false)throw()
{
Graph *m_graph = new(nothrow) Graph;
if (!m_graph)
{
cout<<"内存分配失败"<<endl;
return NULL;
}
cout<<"请输入点个数"<<endl;
cin>>m_graph->PointSize;
while (m_graph->PointSize <= 0)
{
cout<<"请输入一个正数"<<endl;
cin>>m_graph->PointSize;
}
m_graph->m_Point = new Point[m_graph->PointSize];
cout<<"请输入点信息以及相邻点,非正数结束"<<endl;
int tempInt;
Edge * tempEdge;
for (int i=0 ;i<m_graph->PointSize;i++)
{
cin>>m_graph->m_Point[i].info>>tempInt;
while (tempInt>=0)
{
if (!m_graph->m_Point[i].last)
{
m_graph->m_Point[i].last = new Edge;
m_graph->m_Point[i].last->index = tempInt;
}else
{
tempEdge = new Edge;
tempEdge->index = tempInt;
m_graph->m_Point[i].last->next = tempEdge;
m_graph->m_Point[i].last = tempEdge;
}
if (!m_graph->m_Point[i].first)
{
m_graph->m_Point[i].first = m_graph->m_Point[i].last;
}
cin>>tempInt;
}
}
return m_graph;
}
void print(Graph * m_graph)
{
Edge *tempPoint;
for (int i = 0 ;i<m_graph->PointSize;i++)
{
cout<<m_graph->m_Point[i].info;
tempPoint = m_graph->m_Point[i].first;
while (tempPoint)
{
cout<<" "<<tempPoint->index;
tempPoint = tempPoint->next;
}
cout<<endl;
}
}
void delphExtend(Graph * m_graph,int index ,bool *visited)
{
cout<<"访问"<<m_graph->m_Point[index].info<<endl;
visited[index] = true;
Edge * tempPoint = m_graph->m_Point[index].first;
int visitedIndex = -1;
while (tempPoint)
{
if (!visited[tempPoint->index])
{
visitedIndex = tempPoint->index;
break;
}
tempPoint = tempPoint->next;
}
if (visitedIndex != -1)
{
delphExtend(m_graph,visitedIndex,visited);
}
}
//深度优先遍历
void delph(Graph *m_graph)
{
bool visited[MAXPOINTSIZE];
memset(visited,0,sizeof(visited));
for (int i = 0;i<m_graph->PointSize;i++)
{
if (!visited[i])
{
delphExtend(m_graph,i,visited);
}
}
}
deque<int> m_queue;
//广度优先遍历(Breadth First Search)
void bfsExtend(Graph *m_graph,int index ,bool * visited)
{
cout<<"访问"<<m_graph->m_Point[index].info<<endl;
visited[index] = true;
Edge * tempPoint = m_graph->m_Point[index].first;
while (tempPoint)
{
if (!visited[tempPoint->index] && find(m_queue.begin(),m_queue.end(),tempPoint->index)== m_queue.end())
{
m_queue.push_back(tempPoint->index);
}
tempPoint = tempPoint->next;
}
if (m_queue.size()>0)
{
if (!visited[m_queue.front()])
{
bfsExtend(m_graph,m_queue.front(),visited);
}
m_queue.pop_front();
}
}
void bfs(Graph * m_graph)
{
bool visited[MAXPOINTSIZE];
memset(visited,0,sizeof(visited));
for (int i = 0; i < m_graph->PointSize; i++)
{
if (!visited[i])
{
bfsExtend(m_graph,i,visited);
}
}
}
int main()
{
Graph *a =CreateGraph();
delph(a);
cout<<"bfs"<<endl;
bfs(a);
print(a);
delete a;
return 0;
}
#include <string>
#include <new>
#include <deque>
using namespace std;
typedef char POINTTYPE;
#define SAFE_DELETE(a) {delete a;a = NULL;}
#define SAFE_DELETEARRAY(a) {delete[] a;a = NULL;}
#define MAXPOINTSIZE 512
struct Edge
{
int index;
Edge *next;
Edge(){CleanUp();}
~Edge()
{
while (next)
{
Edge * tempPoint = next;
next = next->next;
delete tempPoint;
tempPoint = NULL;
}
}
void CleanUp()
{
memset(this,0,sizeof(Edge));
}
};
struct Point
{
POINTTYPE info;
Edge * first;
Edge * last;
Point(){CleanUp();}
~Point(){
SAFE_DELETE(first);
last = NULL;
}
void CleanUp()
{
memset(this,0,sizeof(Point));
}
};
struct Graph
{
Point* m_Point;
int PointSize;
int EdgeSize;
bool HasDirected;
bool HasWeight;
Graph(){CleanUp();}
~Graph(){
if (PointSize >1)
{
SAFE_DELETEARRAY(m_Point);
}else if (PointSize == 1)
{
SAFE_DELETE(m_Point);
}
}
void CleanUp()
{
memset(this,0,sizeof(Graph));
}
};
Graph * CreateGraph(bool hasDirected =false,bool hasWeight = false)throw()
{
Graph *m_graph = new(nothrow) Graph;
if (!m_graph)
{
cout<<"内存分配失败"<<endl;
return NULL;
}
cout<<"请输入点个数"<<endl;
cin>>m_graph->PointSize;
while (m_graph->PointSize <= 0)
{
cout<<"请输入一个正数"<<endl;
cin>>m_graph->PointSize;
}
m_graph->m_Point = new Point[m_graph->PointSize];
cout<<"请输入点信息以及相邻点,非正数结束"<<endl;
int tempInt;
Edge * tempEdge;
for (int i=0 ;i<m_graph->PointSize;i++)
{
cin>>m_graph->m_Point[i].info>>tempInt;
while (tempInt>=0)
{
if (!m_graph->m_Point[i].last)
{
m_graph->m_Point[i].last = new Edge;
m_graph->m_Point[i].last->index = tempInt;
}else
{
tempEdge = new Edge;
tempEdge->index = tempInt;
m_graph->m_Point[i].last->next = tempEdge;
m_graph->m_Point[i].last = tempEdge;
}
if (!m_graph->m_Point[i].first)
{
m_graph->m_Point[i].first = m_graph->m_Point[i].last;
}
cin>>tempInt;
}
}
return m_graph;
}
void print(Graph * m_graph)
{
Edge *tempPoint;
for (int i = 0 ;i<m_graph->PointSize;i++)
{
cout<<m_graph->m_Point[i].info;
tempPoint = m_graph->m_Point[i].first;
while (tempPoint)
{
cout<<" "<<tempPoint->index;
tempPoint = tempPoint->next;
}
cout<<endl;
}
}
void delphExtend(Graph * m_graph,int index ,bool *visited)
{
cout<<"访问"<<m_graph->m_Point[index].info<<endl;
visited[index] = true;
Edge * tempPoint = m_graph->m_Point[index].first;
int visitedIndex = -1;
while (tempPoint)
{
if (!visited[tempPoint->index])
{
visitedIndex = tempPoint->index;
break;
}
tempPoint = tempPoint->next;
}
if (visitedIndex != -1)
{
delphExtend(m_graph,visitedIndex,visited);
}
}
//深度优先遍历
void delph(Graph *m_graph)
{
bool visited[MAXPOINTSIZE];
memset(visited,0,sizeof(visited));
for (int i = 0;i<m_graph->PointSize;i++)
{
if (!visited[i])
{
delphExtend(m_graph,i,visited);
}
}
}
deque<int> m_queue;
//广度优先遍历(Breadth First Search)
void bfsExtend(Graph *m_graph,int index ,bool * visited)
{
cout<<"访问"<<m_graph->m_Point[index].info<<endl;
visited[index] = true;
Edge * tempPoint = m_graph->m_Point[index].first;
while (tempPoint)
{
if (!visited[tempPoint->index] && find(m_queue.begin(),m_queue.end(),tempPoint->index)== m_queue.end())
{
m_queue.push_back(tempPoint->index);
}
tempPoint = tempPoint->next;
}
if (m_queue.size()>0)
{
if (!visited[m_queue.front()])
{
bfsExtend(m_graph,m_queue.front(),visited);
}
m_queue.pop_front();
}
}
void bfs(Graph * m_graph)
{
bool visited[MAXPOINTSIZE];
memset(visited,0,sizeof(visited));
for (int i = 0; i < m_graph->PointSize; i++)
{
if (!visited[i])
{
bfsExtend(m_graph,i,visited);
}
}
}
int main()
{
Graph *a =CreateGraph();
delph(a);
cout<<"bfs"<<endl;
bfs(a);
print(a);
delete a;
return 0;
}
阅读全文
0 0
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- 邻接表
- LeetCode@Tree_606_Construct_String_from_Binary_Tree
- 【旧资料整理】php 变量前面加@的含意
- OptionsMenu创建使用
- 二叉搜索树的后序遍历
- 假设复制源目录 为 web ,目标目录为oj。怎样才能将web下所有文件复制到oj下了 -rf 不删除 别名 alias
- 邻接表
- 【旧资料整理】php mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc,msql_fetch_object的区别
- iptables详解
- 欢迎使用CSDN-markdown编辑器
- 【旧资料整理】C++ 关于读入文件出错
- 随机森林算法(有监督学习)
- ssm框架整合及Rest风格接口实现
- Maximum Distance in Arrays (第十七周 数组)
- linux 扫描wifi