Wednesday, March 18, 2009

Stack Component in Flex /AIR / AS3

While working on a auditors project i had a scenario where i was to upload files on every object of the for loop and i was to upload the file on the server with a returning id from the previous action , so that the coming files will be uploaded as the child of the previous files(As in the tree structure). I had to create a Stack class which solved my problem. The function defined are pop(), push() and peek().
I will be adding Linked List Component shortly......

Here is the Code :-

a) Stack Class

package
{

public class Stack
{
private var first : Node;

public function isEmpty ():Boolean
{
return first == null;
}

public function push (data : Object):void
{
var oldFirst : Node = first;
first = new Node ();
first.data = data;
first.next = oldFirst;
}

public function pop () : Object
{
if (isEmpty ())
{
trace ("Error: \n\t Objects of type Stack must containt data before you attempt to pop");
return null;
}
var data:Object = first.data;
first = first.next;
return data;
}

public function peek () : Object
{
if (isEmpty ())
{
trace ("Error: \n\t Objects of type Stack must containt data before you attempt to peek");
return null;
}
return first.data;
}
}
}

b)
Node Class as the store house object used in Stack Class
package
{

public class Node
{
public function Node()
{
}

public var next : Node;
public var data : Object;
}
}

Cheers

Varun Rathore

No comments:

About Me