-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vertex.h
84 lines (78 loc) · 2.25 KB
/
Vertex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef VERTEX_H
#define VERTEX_H
#ifndef NULL
#define NULL 0
#endif
#include "Edge.h"
#include <cstring>
class Vertex
{
private:
int m_key;// the key of this vertex
int m_size;// the number of the edges from this vertex to others
char* m_storeName;
Edge* m_pEHead;// the head pointer for the linked list of the edges
Vertex* m_pNext;// the next pointer for the linked list of the vertics
public:
Vertex() {
m_key = 0;
m_size = 0;
m_storeName = new char[100];
m_pEHead = NULL;
m_pNext = NULL;
}
Vertex(int key, char* name) {
m_key = key;
m_size = 0;
m_storeName = new char[100];
strcpy(m_storeName, name);
m_pEHead = NULL;
m_pNext = NULL;
}
~Vertex() {
Clear();
}
/// set the next pointer of this vertex
void SetNext(Vertex* pNext) { this->m_pNext = pNext; }
void SetSize(int size) { this->m_size = size; }
void SetHead(Edge* newhead) { this->m_pEHead = newhead; }
/// get the key of this vertex
int GetKey() const { return this->m_key; }
// the next pointer of this vertex
Vertex* GetNext() const { return this->m_pNext; }
char* GetName() const { return this->m_storeName; }
// the number of the edges
int Size() const { return m_size; }
// add edge with edgeNum at the end of the linked list for the edges
void AddEdge(int edgeKey, int weight) // the key of the vertex for the edge// the weight of the edge
{
Edge* NewEdge = new Edge(edgeKey, weight);
if (this->GetHeadOfEdge() == NULL)
{//first add
this->m_pEHead = NewEdge;//set head
}
else
{
Edge* pCur = this->m_pEHead;
while (pCur->GetNext() != NULL)
pCur = pCur->GetNext();
pCur->SetNext(NewEdge);//set next
}
}
// get the head pointer of the edge
Edge* GetHeadOfEdge() const { return this->m_pEHead; }
/// <summary>
/// memory free for edges
/// </summary>
void Clear()
{
Edge* moveE = this->m_pEHead;
while (m_pEHead != NULL)
{
moveE = m_pEHead;
m_pEHead = m_pEHead->GetNext();
delete moveE;//delete Edge
}
}
};
#endif