js数据结构

js列表

function List() {
    this.listsize = 0;
    this.pos = 0;
    this.dataSource = [];
    this.clear = clear;
    this.find = find;
    this.toString = toString;
    this.insert = insert;
    this.append = append;
    this.remove = remove;
    this.front = front;
    this.end = end;
    this.prev = prev;
    this.next = next;
    this.length = length;
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
    this.contains = contains;
}

function append(element) {
    this.dataSource[this.listsize] = element;
    this.listsize++;
}

function find(element) {
    for (let i = 0; i < this.dataSource.length; i++) {
        if (this.dataSource[i] == element)
            return i;
    }
    return -1;
}

function remove(element) {
    let index = this.find(element);
    if (index != -1) {
        this.dataSource.splice(index, 1);
        this.listsize--;
        return true;
    }
    return false;
}

function length() {
    return this.listsize;
}

function toString() {
    return this.dataSource;
}

function insert(element, after) {
    let index = find(after);
    if (index != -1) {
        this.dataSource.splice(index + 1, 0, element);
        this.listsize++;
        return true;
    }
    return false;
}

function clear() {
    this.dataSource = [];
    this.listsize = this.pos = 0;
}

function contains(element) {
    for (let i = 0; i < this.dataSource.length; i++) {
        if (this.dataSource[i] == element)
            return true;
    }
    return false;
}

function front() {
    this.pos = 0;
}
function end() {
    this.pos = this.listSize - 1;
}
function prev() {
    if (this.pos > 0) {
        --this.pos;
    }
}
function next() {
    if (this.pos < this.listSize - 1) {
        ++this.pos;
    }
}
function currPos() {
    return this.pos;
}
function moveTo(position) {
    this.pos = position;
}
function getElement() {
    return this.dataStore[this.pos];
}


var names = new List();
names.append("Cynthia");
names.append("Raymond");
names.append("Barbara");
console.log(names.toString());
names.remove("Raymond");
console.log(names.toString());


function Stack() {
    this.dataStore = [];
    this.top = 0;
    this.push = push;
    this.pop = pop;
    this.peek = peek;
    this.clear = clear;
    this.length = length;
}

function push(element) {
    this.dataStore[this.top] = element;
    this.top++;
}

function pop() {
    return this.dataStore[--this.top];
}

function peek() {
    return this.dataStore[this.top - 1];
}

function clear() {
    this.dataStore = [];
    this.top = 0;
}

function length() {
    return this.top;
}

var s = new Stack();
s.push("David");
s.push("Raymond");
s.push("Bryan");
console.log("length: " + s.length());
console.log(s.peek());
var popped = s.pop();
console.log("The popped element is: " + popped);
console.log(s.peek());
s.push("Cynthia");
console.log(s.peek());
s.clear();
console.log("length: " + s.length());
console.log(s.peek());
s.push("Clayton");
console.log(s.peek());

队列

function Queue() {
    this.dataStore = [];
    this.enqueue = enqueue;
    this.dequeue = dequeue;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
}

//向队尾添加元素
function enqueue(element) {
    this.dataStore.push(element);
}

//删除队首元素
function dequeue() {
    return this.dataStore.shift();
}

//读取队首元素
function front() {
    return this.dataStore[0];
}

//读取队尾元素
function back() {
    return this.dataStore[this.dataStore.length - 1];
}

function toString() {
    return this.dataStore;
}

function empty() {
    if (this.dataStore.length == 0)
        return true;
    else
        return false;
}

var q = new Queue();
q.enqueue("Meredith");
q.enqueue("Cynthia");
q.enqueue("Jennifer");
console.log(q.toString());
q.dequeue();
console.log(q.toString());
console.log("Front of queue: " + q.front());
console.log("Back of queue: " + q.back());

链表

单向链表

function Node(element) {
    this.element = element;
    this.next = null;
}

function LList() {
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
    this.remove = remove;
    this.display = display;
    this.findPrevious = findPrevious;
}

function find(element) {
    var currNode = this.head;
    while (currNode.element != element) {
        currNode = currNode.next;
    }
    return currNode;
}

function insert(element, item) {
    var newNode = new Node(element);
    var currNode = this.find(item);
    newNode.next = currNode.next;
    currNode.next = newNode;
}

function display() {
    var currNode = this.head;
    while (currNode.next != null) {
        console.log(currNode.element);
        currNode = currNode.next;
    }
}

