-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.cpp
66 lines (54 loc) · 1.89 KB
/
helper.cpp
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
/*
* helper.cpp
* Contains helper functions for debugging purposes, including member function void Matrix::debug() const;
*/
#include "matrix.h"
/* A helper function to be used in class Matrix member function, debug() :
* Given a pointer to an Element, check if it is a circular linked list by traversing in a particular direction (Up, Down, Left, Right).
*/
bool check(const Element* list, direction dir, bool debug = true) {
const Element* current = list;
do {
cout << current->id << " ";
switch (dir) {
case UP: current = current->up; break;
case DOWN: current = current->down; break;
case LEFT: current = current->left; break;
case RIGHT: current = current->right; break;
}
} while (current != list && current != nullptr);
if (debug) {
if (current == nullptr)
cout << "\t\tIt is not circular" << endl;
else
cout << "\t\tIt is circular" << endl;
}
return current != nullptr;
}
// Check the Matrix object links
void Matrix::debug() const {
cout << "Traverse right" << endl;
for (int i = 0; i < height; i++) {
cout << "Row " << i << "\t";
if (!check(rowHeads[i], RIGHT))
cout << "Row " << i << " is not a circular linked list (right pointer)." << endl;
}
cout << "Traverse left" << endl;
for (int i = 0; i < height; i++) {
cout << "Row " << i << "\t";
if (!check(rowHeads[i], LEFT))
cout << "Row " << i << " is not a circular linked list (left pointer)." << endl;
}
cout << "Traverse down" << endl;
for (int i = 0; i < width; i++) {
cout << "Col " << i << "\t";
if (!check(colHeads[i], DOWN))
cout << "Col " << i << " is not a circular linked list (down pointer)." << endl;
}
cout << "Traverse up" << endl;
for (int i = 0; i < width; i++) {
cout << "Col " << i << "\t";
if (!check(colHeads[i], UP))
cout << "Col " << i << " is not a circular linked list (up pointer)." << endl;
}
}