Skip to content

Commit fdd3103

Browse files
committed
Created the Stack data structure.
1 parent 3a25541 commit fdd3103

File tree

5 files changed

+125
-34
lines changed

5 files changed

+125
-34
lines changed

JS Data Structures/Data Structs/OneLinkList.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Item {
4545
OneLinkList.prototype.AddToEnd = function(data) {
4646
// Check if valid data was passed as an argument.
4747
if(data === undefined) {
48-
throw new Err.DSException(Err.ListErr.InvalidData(), 301);
48+
throw new Err.DSException(Err.ErrMsg.InvalidData, 301);
4949
}
5050

5151
let newItem = new Item(data);
@@ -67,11 +67,11 @@ OneLinkList.prototype.AddToEnd = function(data) {
6767
OneLinkList.prototype.FindAt = function(position) {
6868
// Show error when trying to access an invalid position.
6969
if(this.count === 0) {
70-
throw new Err.DSException(Err.ListErr.EmptyList, 100);
70+
throw new Err.DSException(Err.ErrMsg.EmptyList, 100);
7171
} else if(position % 1 != 0 || typeof position != Number.name.toLowerCase()) {
72-
throw new Err.DSException(Err.ListErr.InvalidPosition(), 102);
72+
throw new Err.DSException(Err.ErrMsg.InvalidPosition, 102);
7373
} else if(position > this.count || position < 1) {
74-
throw new Err.DSException(Err.ListErr.NonExistant(OneLinkList.name), 101);
74+
throw new Err.DSException(Err.ErrMsg.NonExistant(OneLinkList.name), 101);
7575
}
7676

7777
// Sequentially search for item (1 -> last).
@@ -89,11 +89,11 @@ OneLinkList.prototype.FindAt = function(position) {
8989
OneLinkList.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.ErrMsg.NonExistant(OneLinkList.name), 201);
9393
}
9494
// Show error when the position is invalid.
9595
else if(position % 1 != 0 || typeof position != Number.name.toLowerCase()) {
96-
throw new Err.DSException(Err.ListErr.InvalidPosition(), 202);
96+
throw new Err.DSException(Err.ErrMsg.InvalidPosition, 202);
9797
}
9898

9999
// Linearly search for item before desired item (1 -> last).
@@ -129,11 +129,11 @@ OneLinkList.prototype.RemoveAt = function(position) {
129129
OneLinkList.prototype.RemoveThis = function(data) {
130130
// Check if the list is empty.
131131
if(this.count === 0) {
132-
throw new Err.DSException(Err.ListErr.NonExistant(OneLinkList.name), 401);
132+
throw new Err.DSException(Err.ErrMsg.NonExistant(OneLinkList.name), 401);
133133
}
134134
// Check if valid data was passed as an argument.
135135
else if(data === undefined) {
136-
throw new Err.DSException(Err.ListErr.InvalidData(), 402);
136+
throw new Err.DSException(Err.ErrMsg.InvalidData, 402);
137137
}
138138

139139
// Item to remove.
@@ -205,7 +205,7 @@ function FindThisHelper(list, data) {
205205
OneLinkList.prototype.Contains = function(data) {
206206
// Check if valid data was passed as an argument.
207207
if(data === undefined) {
208-
throw new Err.DSException(Err.ListErr.InvalidData(), 301);
208+
throw new Err.DSException(Err.ErrMsg.InvalidData, 301);
209209
}
210210

211211
// Search through list sequentially for the data.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Stack.js : Defines the implimentation for a stack.
2+
// Imports:
3+
const Err = require('../misc/Errors');
4+
5+
/**
6+
* The stack data structure.
7+
*/
8+
class Stack {
9+
/**
10+
* The stack constructor which takes an optional data parameter for the first item.
11+
* @param {any} data - The data for the first item in the stack.
12+
*/
13+
constructor(data) {
14+
this.count = 0;
15+
this.storage = {};
16+
17+
if(data !== undefined) {
18+
this.Push(data);
19+
}
20+
}
21+
}
22+
/**
23+
* Pushes the data to the top of the stack.
24+
* @param {any} data - The data to push.
25+
*/
26+
Stack.prototype.Push = function(data) {
27+
if(data === undefined) {
28+
throw new Err.DSException(Err.ErrMsg.InvalidData, 100);
29+
}
30+
31+
this.storage[this.count] = data;
32+
this.count++;
33+
}
34+
/**
35+
* Pops the data from the top of the stack.
36+
*/
37+
Stack.prototype.Pop = function() {
38+
if(this.count <= 0) {
39+
return undefined;
40+
}
41+
42+
this.count--;
43+
let topData = this.storage[this.count];
44+
delete this.storage[this.count];
45+
return topData;
46+
}
47+
/**
48+
* Returns the number of items in the stack.
49+
*/
50+
Stack.prototype.Size = function() {
51+
return this.count;
52+
}
53+
/**
54+
* Returns the top item to view (does not remove from the list).
55+
*/
56+
Stack.prototype.Peek = function() {
57+
if(this.count <= 0) {
58+
throw new Err.DSException(Err.ErrMsg.EmptyList, 101);
59+
}
60+
61+
return this.storage[this.count - 1];
62+
}
63+
/**
64+
* Swaps the top and second from top items.
65+
*/
66+
Stack.prototype.Swap = function() {
67+
if(this.count <= 0) {
68+
throw new Err.DSException(Err.ErrMsg.EmptyList, 103);
69+
}
70+
71+
let temp, lastIndex = this.count - 1,
72+
secondToLastIndex = lastIndex - 1;
73+
temp = this.storage[secondToLastIndex];
74+
this.storage[secondToLastIndex] = this.storage[lastIndex];
75+
this.storage[lastIndex] = temp;
76+
}
77+
/**
78+
* Empties the entire stack.
79+
*/
80+
Stack.prototype.Empty = function() {
81+
while(this.count > 0) {
82+
this.Pop();
83+
}
84+
}
85+
/**
86+
* Exports the stack data structure.
87+
*/
88+
module.exports = Stack;

JS Data Structures/Data Structs/TwoLinkList.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// TwoLinkList.js : Defines the implementation for a doubly linked list.
22

33
// Imports:
4-
const err = require('../misc/Errors');
4+
const Err = require('../misc/Errors');
55

66
class TwoLinkList {
77
/**
@@ -43,7 +43,7 @@ class Item {
4343
TwoLinkList.prototype.AddToEnd = function(data) {
4444
// Check if valid data was passed as an argument.
4545
if(data === undefined) {
46-
throw new Err.DSException(Err.ListErr.InvalidData(), 301);
46+
throw new Err.DSException(Err.ErrMsg.InvalidData, 301);
4747
}
4848

4949
let newItem = new Item(data)
@@ -70,11 +70,11 @@ TwoLinkList.prototype.AddToEnd = function(data) {
7070
TwoLinkList.prototype.FindAt = function(position) {
7171
// Show error when trying to access an invalid position.
7272
if(this.count === 0) {
73-
throw new Err.DSException(Err.ListErr.EmptyList, 100);
73+
throw new Err.DSException(Err.ErrMsg.EmptyList, 100);
7474
} else if(position % 1 != 0 || typeof position != Number.name.toLowerCase()) {
75-
throw new Err.DSException(Err.ListErr.InvalidPosition(), 101);
75+
throw new Err.DSException(Err.ErrMsg.InvalidPosition, 101);
7676
} else if(position > this.count || position < 1) {
77-
throw new Err.DSException(Err.ListErr.NonExistant(TwoLinkList.name), 102);
77+
throw new Err.DSException(Err.ErrMsg.NonExistant(TwoLinkList.name), 102);
7878
}
7979

8080
// Sequentially search for item (1 -> last).
@@ -92,11 +92,11 @@ TwoLinkList.prototype.FindAt = function(position) {
9292
TwoLinkList.prototype.RemoveAt = function(position) {
9393
// Show error when trying to access position outside of list.
9494
if(this.count === 0 || position > this.count || position < 1) {
95-
throw new Err.DSException(Err.ListErr.NonExistant(TwoLinkList.name), 201);
95+
throw new Err.DSException(Err.ErrMsg.NonExistant(TwoLinkList.name), 201);
9696
}
9797
// Show error when the position is invalid.
9898
else if(position % 1 != 0 || typeof position != Number.name.toLowerCase()) {
99-
throw new Err.DSException(Err.ListErr.InvalidPosition(), 202);
99+
throw new Err.DSException(Err.ErrMsg.InvalidPosition, 202);
100100
}
101101

102102
// Handle list with one item.
@@ -140,11 +140,11 @@ TwoLinkList.prototype.RemoveAt = function(position) {
140140
TwoLinkList.prototype.RemoveThis = function(data) {
141141
// Check if the list is empty.
142142
if(this.count === 0) {
143-
throw new Err.DSException(Err.ListErr.NonExistant(TwoLinkList.name), 401);
143+
throw new Err.DSException(Err.ErrMsg.NonExistant(TwoLinkList.name), 401);
144144
}
145145
// Check if valid data was passed as an argument.
146146
else if(data === undefined) {
147-
throw new Err.DSException(Err.ListErr.InvalidData(), 402);
147+
throw new Err.DSException(Err.ErrMsg.InvalidData, 402);
148148
}
149149

150150
// Item to remove.
@@ -218,7 +218,7 @@ function FindThisHelper(list, data) {
218218
TwoLinkList.prototype.Contains = function(data) {
219219
// Check if valid data was passed as an argument.
220220
if(data === undefined) {
221-
throw new Err.DSException(Err.ListErr.InvalidData(), 301);
221+
throw new Err.DSException(Err.ErrMsg.InvalidData, 301);
222222
}
223223

224224
// Search through list sequentially for the data.

JS Data Structures/app.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// app.js : Main entry point for the app.
22
const OneLinkList = require('./Data Structs/OneLinkList');
33
const TwoLinkList = require('./Data Structs/TwoLinkList');
4+
const Stack = require('./Data Structs/Stack');
45
const test_OneList = require('./tests/Test_OneLinkList');
56

6-
// -- TwoLinkList --
7-
list = new TwoLinkList('one');
8-
list.AddToEnd('two');
9-
list.AddToEnd('three');
10-
list.AddToEnd('four');
11-
list.PrintAll();
7+
// -- Stack --
8+
let stak = new Stack('before one');
9+
stak.Push('one');
10+
stak.Push('two');
11+
stak.Pop();
12+
stak.Push('two');
13+
stak.Push('three');
14+
stak.Swap();
15+
let top = stak.Peek();
16+
top = null;
17+
stak.Empty();
1218

1319
// -- Test --

JS Data Structures/misc/Errors.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Errors.js : Contains all error messages the js data structures will use.
2+
// Note: Consider adding a default error code.
23

34
/**
45
* Data Structure error class.
@@ -17,23 +18,19 @@ class DSException {
1718
/**
1819
* Errors that may occur in lists.
1920
*/
20-
const ListErr = {
21+
const ErrMsg = {
2122
NonExistant: function(list) {
2223
if(!list) {
2324
list = 'this';
2425
}
2526
return `The desired item does not exist in ${list} instance.`;
2627
},
27-
InvalidPosition: function() {
28-
return `The position provided was not a valid integer.`;
29-
},
30-
InvalidData: function() {
31-
return `No valid data was passed as an argument.`;
32-
},
33-
EmptyList: `The linked list is empty.`,
28+
InvalidPosition: `The position provided was not a valid integer.`,
29+
InvalidData: `No valid data was passed as an argument.`,
30+
EmptyList: `The data structure container appears to be empty.`,
3431
}
3532

3633
module.exports = {
37-
ListErr: ListErr,
34+
ErrMsg: ErrMsg,
3835
DSException: DSException,
3936
}

0 commit comments

Comments
 (0)