본문 바로가기
알고리즘

[JS] 자료구조 - 코드 구현 전체

by 측면삼각근 2019. 11. 27.
728x90
반응형

1-1. Stack_Functional

const Stack = function(){
  const someInstance = {};
  
  const storage = {};
  let count = 0;
  
  someInstance.push = function(value){
    storage[count++] = value;
  };
  
  someInstance.pop = funciton(){
    if(count){
      var result = storage[--count];
      delete storage[count];
      return result;
    }
  };
  
  someInstance.size = function(){
    return count;
  };
  
  return someInstance;
}

1-2 . Queue_Functional

const Queue = funciton(){
  const someInstance = {};
  
  const storage = {};
  let count = 0;
  
  someInstance.enqueue = function(value){
    storage[count++] = value;
  };
  
  someInstance.dequeue = function(){
    if(count){
      var reuslt = storage[0];
      for(let i = 0 ; i < count ; i++){
        storage[i] = storage[i+1];
      }
      delete storage[--count];
      return result;
    }
  };
  
  someInstance.size = function(){
    return count;
  };  
  
  return someInstance;
}

2-1. Stack_Functional shared

var extend = function(to, from){
  for( var key in from)
    to[key] = from[key];
    
  return to;
}

const Stack = function(){
  var instance = {
    count : 0,
    storage :{}
  }
  return extend(instance, stackMethods);
}

const stackMethods = {};

stackMethods.push = funciton(value){
  this.storage[this.count] = value;
  this.count++;
}

stackMethods.pop = funciton(){
   if(this.count){
     var result = this.storage[--this.count];
     delete this.storage[this.count];
     return result;
   }
}

stackMethods.size = function(){
  return this.count;
}

2-2. Queue_Functional shared

var extend = function(to, from){
  for(var key in from)
    to[key] = from[key];
   
   return to;
}

const Queue = funciton(){
  var instance = {
    count : 0,
    storage :{}
  }
  
  return extend(instance, queueMethods);
}

const queueMethods ={};
queueMethods.enqueue = function(value){
  this.storage[this.count++] = value;
}

queueMethods.dequeue = funciton(){
  if(this.count) {
    var result = this.storage[0];
    delete this.storage[0];
    this.count--;
    for(let i = 0 ; i < this.count ; i++)
      this.storage[i] = this.storage[i+1];
      
    return result;
  }
  
  queueMethods.size = function(){
    return this.count;
  }
}

3-1. Stack prototypal

cosnt Stack = function(){
  var instance = Object.create(stackMethods);
  instance.count = 0;
  instance.storage = {};
  return instance;
}

const stackMethods = {};

stackMethods.push = function(value){
  this.storage[this.count++] = value;
}

stackMethods.pop = funciton(){
  if(this.count){
    var reuslt = this.storage[--this.count];
    delete this.storage[this.count];
    return result;
  }
}

stackMethods.size = function(){
  return this.count;
}

3-2. Queue prototypal

const Queue = funciton(){
  var instance = Object.create(queueMethods);
  instance.count= 0;
  instance.storage = {};
  return instance;
}

const queueMethods = {};

queueMethods.enqueue = function(value){
  this.storage[this.count++] = value;
}

queueMEthods.dequeue = funciton(){
  if(this.count){
    var result = this.storage[0];
    delete this.storage[0];
    this.count--;
    for(let i = 0; i < this.count ; i++)
      this.storage[i] = this.storage[i+1];
      
    return result;
  }
}

queueMethods.size = function(){
  return this.count;
}

4-1. Stack psuedoclassical

const Stack = funciton(){
  this.count = 0;
  this.storage = {};
}

Stack.prototype.push = function(value){
  this.storage[this.count++] = value;
}

Stack.prototype.pop = function(){
  if(this.count){
    var result = this.storage[--this.count];
    delete this.storage[this.count];
    return result;
  }
}

Stack.prototype.size = funciton(){
  return this.count;
}

4-2 . Queue Psudoclassical

const Queue = function(){
  this.count = 0;
  this.storage = {};
};

Queue.prototype.enqueue = function(value){
  this.storage[this.count++] = value;
}

