• Javascript


    最近在重温数据结构,于是写了一些代码玩玩,都是很初级的,表喷各位。。。。

    function Stack() {
        this.dataStore = [];
        this.top = 0;
    }
    
    Stack.prototype = {
        length: function () {
            return this.top;
        }, push: function (element) {
            this.dataStore[this.top++] = element;
            return this.top;
        }, pop: function () {
            return this.dataStore[--this.top];
        }, peek: function () {
            return this.dataStore[this.top - 1];
        }, clear: function () {
            return this.top = 0;
        }
    };
    
    var stack = new Stack();
    console.log("length:" + stack.length());
    stack.push("huangjacky");
    stack.push("fiend");
    console.log("length:" + stack.length());
    while (stack.length() > 0) {
        var s = stack.pop();
        console.log("element:" + s);
    }
    console.log("length:" + stack.length());
    
    function Node(element) {
        this.element = element;
        this.next = null;
    }
    
    function LinkList() {
        this.head = new Node("head");
    }
    LinkList.prototype = {
        insert: function (element, after) {
            var s = new Node(element);
            if (after == undefined) { //如果没有指定after,那么就直接插入到链表的最前面
                s.next = this.head.next;
                this.head.next = s;
                return true;
            } else {
                var node = this.head;
                while (node != null) {
                    if (node.element == after) {
                        s.next = node.next;
                        node.next = s;
                        return true;
                    }
                    node = node.next;
                }
            }
            return false;
        }, remove: function (element) {
            var node = this.head;
            var preNode = null;
            while (node != null) {
                if (node.element == element) {
                    preNode.next = node.next;
                    return true;
                }
                preNode = node;
                node = node.next;
            }
            return false;
        }, find: function (element) {
            var node = this.head;
            while (node != null) {
                if (node.element == element) {
                    break;
                }
                node = node.next;
            }
            return node;
        }, toString: function () {
            var s = "";
            var node = this.head;
            while (node != null) {
                if (node.next == null) {
                    s += node.element;
                } else {
                    s += node.element + " --> ";
                }
                node = node.next;
            }
            return s;
        }
    };
    var l = new LinkList();
    l.insert("huangjacky");
    l.insert("fiend", "huangjacky");
    l.insert("jackyhuang", "fiend");
    l.insert("abc");
    var s = l.find("huangjacky");
    if (s) {
        console.log("next to huangjacky is :" + s.next.element);
    }
    l.remove("fiend");
    console.log("linklist is :" + l.toString());
    

      

  • 相关阅读:
    今天学会了如何察看SSDT里面的东西、修改里面的地址
    今天。。。。忙了一天
    我承认,我怕,我很怕
    WinCE下的第二个窗口程序
    终于搭好了WinCE上MFC的SDK环境
    原来腾迅的QQ号竟然是个int变量
    操了,编译器,你就不能少给我下几个断么
    谁能教我网络编程阿
    反汇编。。。本来以为很难,原来却这么容易
    PE头里的东西更多。。。越看越恶心了,我都不想看了
  • 原文地址:https://www.cnblogs.com/huangjacky/p/4004842.html
Copyright © 2020-2023  润新知