forked from ManspergerMichael/Algorithims
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.js
86 lines (79 loc) · 1.55 KB
/
Stack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//array baised stack
class Stack{
top = 0;
storage = {};
push = function(value){
storage[top] = value;
top ++;
}
pop = function(){
if(top === 0){
return undefined;
}
top--;
var result = storage[top];
delete storage[top];
return result;
}
peek = function(){
return storage[top-1];
}
contains = function(value){
for(var i = top -1; i > 0; i--){
if(storage[i] === value){
return true;
}
}
return false;
}
isEmpty = function(){
if(top === 0){
return true;
}
else{
return false;
}
}
size = function(){
return top;
}
}
//singly linked list stack
class Node {
val = val;
next = undefined;
print = function(){
console.log(val);
}
}
class SLStack {
top = undefined;
push = function(val){
var NewNode = new Node(val);
if(top === undefined){
top = NewNode;
}
else{
NewNode.next = top;
top = NewNode;
}
}
pop = function(){
var result = top;
top = top.next;
return result;
}
}
//array stack testing
/* var daStack = new Stack();
console.log(daStack.isEmpty());
daStack.push(1);
daStack.push(2);
daStack.push(3);
console.log(daStack.size());
console.log(daStack.contains(3)); */
//SL Stack testing
var stack = new SLStack();
stack.push(1);
stack.push(2);
console.log(stack.pop());