1

JavaScript版数据结构与算法代码示例

JavaScript中实现数据结构和算法可以帮助你更好地理解计算机科学的基础概念,并提高你的编程技能。以下是一些常见的数据结构和算法的JavaScript实现示例:

数据结构数组 (Array)

JavaScript内置了数组数据结构。

示例操作:

push(), pop(), shift(), unshift(), splice(), slice(), map(), filter(), reduce()等。
  • 链表 (Linked List)
  • 自定义实现。
  • 包括单向链表和双向链表。

    javascript
    class Node {  
    constructor(data) {  
      this.data = data;  
      this.next = null;  
    }  
    }  
    
    class LinkedList {  
    constructor() {  
      this.head = null;  
    }  
    
    insertAtEnd(data) {  
      let newNode = new Node(data);  
      if (!this.head) {  
        this.head = newNode;  
      } else {  
        let lastNode = this.head;  
        while (lastNode.next) {  
          lastNode = lastNode.next;  
        }  
        lastNode.next = newNode;  
      }  
    }  
    
    // ... 其他链表操作  
    }
    栈 (Stack)
    可以使用数组模拟栈。
    javascript
    class Stack {  
    constructor() {  
      this.items = [];  
    }  
    
    push(element) {  
      this.items.push(element);  
    }  
    
    pop() {  
      if (this.isEmpty()) {  
        return "Underflow";  
      }  
      return this.items.pop();  
    }  
    
    peek() {  
      if (this.isEmpty()) {  
        return "No elements in Stack";  
      }  
      return this.items[this.items.length - 1];  
    }  
    
    isEmpty() {  
      return this.items.length == 0;  
    }  
    
    // ... 其他栈操作  
    }
    队列 (Queue)
    可以使用数组或链表模拟队列。
    javascript
    class Queue {  
    constructor() {  
      this.items = [];  
    }  
    
    enqueue(element) {  
      this.items.push(element);  
    }  
    
    dequeue() {  
      if (this.isEmpty()) {  
        return "Underflow";  
      }  
      return this.items.shift();  
    }  
    
    front() {  
      if (this.isEmpty()) {  
        return "No elements in Queue";  
      }  
      return this.items[0];  
    }  
    
    isEmpty() {  
      return this.items.length == 0;  
    }  
    
    // ... 其他队列操作  
    }
    哈希表 (Hash Table)
    使用JavaScript的对象来模拟哈希表。
    javascript
    class HashTable {  
    constructor() {  
      this.table = {};  
    }  
    
    put(key, value) {  
      this.table[key] = value;  
    }  
    
    get(key) {  
      return this.table[key] || null;  
    }  
    
    remove(key) {  
      if (this.table[key] !== undefined) {  
        delete this.table[key];  
        return true;  
      }  
      return false;  
    }  
    
    // ... 其他哈希表操作  
    }
    树 (Tree)

    包括二叉树、二叉搜索树、AVL树、红黑树等。

    javascript
    class TreeNode {  
    constructor(value) {  
      this.value = value;  
      this.left = null;  
      this.right = null;  
    }  
    }  
    
    class BinaryTree {  
    constructor() {  
      this.root = null;  
    }  
    
    insert(value) {  
      let newNode = new TreeNode(value);  
      if (this.root === null) {  
        this.root = newNode;  
      } else {  
        this.insertNode(this.root, newNode);  
      }  
    }  
    
    insertNode(node, newNode) {  
      if (newNode.value < node.value) {  
        if (node.left === null) {  
          node.left = newNode;  
        } else {  
          this.insertNode(node.left, newNode);  
        }  
      } else {  
        if (node.right === null) {  
          node.right = newNode;
    

忧郁的警车
9 声望1 粉丝