~zodiacsohma/armagetronad/vectron

« back to all changes in this revision

Viewing changes to src/orfaust/containers/LinkedList.as

  • Committer: zodiacsohma1 at gmail
  • Date: 2014-01-22 02:46:23 UTC
  • Revision ID: zodiacsohma1@gmail.com-20140122024623-1dyhnemuhogi23gp
Converted all tab characters into spaces (4 gaps).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
package orfaust.containers
27
27
{
28
 
        import orfaust.Debug
29
 
 
30
 
        public class LinkedList
31
 
        {
32
 
                private var _head:Object;
33
 
                private var _tail:Object;
34
 
                private var _length:uint;
35
 
 
36
 
                public function push(data:Object):void
37
 
                {
38
 
                        var node = {prev:_tail,next:null,data:data};
39
 
                        if(_tail)
40
 
                                _tail.next = node;
41
 
                        else
42
 
                                _head = node;
43
 
 
44
 
                        _tail = node;
45
 
                        _length ++;
46
 
                }
47
 
 
48
 
                public function pop():Object
49
 
                {
50
 
                        if(_tail == null)
51
 
                                return null;
52
 
 
53
 
                        var node = _tail;
54
 
                        removeNode(node);
55
 
                        return node.data;
56
 
                }
57
 
 
58
 
                public function get front():Object
59
 
                {
60
 
                        return _tail.data;
61
 
                }
62
 
                public function get back():Object
63
 
                {
64
 
                        return _head.data;
65
 
                }
66
 
 
67
 
                public function find(data:Object):Boolean
68
 
                {
69
 
                        var node = findNode(data);
70
 
                        return node != null;
71
 
                }
72
 
                public function remove(data:Object):Boolean
73
 
                {
74
 
                        var node = findNode(data);
75
 
                        if(node)
76
 
                        {
77
 
                                removeNode(node);
78
 
                                return true;
79
 
                        }
80
 
                        return false;
81
 
                }
82
 
 
83
 
                private function findNode(data:Object):Object
84
 
                {
85
 
                        var node = _head;
86
 
                        while(node)
87
 
                        {
88
 
                                if(node.data == data)
89
 
                                        return node;
90
 
                                node = node.next;
91
 
                        }
92
 
                        return null;
93
 
                }
94
 
 
95
 
                private function removeNode(node:Object):void
96
 
                {
97
 
                        if(node.prev)
98
 
                                node.prev.next = node.next;
99
 
 
100
 
                        if(node.next)
101
 
                                node.next.prev = node.prev;
102
 
 
103
 
                        if(node == _head)
104
 
                                _head = node.next;
105
 
 
106
 
                        if(node == _tail)
107
 
                                _tail = node.prev;
108
 
 
109
 
                        _length --;
110
 
                }
111
 
 
112
 
                public function get length():uint
113
 
                {
114
 
                        return _length;
115
 
                }
116
 
 
117
 
                public function get iterator():ListIterator
118
 
                {
119
 
                        return new ListIterator(this,_head);
120
 
                }
121
 
 
122
 
                public function empty():void
123
 
                {
124
 
                        each(remove);
125
 
                }
126
 
 
127
 
                public function each(callBack:Function):void
128
 
                {
129
 
                        var node = _head;
130
 
                        while(node != null)
131
 
                        {
132
 
                                callBack(node.data);
133
 
                                node = node.next;
134
 
                        }
135
 
                }
136
 
                
137
 
                public function get getArray():Array
138
 
                {
139
 
                        var node = _head;
140
 
                        var objArray:Array = new Array();
141
 
                        while(node != null)
142
 
                        {
143
 
                                objArray[objArray.length] = node.data;
144
 
                                node = node.next;
145
 
                        }
146
 
                        return objArray;
147
 
                }
148
 
        }
 
28
    import orfaust.Debug
 
29
 
 
30
    public class LinkedList
 
31
    {
 
32
        private var _head:Object;
 
33
        private var _tail:Object;
 
34
        private var _length:uint;
 
35
 
 
36
        public function push(data:Object):void
 
37
        {
 
38
            var node = {prev:_tail,next:null,data:data};
 
39
            if(_tail)
 
40
                _tail.next = node;
 
41
            else
 
42
                _head = node;
 
43
 
 
44
            _tail = node;
 
45
            _length ++;
 
46
        }
 
47
 
 
48
        public function pop():Object
 
49
        {
 
50
            if(_tail == null)
 
51
                return null;
 
52
 
 
53
            var node = _tail;
 
54
            removeNode(node);
 
55
            return node.data;
 
56
        }
 
57
 
 
58
        public function get front():Object
 
59
        {
 
60
            return _tail.data;
 
61
        }
 
62
        public function get back():Object
 
63
        {
 
64
            return _head.data;
 
65
        }
 
66
 
 
67
        public function find(data:Object):Boolean
 
68
        {
 
69
            var node = findNode(data);
 
70
            return node != null;
 
71
        }
 
72
        public function remove(data:Object):Boolean
 
73
        {
 
74
            var node = findNode(data);
 
75
            if(node)
 
76
            {
 
77
                removeNode(node);
 
78
                return true;
 
79
            }
 
80
            return false;
 
81
        }
 
82
 
 
83
        private function findNode(data:Object):Object
 
84
        {
 
85
            var node = _head;
 
86
            while(node)
 
87
            {
 
88
                if(node.data == data)
 
89
                    return node;
 
90
                node = node.next;
 
91
            }
 
92
            return null;
 
93
        }
 
94
 
 
95
        private function removeNode(node:Object):void
 
96
        {
 
97
            if(node.prev)
 
98
                node.prev.next = node.next;
 
99
 
 
100
            if(node.next)
 
101
                node.next.prev = node.prev;
 
102
 
 
103
            if(node == _head)
 
104
                _head = node.next;
 
105
 
 
106
            if(node == _tail)
 
107
                _tail = node.prev;
 
108
 
 
109
            _length --;
 
110
        }
 
111
 
 
112
        public function get length():uint
 
113
        {
 
114
            return _length;
 
115
        }
 
116
 
 
117
        public function get iterator():ListIterator
 
118
        {
 
119
            return new ListIterator(this,_head);
 
120
        }
 
121
 
 
122
        public function empty():void
 
123
        {
 
124
            each(remove);
 
125
        }
 
126
 
 
127
        public function each(callBack:Function):void
 
128
        {
 
129
            var node = _head;
 
130
            while(node != null)
 
131
            {
 
132
                callBack(node.data);
 
133
                node = node.next;
 
134
            }
 
135
        }
 
136
 
 
137
        public function get getArray():Array
 
138
        {
 
139
            var node = _head;
 
140
            var objArray:Array = new Array();
 
141
            while(node != null)
 
142
            {
 
143
                objArray[objArray.length] = node.data;
 
144
                node = node.next;
 
145
            }
 
146
            return objArray;
 
147
        }
 
148
    }
149
149
}
 
 
b'\\ No newline at end of file'