Skip to content

Commit e914b9b

Browse files
author
Peter Kaufinger
committed
Fixed and Added RemoveThis method in One/TwoLinkLists.
1 parent bc01224 commit e914b9b

File tree

3 files changed

+108
-113
lines changed

3 files changed

+108
-113
lines changed

JS Data Structures/Data Structs/OneLinkList.js

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -136,69 +136,67 @@ OneLinkList.prototype.RemoveThis = function(data) {
136136
throw new Err.DSException(Err.ListErr.InvalidData(), 402);
137137
}
138138

139-
// Search through list sequentially for the data.
140-
let currItem = this.first,
141-
itemToRemove;
139+
// Item to remove.
140+
let itemToRemove;
142141

143142
// Handle list with one item.
144143
if(this.count === 1) {
145-
if(typeof(data) == 'object') {
146-
if(JSON.stringify(currItem.data) === JSON.stringify(data)) {
147-
this.first = null;
148-
this.last = null;
149-
this.count--;
150-
return;
151-
}
152-
} else {
153-
if(currItem.data === data) {
154-
this.first = null;
155-
this.last = null;
156-
this.count--;
157-
return;
158-
}
144+
if(JSON.stringify(this.first.data) === JSON.stringify(data)) {
145+
itemToRemove = this.first;
146+
this.first = null;
147+
this.last = null;
148+
this.count--;
159149
}
160150
}
161151
// Handle list with more than one item.
162152
else {
163-
// If data param is an object.
164-
if(typeof(data) == 'object') {
165-
while(currItem.next) {
166-
if(JSON.stringify(currItem.next.data) === JSON.stringify(data)) {
167-
itemToRemove = currItem.next;
168-
currItem.next = currItem.next.next;
169-
170-
// Check if removing last item.
171-
if(itemToRemove === this.last) {
172-
this.last = currItem;
173-
}
174-
this.count--;
175-
break;
176-
}
153+
itemToRemove = FindThisHelper(this, data);
154+
}
177155

178-
currItem = currItem.next;
179-
}
180-
}
181-
// Else data param is a non-reference data type.
182-
else {
183-
while(currItem.next) {
184-
if(currItem.next.data === data) {
185-
itemToRemove = currItem.next;
186-
currItem.next = currItem.next.next;
156+
itemToRemove = null;
157+
}
158+
/**
159+
* Helper method for the FindThis method.
160+
* @param {OneLinkList} list - The list used to search through.
161+
* @param {Any} data - The data to search for.
162+
*/
163+
function FindThisHelper(list, data){
164+
// Variables needed to search
165+
let currItem = list.first,
166+
nextItem = list.first.next,
167+
itemToRemove;
187168

188-
// Check if removing last item.
189-
if(itemToRemove === this.last) {
190-
this.last = currItem;
191-
}
192-
this.count--;
193-
break;
194-
}
169+
// Check the first item.
170+
if(JSON.stringify(list.first.data) === JSON.stringify(data)){
171+
itemToRemove = list.first;
172+
list.first = list.first.next;
173+
list.count--;
174+
return itemToRemove;
175+
}
195176

196-
currItem = currItem.next;
197-
}
177+
// Search through the items in between the first and last sequentially.
178+
for(let currPos = 1; currPos < list.count-1; currPos++){
179+
if(JSON.stringify(nextItem.data) === JSON.stringify(data)){
180+
itemToRemove = nextItem;
181+
currItem.next = nextItem.next;
182+
list.count--;
183+
return itemToRemove;
198184
}
185+
186+
currItem = nextItem;
187+
nextItem = nextItem.next;
199188
}
200189

201-
itemToRemove = null;
190+
// Check the last item.
191+
if(JSON.stringify(list.last.data) === JSON.stringify(data)){
192+
// Note: currItem == one before the end (from the for statement).
193+
itemToRemove = list.last;
194+
currItem.next = null;
195+
list.last = currItem;
196+
list.count--;
197+
}
198+
199+
return itemToRemove;
202200
}
203201
/**
204202
* Checks if the list has an item with the passed data.

JS Data Structures/Data Structs/TwoLinkList.js

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -137,76 +137,76 @@ TwoLinkList.prototype.RemoveAt = function(position) {
137137
TwoLinkList.prototype.RemoveThis = function(data) {
138138
// Check if the list is empty.
139139
if(this.count === 0) {
140-
throw new Err.DSException(Err.ListErr.NonExistant(TwoLinkList.name), 401);
140+
throw new Err.DSException(Err.ListErr.NonExistant(OneLinkList.name), 401);
141141
}
142142
// Check if valid data was passed as an argument.
143143
else if(data === undefined) {
144144
throw new Err.DSException(Err.ListErr.InvalidData(), 402);
145145
}
146146

147-
// Search through list sequentially for the data.
148-
let currItem = this.first,
149-
itemToRemove;
147+
// Item to remove.
148+
let itemToRemove;
150149

151150
// Handle list with one item.
152151
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-
}
152+
if(JSON.stringify(this.first.data) === JSON.stringify(data)) {
153+
itemToRemove = this.first;
154+
this.first = null;
155+
this.last = null;
156+
this.count--;
167157
}
168158
}
169159
// Handle list with more than one item.
170160
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-
}
161+
itemToRemove = FindThisHelper(this, data);
207162
}
208163

209164
itemToRemove = null;
210165
}
166+
/**
167+
* Helper method for the FindThis method.
168+
* @param {OneLinkList} list - The list used to search through.
169+
* @param {Any} data - The data to search for.
170+
*/
171+
function FindThisHelper(list, data){
172+
// Variables needed to search
173+
let currItem = list.first,
174+
nextItem = list.first.next,
175+
itemToRemove;
176+
177+
// Check the first item.
178+
if(JSON.stringify(list.first.data) === JSON.stringify(data)){
179+
itemToRemove = list.first;
180+
list.first = list.first.next;
181+
list.first.prev = null;
182+
list.count--;
183+
return itemToRemove;
184+
}
185+
186+
// Check the last item.
187+
if(JSON.stringify(list.last.data) === JSON.stringify(data)){
188+
itemToRemove = list.last;
189+
list.last.prev.next = null; // Set the second to last item's next to null.
190+
list.last = list.last.prev; // Set last to second to last item.
191+
list.count--;
192+
return itemToRemove;
193+
}
194+
195+
// Search through the items in between the first and last sequentially.
196+
currItem = currItem.next;
197+
for(let currPos = 2; currPos < list.count; currPos++){
198+
if(JSON.stringify(currItem.data) === JSON.stringify(data)){
199+
itemToRemove = currItem;
200+
currItem.prev.next = currItem.next; // Set prev item's next to this item's next.
201+
currItem.next.prev = currItem.prev; // Set next item's prev to this item's prev.
202+
list.count--;
203+
return itemToRemove;
204+
}
205+
206+
currItem = currItem.next;
207+
}
208+
209+
return itemToRemove;
210+
}
211211

212212
module.exports = TwoLinkList;

JS Data Structures/app.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ const OneLinkList = require('./Data Structs/OneLinkList');
33
const TwoLinkList = require('./Data Structs/TwoLinkList');
44
const test_OneList = require('./tests/Test_OneLinkList');
55

6-
// -- Test the OneLinkList --
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.
10-
116
// -- TwoLinkList --
12-
var list = new TwoLinkList('one');
7+
list = new TwoLinkList('one');
138
list.AddToEnd('two');
149
list.AddToEnd('three');
15-
16-
list.RemoveThis('three');
10+
list.AddToEnd('four');
11+
list.RemoveThis(new String('three'));
1712
console.log(list);
18-
//list.PrintAll();
13+
//list.PrintAll();
14+
15+
// -- Test --

0 commit comments

Comments
 (0)