This is a cheatsheet for the homeworks of the Statistics course of the MSc in Cybersecurity @ Sapienza. It contains Javascript code snippets for the most useful data structures.
|
Declaration and initialization
let array = ['cyber', 'insecurity', 'sapienza'];let also = new Array(3); |
|
Adding an element
array[3] = 'statistics'; |
|
Setting a value
array[1] = 'security'; |
|
Deleting an element
delete array[3]; |
|
Looping
array.forEach(function(item) { console.log(item);}); |
|
Checking the existence of a value #1
array.includes('cyber'); |
|
Checking the existence of a value #2
array.indexOf('statistics') !== -1; |
|
Checking the existence of a value #3
for (let i = 0; i < array.length; i++) { if (array[i] === searchValue) { found = true; break; }} |
|
Checking the existence of a value #4
array.find(item => item === searchValue) !== undefined; |
|
Creation
let linkedList = new LinkedList(); |
|
Adding an element
linkedList.append(3);linkedList.prepend(1);linkedList.addAt(1, 3); |
|
Fetch of the head value
linkedList.fetchHead(); |
|
Get the value of a node
linkedList.getAt(2); |
|
Removing an element
linkedList.removeAt(1);linkedList.delete(7); |
|
Check existence of a value
linkedList.contains(7); |
|
Looping
for (const element of linkedList) { console.log(element);} |
|
Creation
const list = new List(); |
|
Adding an element
list.add(1);list.addAtHead(0); |
|
Removing an element
list.remove(2); |
|
Setting a value
list.set(0, 1337); |
|
Checking existence of a value
list.contains(1); |
|
Looping
for(let i = 0; i < list.size(); i++){ console.log(list.get(i));}); |
|
Creation
let dict = new Dictionary(); |
|
Adding an key-value pair
dict.add('frequency', 20); |
|
Setting a value for a key
dict.set('percentage', 40.10); |
|
Removing a key
dict.remove('percentage'); |
|
Checking existence of a key
dict.hasKey('frequency'); |
|
Checking existence of a value
dict.hasValue(20); |
|
Creation
let hashset = new HashSet(); |
|
Adding an element
hashset.add(1); |
|
Removing an element
hashset.delete(1); |
|
Checking the existence of an element
hashset.has(3); |
|
Looping
let keys = hashset.values();for(let i = 0; i < keys.length; i++){ console.log(keys[i]);}); |
|
Creation
let queue = new Queue(); |
|
Adding an element
queue.enqueue(1); |
|
Fetch of the first element
queue.front(); |
|
Removing an element
queue.dequeue(); |
|
Checking the existence of an element
queue.contains(7); |
|
Creation
let stack = new Stack(); |
|
Adding an element
stack.push(1); |
|
Fetching top of stack
stack.peek(); |
|
Removing an element
stack.pop(); |
|
Creation
let sortedSet = new SortedSet(); |
|
Adding an element
sortedSet.add(3); |
|
Removing an element
sortedSet.delete(3); |
|
Checking the existence of an element
sortedSet.has(1); |
|
Looping
for(element in sortedSet.toArray) { console.log(element);}); |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.