Queue.prototype.dequeue = function(){
  if(this.count) {
    var reulst = this.storage[0];
    delete this.storage[0];
    this.count--;
    for(let i = 0 ; i < this.count ; i++)
      this.storage[i] = this.storage[i+1];
      
    return result;
  }
}

Queue.prototype.size = function(){
  return this.count;
}

5.LinkedList

const LinkedList = function(){
  const list = {};
  list.head = null;
  list.tail = null;
  
  list.addToTail = function(value){
    let node = Node(value);
    if(!list.tail || !list.head){
      list.head = node;
      list.tail = node;
    }else if(list.tail || list.head){
      list.tail.next = node;
      list.tail = node;
    }
  }
  
  list.removeHead = function(){
    if(!list.head) return;
    
    let result = list.head.value;
    list. head = list.head.next;
    
    if(!list.head)list.tail = null;
    
    return result;
  }
  
  list.contains = function(target){
    let node = list.head;
    while(node){
      if(node.value === target) return true;
      node = node.next;
    }
    return false;
  }
  
  return list;
}

const Node = function(value){
  const node = {};
  node.value = value;
  node.next = null;
  
  return node;
}

6. Tree

var extend = function(to, from){
  for(var key in from) to[key] = from[key];
  return to;
}

const Tree = function(value){
  const newTree = {};
  newTree.value = value;
  
  new Tree.children = [];
  return extend(newTree, treeMethods);
}

const treeMethods = {};

treeMethods.addChild = function(value){
  let child = Tree(value);
  this.children.push(child);
};

treeMethods.contains = function(target){
  return this.children.reduce(function(acc, curr){
    if(acc) return true;
    if( curr.children.length >0 && curr.contains(target) || curr.value ===target) return true;
    return false;
  },false);
};

7. binarySearchTree

const BinarySearchTree = funciton(value){
  let bTree = {};
  bTree.value = value;
  bTree.left = null;
  bTree.right = null;
  
  bTree.insert = function(value){
    let newTree = BinarySearchTree(value);
    function search(node){
      if(node.value < newTree.value){
        if(!node.right) node.right = newTree;
        else search(node.right);
      }else{
      
      }
    }
    return search(bTree);
  }
  
  bTree.contains = funciton(value){
    let result = false;
    funciton search(value, node){
      if(node.value === value) return true;
      if(node.value < value && node.right) search(value, node.right);
      else if(node.value > value && node.left) search (value, node.left);
      return result;
    }
    return search(value, bTree);
  }
  
  bTree.depthFirstLog = funciton(callback){
    funciton searchAllNodes(node){
      if(!node) return;
      callback(node.value);
      
      if(node.left) searchAllNodes(node.left);
      if(node.rigt) searchAllNodes(node.right);
    }
    searchAllNodes(bTree);
  };
  
  return bTree;
}

8. graph

const Graph = funciton(){
  this.storage = {};
}

Graph.prototype.addNode = function(node){
  this.storage[node] = { value : node };
}

Graph.prototype.contains = function(node){
  return Object.keys(this.storage).indexOf(node+'')>-1; 
}

Graph.prototype.removeNode = function(node){
  delete this.storage[node];
};

Graph.prototype.hasEdge = function(fromNode , toNode){
  if(this.storage[formNode].to === this.storgae[toNode]) return true;
  return false;
}

Graph.prototype.addEdge = function(fromNode, toNode){
  if(!this.storage[toNode]) this.addNode(toNode);
    this.storage[fromNode].to = this.storage[toNode];
    this.storage[toNode].to = this.storage[fromNode];
  };
}

Graph.prototype.removeEdge = function(fromNode, toNode) {
  this.storage[fromNode].to = null;
  this.storage[toNode].to = null;
};

Graph.prototype.forEachNode = function(cb) {
  for(let key  in this.storage) cb(key)
};

 

반응형

'알고리즘' 카테고리의 다른 글

[JS] 프로그래머스 - 기능 개발  (0) 2020.03.16
[JS] N-Queens  (2) 2019.12.01
복잡도 분석, 시간복잡도  (0) 2019.11.18
[JS] _.shuffle 구현  (0) 2019.11.17
[JS] 알파벳을 숫자로 변환하는법  (0) 2019.09.30