-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
156 lines (141 loc) · 4.96 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
156 lines (141 loc) · 4.96 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
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
public class SinglyLinkedList {
Node head;
Node tail;
int size;
SinglyLinkedList() {
}
static class Node {
int data;
int row;
int column;
Node nextColumn;
Node nextRow;
Node(int data, int row, int column) {
this.data = data;
this.row = row;
this.column = column;
}
}
String traverseInRow() {
Node node = this.head;
StringBuilder result = new StringBuilder();
while (node != null) {
result.append(node.row).append(" ").append(node.column).append(" ").append(node.data).append("\n");
node = node.nextColumn;
}
return result.toString();
}
String traverseInColumn() {
Node node = this.head;
StringBuilder result = new StringBuilder();
while (node != null) {
result.append(node.row).append(" ").append(node.column).append(" ").append(node.data).append("\n");
node = node.nextRow;
}
return result.toString();
}
void addInRow(int data, int row, int column, SinglyLinkedList columns) {
Node node = new Node(data, row, column);
Node current = this.head;
Node pre = null;
while (current != null && current.column < column) { //find the place
pre = current;
current = current.nextColumn;
}
if (current == this.head) {
node.nextColumn = current;
this.head = node;
if (this.size == 0) this.tail = this.head;
} else {
pre.nextColumn = node;
node.nextColumn = current;
}
columns.addInColumn(node);
this.size++;
}
private void addInColumn(Node node) {
Node current = this.head;
Node pre = null;
while (current != null && current.row < node.row) { //find the place
pre = current;
current = current.nextRow;
}
if (current == this.head) {
node.nextRow = current;
this.head = node;
if (this.size == 0) this.tail = this.head;
} else {
pre.nextRow = node;
node.nextRow = current;
}
this.size++;
}
void remove(int column, SinglyLinkedList columns) {
Node pre = null;
Node node = this.head;
while (node != null) {
if (node.column == column) { //if it isn't 0,changed the data to 0!
node.data = 0;
this.size--;
columns.removeFromColumn(node.row);
if (node == this.head) { //change the pointers
this.head = node.nextColumn;
} else if (pre != null) {
pre.nextColumn = node.nextColumn;
}
break;
} else if (node.column > column) break; //It's 0 by default!
else pre = node;
node = node.nextColumn;
}
}
private void removeFromColumn(int row) {
Node pre = null;
Node node = this.head;
while (node != null) {
if (node.row == row) { //if it isn't 0,changed the data to 0!
this.size--;
if (node == this.head) { //change the pointers
this.head = node.nextRow;
} else pre.nextRow = node.nextRow;
break;
} else if (node.row > row) break; //It's 0 by default!
else pre = node;
node = node.nextRow;
}
}
void update(int newValue, int row, int column, SinglyLinkedList columns) {
Node node = this.head;
while (node != null) {
if (node.column == column) { //if it isn't 0,changed the data!
node.data = newValue;
break;
} else if (node.column > column) { //if it's 0,updating is like adding!
addInRow(newValue, row, column, columns);
break;
} else if (newValue == 0) {
remove(column, columns);
break;
}
node = node.nextColumn;
}
}
//search in row
String search(int value) {
Node rowNode = this.head;
String result = null;
Node columnNode = this.head;
while (columnNode != null) {
if (columnNode.data == value) { //search in a row
result = "Row: " + columnNode.row + " Column: " + columnNode.column;
break;
} else if (rowNode!=null && rowNode.data == value) { //search in a column
result = "Row: " + rowNode.row + " Column: " + rowNode.column;
break;
}
rowNode = columnNode.nextRow;
columnNode = columnNode.nextColumn;
}
return result;
}
}