-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathMatrixList.java
141 lines (112 loc) · 2.11 KB
/
MatrixList.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
/*
Solved By: Sandeep Ranjan (1641012352)
Solution for Programming Project
5.6
*/
class Node {
int data;
Node fLink; //forward
Node dLink; //down
Node(int data) {
this.data = data;
this.fLink = this.dLink = null;
}
}
class Row {
private int col;
private Node start;
public Row(int col) {
this.col = col;
generateRow();
}
private void generateRow() {
Node newNode = new Node(-1);
start = newNode;
Node p = start;
while(col > 1) {
Node newN = new Node(-1);
p.fLink = newN;
p = newN;
col--;
}
}
public Node getStart() {
return start;
}
}
class Matrix {
private int n; //row
private int m; //col
private Row[] r;
private Node start;
public Matrix(int n, int m) {
this.n = n;
this.m = m;
start = null;
r = new Row[n];
check();
generateMatix();
}
private void check() {
if(n <= 0 || m <= 0) {
System.out.println("Value of 'n' & 'm' must be greater than 0");
System.exit(0);
}
}
private void generateMatix() {
for(int i=0; i<n; i++) {
r[i] = new Row(m);
}
start = r[0].getStart();
for(int i=0; i<n-1; i++) {
Node p1 = r[i].getStart();
Node p2 = r[i+1].getStart();
for(int j=0; j<m; j++) {
p1.dLink = p2;
p1 = p1.fLink;
p2 = p2.fLink;
}
}
}
public void insert (int x, int y, int data) {
Node cell = start;
int col = y;
if(x > n || y > m || x <= 0 || y <= 0) {
System.out.println("(x,y) such that 0 < x < "+ n + " and 0 < y < " + m + ".");
return;
}
for(int row=1; row<x; row++) {
cell = cell.dLink;
}
while(col>1) {
cell = cell.fLink;
col--;
}
cell.data = data;
}
public void display() {
Node row = start;
while(row != null) {
Node cell = row;
while(cell != null) {
System.out.print(cell.data + " ");
cell = cell.fLink;
}
System.out.println();
row = row.dLink;
}
System.out.println();
}
}
class MatrixList {
public static void main(String[] args) {
Matrix m = new Matrix(2,2);
m.display(); //Empty
//Value filling
m.insert(1,0,23);
m.insert(1,2,13);
m.insert(2,1,43);
m.insert(2,2,41);
m.display(); //After filling
}
}