~zodiacsohma/armagetronad/vectron

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
*************************************************************************

Vectron - map editor for Armagetron Advanced.
Copyright (C) 2010 Carlo Veneziano (carlorfeo@gmail.com)

**************************************************************************

This file is part of Vectron.

Vectron is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vectron is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vectron.  If not, see <http://www.gnu.org/licenses/>.

*/

package orfaust.containers
{
    import orfaust.Debug

    public class LinkedList
    {
        private var _head:Object;
        private var _tail:Object;
        private var _length:uint;

        public function push(data:Object):void
        {
            var node = {prev:_tail,next:null,data:data};
            if(_tail)
                _tail.next = node;
            else
                _head = node;

            _tail = node;
            _length ++;
        }

        public function pop():Object
        {
            if(_tail == null)
                return null;

            var node = _tail;
            removeNode(node);
            return node.data;
        }

        public function get front():Object
        {
            return _tail.data;
        }
        public function get back():Object
        {
            return _head.data;
        }

        public function find(data:Object):Boolean
        {
            var node = findNode(data);
            return node != null;
        }
        public function remove(data:Object):Boolean
        {
            var node = findNode(data);
            if(node)
            {
                removeNode(node);
                return true;
            }
            return false;
        }

        private function findNode(data:Object):Object
        {
            var node = _head;
            while(node)
            {
                if(node.data == data)
                    return node;
                node = node.next;
            }
            return null;
        }

        private function removeNode(node:Object):void
        {
            if(node.prev)
                node.prev.next = node.next;

            if(node.next)
                node.next.prev = node.prev;

            if(node == _head)
                _head = node.next;

            if(node == _tail)
                _tail = node.prev;

            _length --;
        }

        public function get length():uint
        {
            return _length;
        }

        public function get iterator():ListIterator
        {
            return new ListIterator(this,_head);
        }

        public function empty():void
        {
            each(remove);
        }

        public function each(callBack:Function):void
        {
            var node = _head;
            while(node != null)
            {
                callBack(node.data);
                node = node.next;
            }
        }

        public function get getArray():Array
        {
            var node = _head;
            var objArray:Array = new Array();
            while(node != null)
            {
                objArray[objArray.length] = node.data;
                node = node.next;
            }
            return objArray;
        }
    }
}