/ Published in: ActionScript 3
URL: http://improve.dk/blog/2010/04/20/simple-as3-stack-implementation
Taken from http://improve.dk/blog/2010/04/20/simple-as3-stack-implementation
Expand |
Embed | Plain Text
package dk.improve.collections { internal final class StackNode { public var value:Object; public var next:StackNode; public function StackNode(value:Object):void { this.value = value; } } } package dk.improve.collections { public class Stack { private var head:StackNode; private var tail:StackNode; public function push(obj:Object):void { var newNode:StackNode = new StackNode(obj); if(head == null) head = newNode; else tail.next = newNode; tail = newNode; } public function pop():Object { var result:StackNode = head; if(result != null) { // If there's no next, the tail should be nulled too as it's == head if(head.next == null) tail = null; head = head.next; return result.value; } else return null; } public function peek():Object { if(head != null) return head.value; else return null; } } }
You need to login to post a comment.
