-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstack_using_ll.js
More file actions
57 lines (57 loc) · 1.51 KB
/
stack_using_ll.js
File metadata and controls
57 lines (57 loc) · 1.51 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
// Creating a node
var StackNode = /** @class */ (function () {
function StackNode(value) {
this.value = value;
this.next = null;
}
return StackNode;
}());
// Class for Stack using LinkedList
var StackLinkedList = /** @class */ (function () {
function StackLinkedList() {
this.head = null;
this.tail = null;
}
StackLinkedList.prototype.add = function (value) {
var node = new StackNode(value);
// It is first node
if (this.head == null && this.tail == null) {
this.head = node;
this.tail = node;
}
else {
if (this.tail) {
this.tail.next = node;
this.tail = node;
}
}
};
StackLinkedList.prototype.remove = function () {
var _a;
if (((_a = this.head) === null || _a === void 0 ? void 0 : _a.next) == null) {
var temp = this.head;
this.head = null;
this.tail = null;
return temp === null || temp === void 0 ? void 0 : temp.value;
}
else {
var temp = this.head;
while (temp.next.next != null) {
temp = temp.next;
}
this.tail = temp;
this.tail.next = null;
}
};
return StackLinkedList;
}());
var StackLL = new StackLinkedList();
StackLL.add(6);
StackLL.add(10);
StackLL.add(14);
StackLL.add(18);
StackLL.add(22);
StackLL.add(26);
console.log(StackLL);
StackLL.remove();
console.log(StackLL);