-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathref-queue-stack.js
More file actions
82 lines (66 loc) · 1.41 KB
/
ref-queue-stack.js
File metadata and controls
82 lines (66 loc) · 1.41 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
70
71
72
73
74
75
76
77
78
79
80
// Example linked list class (singly)
function LinkedListNode(value) {
this.value;
this.next = null;
}
// Example binary search tree
function BinarySearchTree(value) {
this.value = value;
this.left = null;
this.right = null;
}
BinarySearchTree.prototype.insertLeft = function(value) {
this.left = new BinarySearchTree(value);
return this.left;
};
BinarySearchTree.prototype.insertRight = function(value) {
this.right = new BinarySearchTree(value);
return this.right;
};
// Example stack
function Stack() {
this.data = [];
this.top = 0;
}
Stack.prototype.push = function(element) {
this.data[this.top++] = element;
};
Stack.prototype.pop = function() {
return this.data[--this.top];
};
Stack.prototype.peek = function() {
return this.data[this.top - 1];
};
Stack.prototype.clear = function() {
this.top = 0;
};
Stack.prototype.length = function() {
return this.top;
};
// Example queue
function Queue() {
this.data = [];
}
Queue.prototype.enqueue = function(element) {
this.data.push(element);
};
Queue.prototype.dequeue = function(element) {
this.data.shift();
};
Queue.prototype.front = function() {
return this.data[0];
};
Queue.prototype.back = function() {
return this.data[this.data.length - 1];
};
queue.prototype.empty = function() {
if (this.data.length == 0) {
return true;
} else {
return false;
}
};
// Example hash table class
function HashTable() {
this.table = new Array(137);
}