-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
69 lines (61 loc) · 1.33 KB
/
stack.js
File metadata and controls
69 lines (61 loc) · 1.33 KB
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
/**
* This is an implementation of custom stack class.
* @author Ramesh Kumar
*
*/
class Stack {
/**
* initialization of an object.
*/
constructor() {
this.counter = 0;
this.storage = {};
}
/**
* Adds a value at the end of the stack.
* @param value new value for the stack.
*/
push(value) {
this.storage[this.counter] = value;
this.counter++;
return this.counter;
}
/**
* Removes and returs the value at the end of the stack.
*/
pop() {
this.counter--;
var result = this.storage[this.counter];
delete this.storage[this.counter];
return result;
}
/**
* Returns size of the stack.
*/
size() {
return this.counter;
}
/**
* Returns a value at the end of the stack.
*/
peek() {
return this.storage[this.counter - 1];
}
/**
* Returns a string representation of an object.
*/
toString() {
return this.storage;
}
}
var stack = new Stack();
console.log(stack.push('A'));
console.log(stack.push('B'));
console.log(stack.push('C'));
console.log(stack.toString());
console.log(stack.size());
console.log(stack.pop());
console.log(stack.size());
console.log(stack.peek());
console.log(stack.size());
console.log(stack.toString());