Cheatography
                https://cheatography.com
            
        
        
    
                   
                            
    
                    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.
                    
                 
                    
        
        
            
    
        
                            
        
                
        
            
                                
            
                
                                                | Array
                        
                                                                                    
                                                                                            | 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 #1array.includes('cyber');
 |  
                                                                                            | Checking the existence of a value #2array.indexOf('statistics') !== -1;
 |  
                                                                                            | Checking the existence of a value #3for (let i = 0; i < array.length; i++) {
      if (array[i] === searchValue) {
          found = true;
          break;
      }
 }
 |  
                                                                                            | Checking the existence of a value #4array.find(item => item === searchValue) !== undefined;
 |  Arrays are also used to implement List, Queue, Stack. For these data structures I'll create a JS class that exposes useful methods. LinkedList
                        
                                                                                    
                                                                                            | Creationlet linkedList = new LinkedList();
 |  
                                                                                            | Adding an elementlinkedList.append(3);
 linkedList.prepend(1);
 linkedList.addAt(1, 3);
 |  
                                                                                            | Fetch of the head valuelinkedList.fetchHead();
 |  
                                                                                            | Get the value of a nodelinkedList.getAt(2);
 |  
                                                                                            | Removing an elementlinkedList.removeAt(1);
 linkedList.delete(7);
 |  
                                                                                            | Check existence of a valuelinkedList.contains(7);
 |  
                                                                                            | Loopingfor (const element of linkedList) {
     console.log(element);
 }
 |  |  | List
                        
                                                                                    
                                                                                            | Creationconst list = new List();
 |  
                                                                                            | Adding an elementlist.add(1);
 list.addAtHead(0);
 |  
                                                                                            | Removing an elementlist.remove(2);
 |  
                                                                                            | Setting a valuelist.set(0, 1337);
 |  
                                                                                            | Checking existence of a valuelist.contains(1);
 |  
                                                                                            | Loopingfor(let i = 0; i < list.size(); i++){
     console.log(list.get(i));
 });
 |  The type SortedList has the same methods, but ensures that the values are sorted. Dictionary
                        
                                                                                    
                                                                                            | Creationlet dict = new Dictionary();
 |  
                                                                                            | Adding an key-value pairdict.add('frequency', 20);
 |  
                                                                                            | Setting a value for a keydict.set('percentage', 40.10);
 |  
                                                                                            | Removing a keydict.remove('percentage');
 |  
                                                                                            | Checking existence of a keydict.hasKey('frequency');
 |  
                                                                                            | Checking existence of a valuedict.hasValue(20);
 |  HashSet
                        
                                                                                    
                                                                                            | Creationlet hashset = new HashSet();
 |  
                                                                                            | Adding an elementhashset.add(1);
 |  
                                                                                            | Removing an elementhashset.delete(1);
 |  
                                                                                            | Checking the existence of an elementhashset.has(3);
 |  
                                                                                            | Loopinglet keys = hashset.values();
 for(let i = 0; i < keys.length; i++){
     console.log(keys[i]);
 });
 |  The HashSet is pretty similar to the Dictionary, but it doesn't have values; it just uses the keys. |  | Queue
                        
                                                                                    
                                                                                            | Creationlet queue = new Queue();
 |  
                                                                                            | Adding an elementqueue.enqueue(1);
 |  
                                                                                            | Fetch of the first elementqueue.front();
 |  
                                                                                            | Removing an elementqueue.dequeue();
 |  
                                                                                            | Checking the existence of an elementqueue.contains(7);
 |  Stack
                        
                                                                                    
                                                                                            | Creationlet stack = new Stack();
 |  
                                                                                            | Adding an elementstack.push(1);
 |  
                                                                                            | Fetching top of stackstack.peek();
 |  
                                                                                            | Removing an elementstack.pop();
 |  SortedSet
                        
                                                                                    
                                                                                            | Creationlet sortedSet = new SortedSet();
 |  
                                                                                            | Adding an elementsortedSet.add(3);
 |  
                                                                                            | Removing an elementsortedSet.delete(3);
 |  
                                                                                            | Checking the existence of an elementsortedSet.has(1);
 |  
                                                                                            | Loopingfor(element in sortedSet.toArray) {
     console.log(element);
 });
 |  | 
            
                            
            
            
        
        
        
        
        
            
    
        
          
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets