sparse matrix with classes and linked list

Nov 18, 2011 at 11:33am
I am trying to create a sparse matrix using 2 classes and an ordered linked list to contain the data.

But the problem is the head pointer for the linked list and the data type for the linked list is declared private.
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class Element
{
	public:
		Element();					//default constructor
		Element(int, int, int); 	//constructor
		Element(const Element&);	//copy constructor
		
		void print() const;
		void set(int, int, int);
		int getRow() const;
		int getCol() const;
		int getValue() const;
		
	private:
		int row;
		int col;
		int value;
};

struct Node;
typedef Node* NodePtr;
struct Node
{
	Element e;
	NodePtr next;
};
			

class SparseMatrix
{
	public:
		SparseMatrix();
		~SparseMatrix();
		SparseMatrix(const SparseMatrix&);
		
		void constructMatrix();
		void insert(int);
		int findPosition(int, int, NodePtr&, NodePtr&);
		int compareEle(int, int, int, int);
		void addHead(int, int, int);
		void addTail(int, int, int);
		bool delElement(int, int);
		bool compare(int, int, int);
		void printList() const;
		
		NodePtr getHead() const;
		void setHead(NodePtr);
		
	private:
		NodePtr head;
};

int main ()
{	
	srand(time(NULL));
	
	SparseMatrix sM;
	
	sM.constructMatrix();

       	sM.printList();
}

Element::Element()
{
	this -> row = 1;
	this -> col = 1;
	this -> value = 1; 
}

Element::Element(int row, int col, int value)
{
	this -> row = row;
	this -> col = col;
	this -> value = value;
}

Element::Element(const Element& ele)
{
	this -> row = ele.row;
	this -> col = ele.col;
	this -> value = ele.value;
}

void Element::print() const
{
	cout << "(" << getRow() << ", " << getCol() << ", " << getValue() << ")" << endl;
}

void Element::set(int row, int col, int value)
{
	(*this).row = row;
	(*this).col = col;
	(*this).value = value;
}

int Element::getRow() const
{
	return row;
}

int Element::getCol() const
{
	return col;
}

int Element::getValue() const
{
	return value;
}

SparseMatrix::SparseMatrix()
{
	head = NULL;
}

SparseMatrix::~SparseMatrix()
{

}

SparseMatrix::SparseMatrix(const SparseMatrix&)
{
	
}

void SparseMatrix::constructMatrix()
{
	int no;
	cout << "How many elements do you wish to construct?: ";
	cin >> no;
	insert(no);

}

void SparseMatrix::insert(int no)
{
	NodePtr prev, curr;
	int distinct;
	int row, col, value;
	do
	{	 
		row = rand() % 9 + 1;
		col = rand() % 9 + 1;
		value = rand() % 99 + 1;
			  	   
		distinct = findPosition(row, col, prev, curr);
		
		if(distinct != -1)
		{
			if(prev == NULL)
				addHead(row, col, value);
			else if(curr == NULL)
				addTail(row, col, value);
			else
			{
				NodePtr temp = new Node;
				temp -> e.set(row, col, value);
				prev -> next = temp;
				temp -> next = curr;
			}
			no--;
		}
	}while(no > 0);
}	 	 
int SparseMatrix::findPosition(int row, int col, NodePtr& prev, NodePtr& curr)
{
	prev = NULL;
	curr = getHead();

	int distinct;
	
	distinct = compareEle(row, col, curr -> e.getRow(), curr -> e.getCol());
	
	while(curr !=NULL && distinct == 1)
	{
		prev = curr;
		curr = curr -> next;
	}
	
	return distinct;
}

int SparseMatrix::compareEle(int row, int col, int getRow, int getCol)
{
	
	if(row > getRow)
		return 1;
	else if(row == getRow && col > getCol)
		return 1;
	else if(row == getRow && col == getCol)
		return -1;
	else
		return 0;
}	 

void SparseMatrix::addHead(int row, int col, int value)
{
	NodePtr temp = new Node;
	temp -> e.set(row, col, value);
	temp -> next = getHead();
	
	setHead(temp);
}

void SparseMatrix::addTail(int row, int col, int value)
{
	NodePtr temp = new Node;
	temp -> e.set(row, col, value);
	temp -> next = NULL;
	
	if(head == NULL)
		setHead(temp);
	else
	{
		NodePtr curr = getHead();
		
		while(curr -> next != NULL);
			curr = curr -> next;
		
		curr -> next = temp;
	}	 	 	 
}

void SparseMatrix::setHead(NodePtr temp)
{
	this -> head = temp;
}

NodePtr SparseMatrix::getHead() const
{
	return head;
}

void SparseMatrix::printList() const
{	
	NodePtr temp = head;
	while(temp != NULL)
	{
		temp -> e.print();
		temp = temp -> next;
	}
	
	cout << endl;
}


I edited an paste a more complete of what I have done so far. So what this program "suppose" to do so far...it ask me how many elements I wan so if I input 3, it should generate 3 set for the sparse matrix, the row, the column and the data value. (the row and column is kind of like 2D array except I am not allow to use array.)

As the row and column need to be unique, i run a check to see of there is 2 or more of them having the same row and column. This is where i stuck. The program apparent just crash when I run it. I use "curr -> e.getRow()" to put to the get the value of row as it is private thus using accessor. But seems like it's not very correct this way. When I run and after keying a number, it crash. Any help ?
Last edited on Nov 18, 2011 at 1:22pm
Nov 18, 2011 at 12:06pm
Hi,

How do you pass the parameters when you call the class function in the main part?
Nov 18, 2011 at 12:11pm
hi,
please use code tags while posting code.
your code...
1
2
3
4
5
6
7
//...
NodePtr getHead() const;
void setHead(NodePtr);

private:
NodePtr head;
};


whell you have a metod to get a "head pointer" which isn't private so use it.
do the same with datatype pointer or whatever it is, cos I can't read your code as it is.
Nov 18, 2011 at 1:24pm
I updated my first post and used code tag also updated a more full coding.
Topic archived. No new replies allowed.