-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLLPath.java
190 lines (151 loc) · 4.98 KB
/
DLLPath.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ragha
*/
/**
* Class DLLPath represents a path through a chessboard composed of a
* doubly-linked list of ChessboardSteps (positions)
*/
public class DLLPath {
private PathStepNode head;
private PathStepNode tail;
private int size;
public DLLPath() {
this.head = null;
this.tail = null;
this.size = 0;
}
public int size() {
return size;
}
public void addFirst(int row, int col) {
PathStepNode n = new PathStepNode(row, col);
if (this.head == null) { // empty list
this.head = n;
this.tail = n;
} else { // non-empty list
n.setNext(head);
head.setPrev(n);
head = n;
}
size++;
}
public void addLast(int row, int col) {
// TO DO (Task #1 - c)
// Adding last node in DLL
// Write your code here ..
PathStepNode n = new PathStepNode(row, col);
if (this.head == null) { // empty list
this.head = n;
this.tail = n;
} else { // non-empty list
tail.setNext(n);
n.setPrev(tail);
tail=n;
}
size++;
}
public int[] removeFirst() {
// TO DO (Task #1 - d)
// Removing first node in DLL
int[] coords = null;
// Write your code here ..
if(this.head==null && this.tail==null)
return null;
else{
coords = new int[]{head.getRow(),head.getCol()};
if(head==tail)
head = tail = null;
else{
head = head.getNext();
head.setPrev(null);
}
size--;
}
return coords;
}
public int[] removeLast() {
// TO DO (Task #1 - d)
// Removing last node in DLL
int[] coords = null;
// Write your code here ..
if(this.head==null && this.tail==null)
return null;
else{
coords = new int[]{tail.getRow(),tail.getCol()};
if(head==tail)
head = tail = null;
else{
tail = tail.getPrev();
tail.setNext(null);
}
size--;
}
return coords;
}
/**
* Text-based representation of DLLPath.
* with the format: (row1, col1), (row2, col2), ..., (rowN, colN)
*/
public String toString() {
String sPath = "";
PathStepNode s = null;
if (head != null) {
sPath = "(" + head.getRow() + ", " + head.getCol() + ")";
s = head.getNext();
}
while (s != null) {
sPath = sPath + ", " + "(" + s.getRow() + ", " + s.getCol() + ")";
s = s.getNext();
}
return sPath;
}
/*
* Compares two DLLPaths.
* Two DLLPaths are equals if they containing the same sequence of steps.
*/
public boolean equals(Object o) {
DLLPath otherPath = (DLLPath) o;
if (this.size != otherPath.size) {
return false;
}
PathStepNode s = head;
PathStepNode os = otherPath.head;
while ((s != null) && (os != null)) {
if ((s.getRow() != os.getRow()) || (s.getCol() != os.getCol())) {
return false;
}
s = s.getNext();
os = os.getNext();
}
return s == os;
}
public static void main(String[] args){
DLLPath p1 = new DLLPath();
p1.addLast(0, 0);p1.addLast(1, 0);p1.addLast(1, 1);p1.addLast(1, 2);p1.addLast(1, 3);p1.addLast(2, 3);p1.addLast(3, 3);
System.out.println("Size of path: "+p1.size()+"\n");
System.out.println(p1);
/*while(p1.size()!=0){
int[] c1 = p1.removeFirst();
System.out.println("Remove First: "+c1[0]+","+c1[1]);}
int[] c1 = p1.removeFirst();
if(c1==null)
System.out.println("Empty Path");*/
/*while(p1.size()!=0){
int[] c1 = p1.removeLast();
System.out.println("Remove Last: "+c1[0]+","+c1[1]);}
int[] c1 = p1.removeLast();
if(c1==null)
System.out.println("Empty Path");*/
DLLPath p2 = new DLLPath();
p2.addLast(0, 0);p2.addLast(1, 0);p2.addLast(1, 1);p2.addLast(1, 2);p2.addLast(1, 3);p2.addLast(2, 3);p2.addLast(3, 3);
System.out.println("Path 1 and 2 are equals? "+p1.equals(p2));
p2.removeFirst();
System.out.println("Path 1 and 2 are equals? "+p1.equals(p2));
}
}