8000 Added methods to stack · AllAlgorithms/javascript@7edc54d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 7edc54d

Browse files
committed
Added methods to stack
1 parent daac950 commit 7edc54d

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

data-structures/stack.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* @author Rashik Ansar
2+
* @author Rashik Ansar and Luiz Guerra
3+
*
34
*
45
* Implemtaion of Stack data structure
56
* Stack follows LIFO (Last In First Out) priniciple
@@ -62,6 +63,42 @@ class Stack {
6263
}
6364
return this.first.data;
6465
}
66+
67+
/**
68+
* @returns size of the Stack
69+
*/
70+
size() {
71+
return this.size;
72+
}
73+
74+
/**
75+
* @returns if Stack is empty
76+
*/
77+
isEmpty() {
78+
return this.size == 0;
79+
}
80+
81+
/**
82+
* clears the Stack
83+
*/
84+
clear() {
85+
this.first = null;
86+
this.last = null;
87+
this.size = 0;
88+
}
89+
90+
/**
91+
* @returns the Stack
92+
*/
93+
toString() {
94+
let str = "";
95+
let aux = this.first;
96+
for (let i = 0; i < this.count; i++)
97+
str += aux.element + " ";
98+
aux = aux.next;
99+
return str;
100+
}
101+
65102
}
66103

67104
class Node {

0 commit comments

Comments
 (0)
0