function remove(element) {
    var newNode = findPrevious(element);
    var currNode = find(element);
    newNode.next = currNode.next;

}

function findPrevious(element) {
    var preNode = this.head;
    while (preNode.next && preNode.next.element != element) {
        preNode = preNode.next;
    }
    return preNode;
}

var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Alma", "Russellville");
cities.display()

双向链表

function Node(element) {
    this.element = element;
    this.next = null;
    this.previous = null;
}

function LList() {
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
    this.display = display;
    this.remove = remove;
    this.findLast = findLast;
    this.dispReverse = dispReverse;
}

function dispReverse() {
    var lastNode = findLast();
    while (lastNode.previous) {
        console.log(lastNode.element);
        lastNode = lastNode.previous;
    }
}

function findLast() {
    var curNode = this.head;
    while (curNode.next) {
        curNode = curNode.next;
    }
    return curNode;
}

function find(element) {
    var curNode = this.head;
    while (curNode.element != element) {
        curNode = curNode.next;
    }
    return curNode;
}

function insert(newElement, item) {
    var newNode = new Node(newElement);
    var current = this.find(item);
    newNode.next = current.next;
    newNode.previous = current;
    current.next = newNode;
}

function display() {
    var curNode = this.head;
    while (curNode.next) {
        console.log(curNode.element);
        curNode = curNode.next;
    }
}

function remove(item) {
    var currNode = this.find(item);
    if (!(currNode.next == null)) {
        currNode.previous.next = currNode.next;
        currNode.next.previous = currNode.previous;
        currNode.next = null;
        currNode.previous = null;
    }
}

var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Carlisle", "Russellville");
cities.insert("Alma", "Carlisle");
cities.display();
console.log();
cities.remove("Carlisle");
cities.display();
console.log();
cities.dispReverse();

字典

function Dictionary() {
    this.add = add;
    this.dataStore = new Array();
    this.find = find;
    this.remove = remove;
    this.showAll = showAll;
    this.count = count;
    this.clear = clear;
}


function add(key, value) {
    this.dataStore[key] = value;
}

function find(key) {
    return this.dataStore[key];
}

function remove(key) {
    delete this.dataStore[key];
}

function showAll() {
    for (var key in this.dataStore) {
        console.log(key + "->" + this.dataStore[key]);
    }
}

function count() {
    var sum = 0;
    for (var key in this.dataStore) {
        sum++;
    }
    return sum;
}

function clear() {
    delete this.dataStore;
}
var pbook = new Dictionary();
pbook.add("Raymond", "123");
pbook.add("David", "345");
pbook.add("Cynthia", "456");
console.log("Number of entries: " + pbook.count());
console.log("David's extension: " + pbook.find("David"));
pbook.showAll();
pbook.clear();
console.log("Number of entries: " + pbook.count());

散列

function HashTable() {
    this.table = new Array(137);
    this.simpleHash = simpleHash;
    this.showDistro = showDistro;
    this.put = put;
}

function put(data) {
    var key = this.simpleHash(data);
    this.table[key] = data;
}

function simpleHash(data) {
    var total = 0;
    for (let i = 0; i < data.length; i++) {
        total += data.charCodeAt(i);
    }
    return total % this.table.length;
}

function showDistro() {
    var n = 0;
    for (var i = 0; i < this.table.length; ++i) {
        if (this.table[i] != undefined) {
            console.log(i + ": " + this.table[i]);
        }
    }
}
someNames = ["David", "Jennifer", "Donnie", "Raymond",
    "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"];

var hTable = new HashTable();
for (var i = 0; i < someNames.length; ++i) {
    hTable.put(someNames[i]);
}
hTable.showDistro();

集合

function Set() {
    this.dataStore = [];
    this.add = add;
    this.remove = remove;
    this.size = size;
    this.union = union;
    this.intersect = intersect;
    this.subset = subset;
    this.difference = difference;
    this.show = show;
    this.contains = contains;
}

function add(data) {
    if (this.dataStore.indexOf(data) == -1) {
        this.dataStore.push(data);
        return true;
    }
    else
        return false;
}

function remove(data) {
    var index = this.dataStore.indexOf(data);
    if (index != -1) {
        this.dataStore.splice(index, 1);
        return true;
    } else
        return false;

}

function size() {
    return this.dataStore.size;
}

function show() {
    return this.dataStore;
}

