-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixLinkedList.java
163 lines (138 loc) · 3.9 KB
/
MatrixLinkedList.java
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
public class MatrixLinkedList<T> {
/**
* Implements a matrix using a linkedList data structure.
*/
private Link<T> upperLeft;
/**
* number of rows for this matrix
*/
private int rows;
/**
* number of cols for this matrix.
*/
private int cols;
/**
* constructs a matrix using a linked list.
* @param rows number of rows
* @param cols number of cols
*/
public MatrixLinkedList(int rows, int cols) {
this.upperLeft = new Link<T>();
upperLeft.row = 2;
upperLeft.col = 2;
this.rows = rows;
this.cols = cols;
constructMatrix(upperLeft,rows);
}
/**
* Construct matrix by first initializing the upperleft link.
* Creates the number of links corresponding to each row. As the links are
* created for each row. A concurrent link is created below which will set
* up a link from the current link to the right and down. Continues until
* the whole row is filled. Recursively apply the same step to the bottom row.
* @param initLink the upperLeft link in the matrix.(0 0)
* @param rows the number of rows to construct.
*/
public void constructMatrix(Link<T> initLink, int rows) {
if (rows == 0) {
return;
} else {
Link<T> current = initLink;
Link<T> nextInitLink = initLink;
for (int i = 0; i < cols - 1; i++) {
Link<T> newLeftLink = new Link<T>();
current.left = newLeftLink;
// checks to see whether last row is in creation. Dont want to
// create bottom links when the last row is created.
if (rows != 1) {
Link<T> newDownLink = new Link<T>();
current.down = newDownLink;
}
current = newLeftLink;
}
constructMatrix(nextInitLink.down, rows - 1);
}
}
/**
* displays the matrix.
*/
public void displayMatrix() {
Link<T> downLink = upperLeft;
while (downLink != null) {
displayRow(downLink);
downLink = downLink.down;
}
}
/**
* method to add a new row above the matrix.
*/
public void addTopRow() {
Link<T> newLink = new Link<T>();
Link<T> newUpperLeft = newLink;
Link<T> current = upperLeft;
for (int i = 0; i < cols - 1; i++) {
Link<T> nextLink = new Link<T>();
newLink.left = nextLink;
newLink.down = current;
current = current.left;
newLink = nextLink;
}
upperLeft = newUpperLeft;
}
/**
* inserts element into specified row and col of matrix.
* @param data the data to be insertd.
* @param row the row to insert data in.
* @param col the col to insert data.
*/
public void insertElement(T data, int row, int col) {
Link<T> toInsert = upperLeft;
for (int i = 0; i < row; i++) {
toInsert = toInsert.down;
}
for (int j = 0; j < col; j++) {
toInsert = toInsert.left;
}
toInsert.setData(data);
}
/**
* helper method that is used in the displayMatrix method.
* @param rowLink the 1st link of every row.
*/
public void displayRow(Link<T> rowLink) {
while (rowLink != null) {
rowLink.display();
rowLink = rowLink.left;
}
System.out.println();
}
/**
* inner class Link that is only useful to the Matrix.
* @param <T> the generic data type that the link stores.
*/
public class Link<T> {
protected Link<T> left;
protected Link<T> down;
protected T data;
public int row;
public int col;
public Link(T data) {
this.data = data;
}
public Link(){
}
public void setData(T data) {
this.data = data;
}
public void display() {
if (data == null) {
System.out.print(0 + " ");
} else {
System.out.print(data.toString() + " ");
}
}
public T getData() {
return this.data;
}
}
}