function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gunish Rai ChawlaGunish Rai Chawla 

Native Stack Data Structure

Hi All,

 

We are trying to build a tree trversal method in apex, in salesforce and for DFS we need to access a native data structure of a Stack? 

 

Is this structure available directly or has anyone tried to custom implement a stack ?

 

Thanks in advance,

Gunish

Roger WickiRoger Wicki
There is no such data structure as a stack in Salesforce. However, it is pretty simple to set up an own model of a stack.
Roger WickiRoger Wicki
public class Stack
{
    private list<Object> l;
    
    // Constructor
    public Stack()
    {
        l = new list<Object>();
    }

    // Add the Object on top of the stack (at the end of the list)
    public void push(Object o)
    {
        l.add(o);
    }

    // Remove & Return the top element if the stack (the last element in the list)
    public Object pop()
    {
        return l.remove(l.size()-1);
    }

    // Checks if the stack is empty
    public Boolean isEmpty()
    {
    	return l.isEmpty();
    }

    // Returns the top element in the stack (the last element in the list) without removing it
    public Object peek()
    {
    	return l.get(l.size()-1);
    }

    /**
        Returns the 1-based distance from the top element. Finding the top element returns 1.
        Not found elements return -1
    */
    public Integer search(Object o)
    {
    	for ( Integer i = l.size(); i > 0; i-- )
    	{
    		if ( o == l.get(i-1) )
    		{
    			return Math.abs(l.size() - i + 1);
    		}
    	}
     	return -1;
   }
}
Here you go. This Stack offers all functions a native java Stack offers and works with both, Objects and sObjects. Of course you need a test class to get the Code Coverage for it. Here are two methods, one for an Object (String in this case) and one for an sObejct(Account in this case):
static testMethod void stackTest_sObject()
{
	ArcUtil.Stack s = new ArcUtil.Stack();
	System.assert(s.isEmpty());
	
	Account a = new Account(Name = 'A');
	Account b = new Account(Name = 'B');
	Account c = new Account(Name = 'C');
	
	s.push(a);
	s.push(b);
	s.push(c);
	
	System.assert(!s.isEmpty());
	System.assert(s.peek() == c);
	System.assert(s.pop() == c);
	System.assert(s.peek() == b);
	
	s.push(b);
	s.push(c);
	
	System.assert(s.search(b) == 2);
	System.assert(s.search(c) == 1);
	System.assert(s.search(a) == 4);
	System.assert(s.search(s.pop()) == -1);
}

static testMethod void stackTest_Object()
{
	ArcUtil.Stack s = new ArcUtil.Stack();
	System.assert(s.isEmpty());

	String a = 'a';
	String b = 'b';
	String c = 'c';

	s.push(a);
	s.push(b);
	s.push(c);
	
	System.assert(!s.isEmpty());
	System.assert(s.peek() == c);
	System.assert(s.pop() == c);
	System.assert(s.peek() == b);
	
	s.push(b);
	s.push(c);
	
	System.assert(s.search(b) == 2);
	System.assert(s.search(c) == 1);
	System.assert(s.search(a) == 4);
	System.assert(s.search(s.pop()) == -1);
}
Note: I defined my Stack class as an inline class of "ArcUtil" where I keep (static) everyday helper methods and classes like this one.