存一下先

This commit is contained in:
iridiumR 2021-12-09 22:58:46 +08:00
parent b66d7daea8
commit 13577eb681
4 changed files with 325 additions and 1 deletions

View file

@ -65,7 +65,11 @@ private:
void merge(int lo, int mi, int hi);
};
template <class T>
int Vector<T>::getused()
{
return _used;
}
template <class T>

7
ex11/ex11.cpp Normal file
View file

@ -0,0 +1,7 @@
#include "graph.hpp"
int main()
{
graphVertex<int,std::string> gv;
return 0;
}

155
ex11/graph.hpp Normal file
View file

@ -0,0 +1,155 @@
#ifndef _graph_hpp_
#define _graph_hpp_
#include "vector.hpp"
#include <iostream>
#include <string>
// #define gNodeArray(T) binNode<T> *
template <class Te>
class Edge
{
public:
Te data;
int weight;
Edge()
{
weight = 0;
}
Edge(int w)
{
weight = w;
}
Edge(Te d, int w)
{
data = d;
weight = w;
}
};
template <class Tv>
class Vertex
{
public:
Tv data;
int inDegree;
int outDegree;
Vertex()
{
// data = NULL;
inDegree = 0;
outDegree = 0;
}
Vertex(Tv d)
{
data = d;
inDegree = 0;
outDegree = 0;
}
};
template <class Te, class Tv>
class graphVertex
{
private:
Vector<Vertex<Tv> *> V;
Vector<Vector<Edge<Te> *>> E;
int v;
int e;
public:
graphVertex()
{
v = 0;
e = 0;
}
~graphVertex()
{
int n = v;
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
delete E[j][k];
for (int j = 0; j < n; j++)
delete V[j];
}
virtual Tv Vertex(int i)
{
return V[i]->data;
}
virtual int inDegree(int i)
{
return V[i]->inDegree;
}
virtual int outDegree(int i)
{
return V[i]->outDegree;
}
virtual int insect(int id, Tv value)
{
if (V[id] == NULL)
{
V[id] = new ::Vertex<Tv>(value);
// E[id] = new Vector<Edge<Te>>;
}
else
V[id]->data = value;
v++;
return id;
}
virtual int remove(int id)
{
for (int j = 0; j < v; j++)
{
if (E[id][j] != NULL)
{
delete E[id][j];
E[id][j] = NULL;
V[j]->inDegree--;
}
}
v--;
Tv temp = Vertex(id);
delete V[id];
V[id] = NULL;
}
// id1->id2 进行连接
// return
// -1 顶点不存在
// -2 边已存在
// 0 成功
virtual int link(int id1, int id2, int w)
{
if (V[id1] == NULL || V[id2] == NULL)
return -1;
if (E[id1][id2] != NULL)
return -2;
V[id1]->outDegree++;
V[id2]->inDegree++;
E[id1][id2] = new Edge<Te>(w);
return 0;
}
// 解除 id1->id2 连接
// return
// -1 顶点不存在
// -2 边不存在
// 0 成功
virtual int unlink(int id1, int id2, int w)
{
if (V[id1] == NULL || V[id2] == NULL)
return -1;
if (E[id1][id2] != NULL)
return -2;
V[id1]->outDegree--;
V[id2]->inDegree--;
delete E[id1][id2];
return 0;
}
};
#endif

158
ex11/vector.hpp Normal file
View file

@ -0,0 +1,158 @@
#ifndef _VECTOR_HPP_
#define _VECTOR_HPP_
#include <iostream>
template <class T>
class Vector
{
public:
T *_v;
int _len;
int _used;
bool _sorted = false;
void expand();
void shrink();
void adjust();
Vector(int len)
{
_used = 0;
_len = len;
_v = new T[_len];
for (int i = 0; i < _len; i++)
{
_v[i] = NULL;
}
}
Vector()
{
_used = 0;
_len = 10;
_v = new T[_len];
}
~Vector()
{
delete[] _v;
}
T get(int a) { return _v[a]; }
T operator[](int i)
{
while (i >= _len)
{
expand();
}
return _v[i];
}
//排序搜索,返回秩
int search(T value);
//某处更改为某值
void put(int a, T value);
void swap(int a, int b);
int insert(int locate, int value);
int remove(int locate, int value);
int remove_sorted(int value);
int find(int value);
void bubbleSort();
void printall();
int getlen();
int getused();
int count(int value);
void mergeSort(int lo, int hi);
private:
void merge(int lo, int mi, int hi);
};
template <class T>
void Vector<T>::put(int a, T value)
{
while (i >= _len)
{
expand();
}
_used++;
_v[a] = value;
}
template <class T>
int Vector<T>::getused()
{
return _used;
}
template <class T>
void Vector<T>::expand()
{
int old_len = _len;
_len = _len * 2;
T *p = new T[_len];
for (int i = 0; i < old_len; i++)
p[i] = _v[i];
for (int i = old_len; i < _len; i++)
{
p[i] = NULL;
}
delete[] _v;
_v = p;
}
// template <class T>
// void Vector<T>::shrink()
// {
// _len = _len >> 1;
// T *p = new T[_len];
// for (int i = 0; i < _used; i++)
// p[i] = _v[i];
// for (int i = _used; i < _len; i++)
// {
// p[i] = NULL;
// }
// delete[] _v;
// _v = p;
// }
// template <class T>
// void Vector<T>::adjust()
// {
// if (((double)_used / (double)_len) < 0.25)
// shrink();
// else if (((double)_used / (double)_len) > 0.75)
// expand();
// return;
// }
template <class T>
int Vector<T>::search(T e)
{
if (_sorted == false)
return -1;
int i = 0;
for (; i <= _used && _v[i] <= e; i++)
;
return i - 1;
}
#endif