flash as3打造双向链表

数据结构中的双向链表,用AS3写成了类,很有用的东西,可能有些非程序出身的flasher 并不了解的数据结构。如果把数组理解为把数据排成队,那么链表就是手拉手的排队,不过每个人只知道自己,还有自己的前面与后面,如果用类的思想来理解就是:有三个数据属性的对象:1、自己 2、自己后面的人 3、自己前面的人 和对这些属性进行操作的方法。希望对大家有用 这样的结构在频繁 删除和插入数据时效率会比数组高很多。
                                                                                                                                                                                                                                        

package {

    public class DoubleNode {
        //双向链表, double list node
        private var _data:*;
        private var _prev:DoubleNode;
        private var _next:DoubleNode;
        //标志是否已在链表中
        private var _hasIN:Boolean=false;
        //@initData:结点初始值, the node’ data;
        public function DoubleNode(initData:*) {
            _data = initData;
            _prev = _next = null;
        }
        //this node has in link
        public function get hasIN():Boolean {
            return _hasIN;
        }
        public function set hasIN(boolean:Boolean):void {
            _hasIN=boolean;
        }
        //后继节点
        public function get next():DoubleNode {
            return _next;
        }
        //前驱节点
        public function get prev():DoubleNode {
            return _prev;
        }
        //设定后继
        public function set next(newNode:DoubleNode):void {
            _next = newNode;
        }
        //设定前驱
        public function set prev(newNode:DoubleNode):void {
  _prev = newNode;
        }
        //取得当前结点数据, return the node’s data
        public function get nodeData():* {
            return _data;
        }
        //设置当前结点数据, set the node‘s data
        public function set nodeData(newData:*):void {
            _data = newData;
        }
        //在当前节点后插入结点, append a new node after current node
        public function addAfter(newNode:DoubleNode):void {
            newNode.next = _next;
            newNode.prev = this;
            if (_next!=null) {
                _next.prev = newNode;
            }
            _next = newNode;
            newNode.hasIN=true;
        }
        //在当前节点前插入结点, append a new node before current node
        public function addBefore(newNode:DoubleNode):void {
            newNode.next = this;
            newNode.prev = _prev;
            if (_prev!=null) {
                _prev.next = newNode;
            }
            _prev = newNode;
            newNode.hasIN=true;
        }
        //返回当前数组长度, get the length of the node
        public function get nodeLength():uint {
            var cursor:DoubleNode;
            var length:uint = 1;
            for (cursor = _prev; cursor != null; cursor = cursor.prev) {
                length++;
            }
            return length;
        }
        //从双向链表中脱离。 get out of link to the double-node
        public function unlink():void {
            if (_prev!=null) {
                _prev.next = _next;
            }
            if (_next!=null) {
                _next.prev = _prev;
            }
            _next = _prev = null;
_hasIN=false;
        }
        //描述当前double-node, rerurn a string represent the double-node
        public function toString():String {
            return "[DoubleNode, data=" + _data + "]";
        }
    }
}

阅读更多
文章标签: Flash 数据结构
个人分类: actionscript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