Repository navigation
Expand file tree
/
Copy pathSparseMatrixList.java
More file actions
107 lines (95 loc) · 3.43 KB
/
Copy pathSparseMatrixList.java
File metadata and controls
107 lines (95 loc) · 3.43 KB
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
/**
* SparseMatrixList.java
* This file defines a custom singly linked list used to represent a sparse matrix.
* It strictly stores only non-zero elements to save memory.
* Nodes are kept sorted in ascending order primarily by row, and secondarily by column.
*/
public class SparseMatrixList {
Node head;
int rows;
int cols;
// Constructor to initialize the list with the matrix dimensions
public SparseMatrixList(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.head = null;
}
// Inserts a new node in the correct sorted position (ascending by row, then col).
// Explicitly drops any values of 0.
public void insert(int r, int c, int val) {
if (val == 0) return; // Zeros are not nodes in the linked list
Node newNode = new Node(r, c, val);
// Insert at the head if list is empty or new node comes before the current head
if (head == null || compareNodes(newNode, head) < 0) {
newNode.next = head;
head = newNode;
return;
}
// Traverse the list to find the correct insertion point
Node current = head;
while (current.next != null && compareNodes(newNode, current.next) > 0) {
current = current.next;
}
// If a node already exists at these exact coordinates, update its value
if (current.row == r && current.col == c) {
current.value = val;
return;
}
// Splice the new node into the list
newNode.next = current.next;
current.next = newNode;
}
// Helper method for O(1) constant time insertion during matrix addition.
// Uses a reference array to keep track of the tail node.
public void append(Node[] tailRef, int r, int c, int val) {
if (val == 0) return; // Drop resulting zeroes
Node newNode = new Node(r, c, val);
if (this.head == null) {
this.head = newNode;
tailRef[0] = newNode;
} else {
tailRef[0].next = newNode;
tailRef[0] = newNode;
}
}
// Helper method to compare coordinates of two nodes for sorting
private int compareNodes(Node n1, Node n2) {
if (n1.row != n2.row) {
return Integer.compare(n1.row, n2.row);
}
return Integer.compare(n1.col, n2.col);
}
// Prints the non-zero elements of the linked list sequentially
public void displayList() {
Node current = head;
while (current != null) {
System.out.print(current.value + (current.next != null ? " " : ""));
current = current.next;
}
System.out.println();
}
// Prints the full matrix in a 2D grid format, filling in zeroes for missing nodes
public void displayMatrix() {
Node current = head;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (current != null && current.row == i && current.col == j) {
System.out.print(current.value + " ");
current = current.next;
} else {
System.out.print("0 ");
}
}
System.out.println();
}
}
public int countNodes() {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
}