Skip to content

Commit bc01224

Browse files
committed
Update (1/3/18).
1 parent ae08b8b commit bc01224

File tree

5 files changed

+592
-66
lines changed

5 files changed

+592
-66
lines changed

JS Data Structures/Data Structs/OneLinkList.js

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,14 @@ OneLinkList.prototype.AddToEnd = function(data) {
4848
throw new Err.DSException(Err.ListErr.InvalidData(), 301);
4949
}
5050

51-
let newItem = new Item(data),
52-
currItem = this.first;
51+
let newItem = new Item(data);
5352

5453
if(this.count === 0) {
5554
this.first = newItem;
5655
this.last = this.first;
5756
} else {
5857
this.last.next = newItem;
5958
this.last = newItem;
60-
61-
// while(currItem.next !== null) {
62-
// currItem = currItem.next;
63-
// }
64-
65-
// currItem.next = newItem;
66-
// this.last = newItem;
6759
}
6860

6961
this.count++;
@@ -73,7 +65,7 @@ OneLinkList.prototype.AddToEnd = function(data) {
7365
* @param {Number} position - The position in the list where the item should be located.
7466
*/
7567
OneLinkList.prototype.FindAt = function(position) {
76-
// Show error when trying to access position outside of list.
68+
// Show error when trying to access an invalid position.
7769
if(this.count === 0) {
7870
throw new Err.DSException(Err.ListErr.EmptyList, 100);
7971
} else if(position % 1 != 0 || typeof position != Number.name.toLowerCase()) {
@@ -104,29 +96,30 @@ OneLinkList.prototype.RemoveAt = function(position) {
10496
throw new Err.DSException(Err.ListErr.InvalidPosition(), 202);
10597
}
10698

107-
// Handle list with one item.
108-
if(this.count === 1) {
109-
this.first = null;
110-
this.last = null;
111-
}
11299
// Linearly search for item before desired item (1 -> last).
113-
else {
114-
let currItem = this.first,
115-
beforeItemToRemove,
116-
itemToRemove;
117-
for(let currCount = 1; currCount < position; currCount++) {
118-
beforeItemToRemove = currItem;
119-
itemToRemove = currItem.next;
120-
currItem = currItem.next;
121-
}
100+
let currItem = this.first,
101+
beforeItemToRemove = this.first.next, // Set it equal to first.next to make case w/1 item work.
102+
itemToRemove = currItem;
103+
for(let currCount = 1; currCount < position; currCount++) {
104+
beforeItemToRemove = currItem;
105+
itemToRemove = currItem.next;
106+
currItem = currItem.next;
107+
}
122108

109+
// Special Case: position == 1
110+
if(position === 1) {
111+
this.first = itemToRemove.next;
112+
}
113+
// Special Case: position == last
114+
if(position === this.count) {
115+
this.last = beforeItemToRemove; // Case w/2 items: need to set beforeItemToRemove.next = null (Done).
116+
}
117+
// All other positions
118+
if(this.count > 1 && position > 1) {
123119
beforeItemToRemove.next = itemToRemove.next;
124-
// Check if removing the last item.
125-
if(itemToRemove === this.last) {
126-
this.last = beforeItemToRemove;
127-
}
128-
itemToRemove = null;
129120
}
121+
122+
itemToRemove = null;
130123
this.count--;
131124
}
132125
/**

JS Data Structures/Data Structs/TwoLinkList.js

Lines changed: 109 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,18 @@ TwoLinkList.prototype.AddToEnd = function(data) {
6060

6161
this.count++;
6262
}
63-
6463
/**
6564
* Returns the item at the desired position in the list.
6665
* @param {Number} position - The position in the list where the item should be located.
6766
*/
6867
TwoLinkList.prototype.FindAt = function(position) {
69-
// Show error when trying to access position outside of list.
70-
if(this.count === 0 || position > this.count || position < 1) {
71-
throw new Err.DSException(Err.ListErr.NonExistant(OneLinkList.name), 101);
68+
// Show error when trying to access an invalid position.
69+
if(this.count === 0) {
70+
throw new Err.DSException(Err.ListErr.EmptyList, 100);
7271
} else if(position % 1 != 0 || typeof position != Number.name.toLowerCase()) {
73-
throw new Err.DSException(Err.ListErr.InvalidPosition(), 102);
72+
throw new Err.DSException(Err.ListErr.InvalidPosition(), 101);
73+
} else if(position > this.count || position < 1) {
74+
throw new Err.DSException(Err.ListErr.NonExistant(TwoLinkList.name), 102);
7475
}
7576

7677
// Sequentially search for item (1 -> last).
@@ -81,52 +82,131 @@ TwoLinkList.prototype.FindAt = function(position) {
8182

8283
return currItem;
8384
}
84-
8585
/**
8686
* Removes the item at the desired position in the list.
8787
* @param {Number} position - The position in the list where the item should be located.
8888
*/
8989
TwoLinkList.prototype.RemoveAt = function(position) {
9090
// Show error when trying to access position outside of list.
9191
if(this.count === 0 || position > this.count || position < 1) {
92-
throw new Err.DSException(Err.ListErr.NonExistant(OneLinkList.name), 201);
92+
throw new Err.DSException(Err.ListErr.NonExistant(TwoLinkList.name), 201);
9393
}
9494
// Show error when the position is invalid.
9595
else if(position % 1 != 0 || typeof position != Number.name.toLowerCase()) {
9696
throw new Err.DSException(Err.ListErr.InvalidPosition(), 202);
9797
}
9898

99-
// Linearly search for item before desired item (1 -> last).
100-
let currItem = this.first,
101-
beforeItemToRemove, itemToRemove, currCount;
102-
for(currCount = 1; currCount < position; currCount++) {
103-
beforeItemToRemove = currItem;
104-
itemToRemove = currItem.next;
105-
currItem = currItem.next;
99+
// Handle list with one item.
100+
if(this.count === 1) {
101+
this.first = null;
102+
this.last = null;
106103
}
104+
// Linearly search for item before desired item (1 -> last).
105+
else {
106+
let currItem = this.first,
107+
itemToRemove = currItem;
108+
for(let currCount = 1; currCount < position; currCount++) {
109+
itemToRemove = currItem.next;
110+
currItem = currItem.next;
111+
}
107112

108-
// if(currCount)
109-
beforeItemToRemove.next = itemToRemove.next;
110-
delete itemToRemove;
113+
// Special Case: position == 1
114+
if(position === 1) {
115+
itemToRemove.next.prev = null;
116+
this.first = itemToRemove.next;
117+
}
118+
// Special Case: position == last
119+
else if(position === this.count) {
120+
itemToRemove.prev.next = null;
121+
this.last = itemToRemove.prev;
122+
}
123+
// All other positions
124+
else {
125+
itemToRemove.prev.next = itemToRemove.next;
126+
itemToRemove.next.prev = itemToRemove.prev;
127+
}
128+
129+
itemToRemove = null;
130+
}
111131
this.count--;
112132
}
113-
114133
/**
115-
* Print all items of the list to the console.
134+
* Removes the first item matching the data passed.
135+
* @param {any} data - The data to search and destroy.
116136
*/
117-
TwoLinkList.prototype.PrintAll = function() {
118-
// Check if list is empty.
119-
if(this.count === 0 || !(this.first instanceof Item)) {
120-
console.log('Empty list.');
121-
return;
137+
TwoLinkList.prototype.RemoveThis = function(data) {
138+
// Check if the list is empty.
139+
if(this.count === 0) {
140+
throw new Err.DSException(Err.ListErr.NonExistant(TwoLinkList.name), 401);
141+
}
142+
// Check if valid data was passed as an argument.
143+
else if(data === undefined) {
144+
throw new Err.DSException(Err.ListErr.InvalidData(), 402);
122145
}
123146

124-
// Print each item to the console sequentially.
125-
let curr = this.first;
126-
while(curr) {
127-
console.log(curr);
128-
curr = curr.next;
147+
// Search through list sequentially for the data.
148+
let currItem = this.first,
149+
itemToRemove;
150+
151+
// Handle list with one item.
152+
if(this.count === 1) {
153+
if(typeof(data) == 'object') {
154+
if(JSON.stringify(currItem.data) === JSON.stringify(data)) {
155+
this.first = null;
156+
this.last = null;
157+
this.count--;
158+
return;
159+
}
160+
} else {
161+
if(currItem.data === data) {
162+
this.first = null;
163+
this.last = null;
164+
this.count--;
165+
return;
166+
}
167+
}
168+
}
169+
// Handle list with more than one item.
170+
else {
171+
// If data param is an object.
172+
if(typeof(data) == 'object') {
173+
while(currItem.next) {
174+
if(JSON.stringify(currItem.next.data) === JSON.stringify(data)) {
175+
itemToRemove = currItem.next;
176+
currItem.next = currItem.next.next;
177+
178+
// Check if removing last item.
179+
if(itemToRemove === this.last) {
180+
this.last = currItem;
181+
}
182+
this.count--;
183+
break;
184+
}
185+
186+
currItem = currItem.next;
187+
}
188+
}
189+
// Else data param is a non-reference data type.
190+
else {
191+
while(currItem.next) {
192+
if(currItem.next.data === data) {
193+
itemToRemove = currItem.next;
194+
currItem.next = currItem.next.next;
195+
196+
// Check if removing last item.
197+
if(itemToRemove === this.last) {
198+
this.last = currItem;
199+
}
200+
this.count--;
201+
break;
202+
}
203+
204+
currItem = currItem.next;
205+
}
206+
}
129207
}
208+
209+
itemToRemove = null;
130210
}
131211

132212
module.exports = TwoLinkList;

JS Data Structures/app.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ const TwoLinkList = require('./Data Structs/TwoLinkList');
44
const test_OneList = require('./tests/Test_OneLinkList');
55

66
// -- Test the OneLinkList --
7-
test_OneList.OverallOneLinkTest(true);
7+
//test_OneList.OverallOneLinkTest(true);
8+
// Need to check RemoveAt with only 2 items.
9+
// Also need to check RemoveAt with many items, but removing the first item.
810

911
// -- TwoLinkList --
10-
// var users2 = new TwoLinkList('one');
11-
// users2.AddToEnd('two');
12-
// users2.AddToEnd('three');
13-
// let two = users2.FindAt(2);
14-
// console.log(two);
15-
// users2.PrintAll();
12+
var list = new TwoLinkList('one');
13+
list.AddToEnd('two');
14+
list.AddToEnd('three');
15+
16+
list.RemoveThis('three');
17+
console.log(list);
18+
//list.PrintAll();

JS Data Structures/tests/Test_OneLinkList.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,8 @@ module.exports = {
440440
ContainsTest: ContainsTest,
441441
AddRemoveTest: AddRemoveTest,
442442
FindAtTest: FindAtTest,
443-
}
443+
}
444+
445+
446+
// Notes:
447+
// - Need to add test to remove first item (end points first/last).

0 commit comments

Comments
 (0)