-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_stack.js
More file actions
115 lines (94 loc) · 2.4 KB
/
queue_stack.js
File metadata and controls
115 lines (94 loc) · 2.4 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function Queue(){
var elements = [];
this.add = add;
this.remove = remove;
this.getFirstElement = getFirstElement;
this.hasElements = hasElements;
this.removeAll = removeAll;
this.size = size;
this.toString = toString;
function add(element){
elements.push(element);
}
function remove(){
return elements.shift();
}
function getFirstElement(){
return elements[0];
}
function hasElements(){
return elements.length > 0;
}
function removeAll(){
elements = [];
}
function size(){
return elements.length;
}
function toString(){
return elements.toString();
}
}
function Stack(){
var elements = [];
this.add = add;
this.remove = remove;
this.getLastElement = getLastElement;
this.hasElements = hasElements;
this.removeAll = removeAll;
this.size = size;
this.toString = toString;
function add(element){
elements.push(element);
}
function remove(){
return elements.pop();
}
function getLastElement(){
return elements[elements.length - 1];
}
function hasElements(){
return elements.length > 0;
}
function removeAll(){
elements = [];
}
function size(){
return elements.length;
}
function toString(){
elements = elements.reverse();
return elements.toString();
}
}
console.log('Queue');
var peopleQueue = new Queue();
console.log('Are there elements?: '+peopleQueue.hasElements());
console.log('Add elements...');
peopleQueue.add('Fulano');
peopleQueue.add('Mengano');
peopleQueue.add('Perengano');
console.log('Elements: '+peopleQueue.toString());
console.log('Elements total: '+peopleQueue.size());
console.log('Are there elements: '+peopleQueue.hasElements());
console.log('First element added: '+peopleQueue.getFirstElement());
console.log('Remove elements...');
peopleQueue.remove();
peopleQueue.remove();
console.log('Elements: '+peopleQueue.toString());
console.log('Stack');
var fruitStack = new Stack();
console.log('Are there elements?: ' + fruitStack.hasElements());
console.log('Add elements...');
fruitStack.add('Orange');
fruitStack.add('Apple');
fruitStack.add('Banana');
fruitStack.add('Peach');
console.log('Elements: '+fruitStack.toString());
console.log('Elements total: '+fruitStack.size());
console.log('Are there elements: '+fruitStack.hasElements());
console.log('Last element added: '+fruitStack.getLastElement());
console.log('Remove elements...');
fruitStack.remove();
fruitStack.remove();
console.log('Last element added: '+fruitStack.getLastElement());