DataType:stack

From the wonderful RedGuides Wiki

A stack is a last-in, first-out data structure. Items inserted (Pushed) are placed on the 'top' of the queue. Items removed (Popped) are also removed from the 'top'.


This Data Type is referenced in MQ2Collections, and accessed by Top-Level Object(s): stack

Members

Type Member Description
bool IsEmpty True if Count = 0, False otherwise.
Push[string] True if the item was pushed successfully.
int Count Number of items inserted onto the stack.
string Peek False is returned if IsEmpty is true.
Pop

Examples

Sub StackTest
    /declare s stack

    /echo 'Starting Stack Test'
    /echo 'Stack is Empty: ${s.IsEmpty}'
    
    | Push entries on the stack and pop them off.
    | Note: entries are inserted (Pushed) in and removed
    | (Popped) in reverse order. That is, Pushing A, B, C, D, E
    | and then Popping them will return E, D, C, D, A.

    /echo 'Pushing items onto the Stack.'
    /if (!${s.Push[A]}) {
    	/echo 'Stack Push of A failed.'
	/endmacro
    }
    /if (!${s.Push[B]}) {
    	/echo 'Stack Push of B failed.'
	/endmacro
    }
    /if (!${s.Push[C]}) {
    	/echo 'Stack Push of C failed.'
	/endmacro
    }
    /if (!${s.Push[D]}) {
    	/echo 'Stack Push of D failed.'
	/endmacro
    }
    /if (!${s.Push[E]}) {
    	/echo 'Stack Push of E failed.'
	/endmacro
    }

    /declare count int
    /varset count ${s.Count}
    /if (${count} != 5) {
    	/echo 'Stack count is: ${count} and should be 5.'
	/endmacro
    }

    /echo 'Stack is Empty: ${s.IsEmpty}'
    
    | Peek at the top item:
    /echo 'Top item is: ${s.Peek}'
    
    | Pop items off the stack.

    /echo 'Popping item off stack: ${s.Pop}'
    /echo 'Popping item off stack: ${s.Pop}'
    /echo 'Popping item off stack: ${s.Pop}'
    /echo 'Popping item off stack: ${s.Pop}'
    /echo 'Popping item off stack: ${s.Pop}'

    /echo 'Stack is Empty: ${s.IsEmpty}'
    /echo 'Ending Stack Test'
    
    /return

Notes

  • Pop decreases Count by 1 unless IsEmpty is true.
  • Push increases Count by 1. IsEmpty will never be true after calling Push.
  • Peek retrieves the last element Pushed onto the stack. Count is not modified by calling Peek.


See also