Skip to content

Commit b3604fa

Browse files
jantabackiabranhe
authored andcommitted
* fixed toString method in stack.js implementation. Now reads all elements returns them as a string and uses data property instead of element property that did not exist.
1 parent dc2176d commit b3604fa

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

algorithms/data-structures/stack.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/**
2-
* @author Rashik Ansar and Luiz Guerra
3-
*
4-
*
5-
* Implemtaion of Stack data structure
6-
* Stack follows LIFO (Last In First Out) priniciple
7-
* For Insertion and Deletion its complexity is O(1)
8-
* For Accessing and Searching its complexity is O(n)
9-
*/
2+
* @author Rashik Ansar and Luiz Guerra
3+
*
4+
* Implemtaion of Stack data structure
5+
* Stack follows LIFO (Last In First Out) priniciple
6+
* For Insertion and Deletion its complexity is O(1)
7+
* For Accessing and Searching its complexity is O(n)
8+
*
9+
* @author Jan Tabacki
10+
* Fix in toString method, display all elements and get data property instead of element
11+
* which does no exist.
12+
*/
1013

1114
class Stack {
1215
/**
@@ -63,21 +66,21 @@ class Stack {
6366
}
6467
return this.first.data;
6568
}
66-
69+
6770
/**
6871
* @returns size of the Stack
6972
*/
7073
size() {
7174
return this.size;
7275
}
73-
76+
7477
/**
7578
* @returns if Stack is empty
7679
*/
7780
isEmpty() {
7881
return this.size == 0;
7982
}
80-
83+
8184
/**
8285
* clears the Stack
8386
*/
@@ -86,24 +89,25 @@ class Stack {
8689
this.last = null;
8790
this.size = 0;
8891
}
89-
92+
9093
/**
9194
* @returns the Stack
9295
*/
9396
toString() {
94-
let str = "";
97+
let str = "";
9598
let aux = this.first;
96-
for (let i = 0; i < this.count; i++)
97-
str += aux.element + " ";
98-
aux = aux.next;
99+
while (aux) {
100+
str += aux.data + " ";
101+
aux = aux.next;
102+
}
99103
return str;
100104
}
101-
105+
102106
}
103107

104108
class Node {
105109
constructor(data) {
106110
this.data = data;
107111
this.next = null;
108112
}
109-
}
113+
}

0 commit comments

Comments
 (0)