function contains(data) {
    if (this.dataStore.indexOf(data) != -1) {
        return true;
    }
    else
        return false;
}

function union(set) {
    var tempSet = new Set();
    for (var i = 0; i < this.dataStore.length; ++i) {
        tempSet.add(this.dataStore[i]);
    }
    for (var i = 0; i < set.dataStore.length; ++i) {
        if (!tempSet.contains(set.dataStore[i])) {
            tempSet.dataStore.push(set.dataStore[i]);
        }
    }
    return tempSet;
}

function intersect(set) {
    var tempSet = new Set();
    for (var i = 0; i < this.dataStore.length; ++i) {
        if (set.contains(this.dataStore[i])) {
            tempSet.add(this.dataStore[i]);
        }
    }
    return tempSet;
}

function subset(set) {
    if (this.size() > set.size()) {

        return false;
    }
    else {
        for (var member in this.dataStore) {
            if (!set.contains(member)) {
                return false;
            }
        }
    }
    return true;
}

function difference(set) {
    var tempSet = new Set();
    for (var i = 0; i < this.dataStore.length; ++i) {
        if (!set.contains(this.dataStore[i])) {
            tempSet.add(this.dataStore[i]);
        }
    }
    return tempSet;
}

var it = new Set();
it.add("Cynthia");
it.add("Clayton");
it.add("Jennifer");
it.add("Danny");
it.add("Jonathan");
it.add("Terrill");
it.add("Raymond");
it.add("Mike");
var dmp = new Set();
dmp.add("Cynthia");
dmp.add("Raymond");
dmp.add("Jonathan");
if (dmp.subset(it)) {
    console.log("DMP is a subset of IT.");
}
else {
    console.log("DMP is not a subset of IT.");
}

二叉树

function Node(data) {
    this.data = data;
    this.right = null;
    this.left = null;
    this.show = show;
}

function show() {
    return this.data;
}

function BST() {
    this.root = null;
    this.insert = insert;
    this.inOrder = inOrder;
    this.preOrder = preOrder;
    this.postOrder = postOrder;
    this.getMin = getMin;
    this.getMax = getMax;
    this.find = find;
    this.remove = remove;
}


function insert(data) {
    var newNode = new Node(data);
    if (this.root == null)
        this.root = newNode;
    else {
        var current = this.root;
        while (true) {
            if (data > current.data) {
                if (current.right == null) {
                    current.right = newNode;
                    break;
                } else
                    current = current.right;
            } else {
                if (current.left == null) {
                    current.left = newNode;
                    break;
                } else
                    current = current.left;
            }
        }
    }
}

function inOrder(node) {
    if (node != null) {
        inOrder(node.left);
        console.log(node.data);
        inOrder(node.right);
    }
}

function preOrder(node) {
    if (node) {
        console.log(node.data);
        preOrder(node.left);
        preOrder(node.right);
    }
}

function postOrder(node) {
    if (node) {
        postOrder(node.left);
        postOrder(node.right);
        console.log(node.data);
    }
}

function getMin() {
    var current = this.root;
    while (current.left != null)
        current = current.left;
    return current.data;
}

function getMax() {
    var current = this.root;
    while (current.right != null)
        current = current.right;
    return current.data;
}


function find(data) {
    var current = this.root;
    while (current) {
        if (current.data == data)
            return current;
        else if (current.data < data)
            current = current.right;
        else
            current = current.left;
    }
    return null;
}

function remove(data) {
    root = removeNode(this.root, data);
}
function removeNode(node, data) {
    if (node == null) {
        return null;
    }
    if (data == node.data) {
        // 没有子节点的节点
        if (node.left == null && node.right == null) {
            return null;
        }
        // 没有左子节点的节点
        if (node.left == null) {
            return node.right;
        }
        // 没有右子节点的节点
        if (node.right == null) {
            return node.left;
        }
        // 有两个子节点的节点
        var tempNode = getSmallest(node.right);
        node.data = tempNode.data;
        node.right = removeNode(node.right, tempNode.data);
        return node;
    }
    else if (data < node.data) {
        node.left = removeNode(node.left, data);
        return node;
    }
    else {
        node.right = removeNode(node.right, data);
        return node;
    }
}

var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
inOrder(nums.root);
console.log("\n");
var value = parseInt(23);
var found = nums.find(value);
if (found != null) {
    console.log("Found " + value + " in the BST.");
}
else {
    console.log(value + " was not found in the BST.");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值