Created
December 13, 2018 00:17
-
-
Save daverave1212/021308218d565a913a2acdad5b73b937 to your computer and use it in GitHub Desktop.
A simple Queue class. var q : Queue<T> = new Queue<T>() q.push(x) q.pop() trace(q.peek()) trace(q.length) trace(q.last)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package scripts; | |
| class Node<T>{ | |
| public var data : T; | |
| public var next : Node<T> = null; | |
| public function new(d : T){ | |
| data = d; | |
| } | |
| } | |
| class Queue<T> | |
| { | |
| public var first : Node<T> = null; // Left most, earliest, these are removed first | |
| public var last : Node<T> = null; | |
| public var length : Int = 0; | |
| public function new(){ | |
| } | |
| public function push(t : T){ | |
| if(length == 0){ | |
| first = new Node<T>(t); | |
| last = first; | |
| } else { | |
| var node = new Node<T>(t); | |
| last.next = node; | |
| last = node; | |
| } | |
| length++; | |
| } | |
| public function pop(){ | |
| if(length == 0){ | |
| // Do nothing | |
| } else if(length == 1){ | |
| first = null; | |
| last = null; | |
| length--; | |
| } else{ | |
| first = first.next; | |
| length--; | |
| } | |
| } | |
| public function peek(){ | |
| if(length == 0) return null; | |
| return first.data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment