天天看點

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 + "]";

        }

    }

}

繼續閱讀