RN
Size: a a a
RN
Р
Р
Р
Р
RN
RN
RN
Р
//
// Created by Akhzulag on 26.02.2021.
//
#ifndef OOPGRAPH_GRAPH_H
#define OOPGRAPH_GRAPH_H
#include <iostream>
#include <vector>
#include <set>
#include "Node.h"
#include <istream>
#include <ostream>
using namespace std;
template<class T>
class Graph
{
public:
~Graph();
bool insertEdge(T from,T to);
void deleteVertex(T numberV);
Graph(T *vertexs,const int &count);
Graph(const Graph ©);
Graph(){}
void printlinkVertex(T V);
void printGraph();
bool insertVertex(T a);
vector<node<T>*> getNodes() const;
vector<T> getVertexs() const;
void DFS(T v);
Graph& operator = (const Graph &A)
{
if(this==&A)
{
return *this;
}
countVer=A.countVer;
vertexs=A.vertexs;
nodes=A.nodes;
visited=A.visited;
return *this;
}
friend ostream& operator <<(ostream& s, const Graph<T>& A);
friend istream& operator >>(istream& s, Graph<T>& A);
/*
void operator [] (const T& v)
{
vector<T>::iterator indexV=find(vertexs.begin(),vertexs.end(),v);
if(v==vertexs[indexV-vertexs.begin()])
{
for(set<T>::iterator it=nodes[indexV-vertexs.begin()]->adjacencyList.begin();it!=nodes[indexV-vertexs.begin()]->adjacencyList.end();++it)
{
cout<<(*it)<<' ';
}
}
}*/
void cc()
{
cout<<"GG";
}
private:
bool edge(T from,T to);
void privatDFS(T v);
int countVer=0;
vector<bool> visited;
vector<node<T>*> nodes;
vector<T> vertexs;
};
template<class T>
Graph<T>::~Graph()
{
for(int i=0;i<nodes.size();++i)
{
delete nodes[i];
}
nodes.clear();
}
template<class T>
Graph<T>::Graph(T *vertexs,const int &count)
{
for(int i=0;i<count;++i)
{
node<T>* tmpNode=new node<T>(vertexs[i]);
nodes.push_back(tmpNode);
this->vertexs.push_back(vertexs[i]);
visited.push_back(false);
}
countVer=count;
}
template<class T>
Graph<T>::Graph(const Graph ©)
{
vertexs=copy.getVertexs();
nodes=copy.getNodes();
}
template<class T>
vector<T> Graph<T>::getVertexs() const
{
return vertexs;
}
template<class T>
vector<node<T>*> Graph<T>::getNodes() const
{
return nodes;
}
template<class T>
istream& operator >> (istream& in,Graph<T>& A)
{
while(true)
{
A.vertexs.clear();
A.visited.clear();
A.nodes.clear();
int countV;
cout<<"Enter count of vertex\n";
in>>countV;
if (in.fail())
{
cout<<"Введення невірне,спробуйте знову\n";
in.clear();
in.ignore();
continue;
}
A.countVer=countV;
cout<<"Enter vertex\n";
vector<T> vertex;
for(int i=0;i<countV;++i)
{
T a;
in>>a;
vertex.push_back(a);
}
if (in.fail())
{
cout<<"Введення невірне,спробуйте знову\n";
in.clear();
in.ignore(INT_MAX,'\n');
continue;
}
vector<node<T>*> nodes;
vector<bool> visited;
for(int i=0;i<countV;++i)
{
node<T>* tmpNode=new node<T>(vertex[i]);
nodes.push_back(tmpNode);
visited.push_back(false);
}
A.nodes=nodes;
A.visited=visited;
A.vertexs=vertex;
cout<<"Вводьте звязки між ввершинами, якщо захочете припинити ввдення введіть -1\n";
while(true)
{
T a,b;
in>>a;
if(a==-1 or a=='-' or a=="-1") break;
in >> b;
if(b==-1) break;
if(a>=0 && b>=0)
{
if(A.insertEdge(a,b))
{
cout<<"Ребро додано\n";
}else
{
cout<<"Не вірний запис вершин спробуте ще раз\n";
}
}
}
if (in.fail())
{
cout<<"Введення невірне,спробуйте знову\n";
Р
Р
Р
G
VD