Rewriting graph in linked list

this is my code, i want to rewrite it in linked list rather than array bu not getting what to do.

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  #include"graphtype.h"
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
const int NULL_EDGE = 0;

template<class VertexType>
GraphType<VertexType>::GraphType()
{
  numVertices = 0;
  maxVertices = 50;
  vertices = new VertexType[50];
  edges = new int*[50];
  for(int i=0;i<50;i++)
	edges[i] = new int [50];
  marks = new bool[50];
}
template<class VertexType>
GraphType<VertexType>::GraphType(int maxV)
{
  numVertices = 0;
  maxVertices = maxV;
  vertices = new VertexType[maxV];
  edges = new int*[maxV];
  for(int i=0;i<maxV;i++)
	edges[i] = new int [maxV];
  marks = new bool[maxV];
}
template<class VertexType>
GraphType<VertexType>::~GraphType()
{
  delete [] vertices;
  delete [] marks;
  for(int i=0;i<maxVertices;i++)
	delete [] edges[i];
  delete [] edges;
}
template<class VertexType>
void GraphType<VertexType>::MakeEmpty()
{
    numVertices=0;
}
template<class VertexType>
bool GraphType<VertexType>::IsEmpty()
{
    return (numVertices==0);
}
template<class VertexType>
bool GraphType<VertexType>::IsFull()
{
    return (numVertices==maxVertices);
}
template<class VertexType>
void GraphType<VertexType>::AddVertex(VertexType vertex)
{
  vertices[numVertices] = vertex;

  for (int index = 0; index < numVertices; index++)
  {
    edges[numVertices][index] = NULL_EDGE;
    edges[index][numVertices] = NULL_EDGE;
  }
  numVertices++;
  
  
  
  
}






template<class VertexType>
int IndexIs(VertexType* vertices, VertexType vertex)
{
  int index = 0;

  while (!(vertex == vertices[index]))
    index++;
  return index;
}
template<class VertexType>
void GraphType<VertexType>::ClearMarks()
{
    for(int i=0;i<maxVertices;i++)
        marks[i]=false;
}
template<class VertexType>
void GraphType<VertexType>::AddEdge(VertexType fromVertex,
     VertexType toVertex, int weight)
{
  int row = IndexIs(vertices, fromVertex);
  int col= IndexIs(vertices, toVertex);
  edges[row][col] = weight;
}
template<class VertexType>
int GraphType<VertexType>::WeightIs(VertexType fromVertex, VertexType toVertex)
{
  int row = IndexIs(vertices, fromVertex);
  int col= IndexIs(vertices, toVertex);

  return edges[row][col];
}

template<class VertexType>
void GraphType<VertexType>::GetToVertices(VertexType vertex, queue<VertexType>& adjVertices)
{
  int fromIndex;
  int toIndex;

  fromIndex = IndexIs(vertices, vertex);
  for (toIndex = 0; toIndex < numVertices; toIndex++)
    if (edges[fromIndex][toIndex] != NULL_EDGE)
      adjVertices.push(vertices[toIndex]);
}
template<class VertexType>
void GraphType<VertexType>::MarkVertex(VertexType vertex)
{
    int index=IndexIs(vertices,vertex);
    marks[index]=true;
}
template<class VertexType>
bool GraphType<VertexType>::IsMarked(VertexType vertex)
{
    int index=IndexIs(vertices,vertex);
    return marks[index];
}
template<class VertexType>
void GraphType<VertexType>::DepthFirstSearch(VertexType startVertex, VertexType endVertex)
{
	stack<VertexType> stuck;
	queue<VertexType> vertexQ;

	bool found = false;
	VertexType vertex;
	VertexType item;

	ClearMarks();
	stuck.push(startVertex);
	do
	{   vertex=stuck.top();
		stuck.pop();
		if (vertex == endVertex)
		{
			cout << vertex<<" ";
			found = true;
		}
    else
		{
			if (!IsMarked(vertex))
			{
				MarkVertex(vertex);
				cout << vertex<<" ";
				GetToVertices(vertex, vertexQ);

				while (!vertexQ.empty())
				{

					//vertexQ.Dequeue(item);
					VertexType item=vertexQ.front();
					vertexQ.pop();
					if (!IsMarked(item))
					stuck.push(item);
				}
			}
		}
	} while (!stuck.empty() && !found);
	if (!found)
	cout << "Path not found." << endl;
}
template<class VertexType>
void GraphType<VertexType>::BreathFirstSearch(VertexType startVertex, VertexType endVertex)
{
	queue<VertexType> que;
	queue<VertexType> vertexQ;

	bool found = false;
	VertexType vertex;
	VertexType item;

	ClearMarks();
	que.push(startVertex);
	do
	{
		//que.Dequeue(vertex);
		VertexType item=que.front();
					que.pop();
		if (vertex == endVertex)
		{
			cout  << vertex<<" ";
			found = true;
		}
    else
		{
			if (!IsMarked(vertex))
			{
				MarkVertex(vertex);
				cout  << vertex<<" ";
				GetToVertices(vertex, vertexQ);

				while (!vertexQ.empty())
				{
					//vertexQ.Dequeue(item);
					VertexType item=vertexQ.front();
					vertexQ.pop();
					if (!IsMarked(item))
					que.push(item);
				}
			}
		}
	} while (!que.empty() && !found);
	if (!found)
	cout << "Path not found." << endl;
}
template<class VertexType>
int GraphType<VertexType>::OutDegree(VertexType V)
{   int total=0;
    int index=IndexIs(vertices,V);
    for(int i=0;i<50;i++){
        if(edges[index][i]!=0)
            total++;
    }
    return total;
}
template<class VertexType>
bool GraphType<VertexType>::FoundEdge(VertexType U,VertexType V)
{
    int index_U=IndexIs(vertices,U);
    int index_V=IndexIs(vertices,V);
    return (edges[index_U][index_V]==1||edges[index_V][index_U]==1);
}
Topic archived. No new replies allowed.