Stack

When Java 6 was introduced, it was Sun's recommendation to use the Deque (pronounced "deck") for all Stack and Queue implemenations, which combines both in one collection class containing the familiar Stack and Queue methods, and to deprecate the legacy Stack and Queue classes from Java 5. JavaTools implements the Stack and Queue with the Deque.

In JavaTools a stack can simply be created with JDeque[]. It then has to be filled with elements afterwards.

"stack_1.gif"

"stack_2.gif"

However, it's also possible to create a new stack and supply initial values during creation:

"stack_3.gif"

"stack_4.gif"

JDequeSize[] returns the number of elements in the stack, and JDequeIsEmpty[] shows if the stack is empty:

"stack_5.gif"

"stack_6.gif"

"stack_7.gif"

"stack_8.gif"

A new element is pushed into the stack with JDequePush[]or JDequeAdd[]:

"stack_9.gif"

The whole list of elements in the stack can be shown with JDequeElements[]:

"stack_10.gif"

"stack_11.gif"

The type of elements that can be put in a stack can be anything: Integer, Real, String, or even List:

"stack_12.gif"

"stack_13.gif"

"stack_14.gif"

"stack_15.gif"

"stack_16.gif"

"stack_17.gif"

"stack_18.gif"

"stack_19.gif"

JDequePeek[] retrieves, but does not remove the first element of the stack:

"stack_20.gif"

"stack_21.gif"

JStackPop[] retrieves and removes the first element of the stack:

"stack_22.gif"

"stack_23.gif"

"stack_24.gif"

"stack_25.gif"

JDequeGetFirst[] and JDequeGetLast[] show the first and last elements in the stack:

"stack_26.gif"

"stack_27.gif"

"stack_28.gif"

"stack_29.gif"

With JDequeRemoveFirst[] and JDequeRemoveLast[] we can remove the first and last elements from the stack:

"stack_30.gif"

"stack_31.gif"

"stack_32.gif"

"stack_33.gif"

"stack_34.gif"

"stack_35.gif"

With JDequeRemoveFirstOccurrence[] and JDequeRemoveLastOccurrence[] we can remove the elements of the first and last occurrences from the stack:

"stack_36.gif"

"stack_37.gif"

"stack_38.gif"

"stack_39.gif"

"stack_40.gif"

"stack_41.gif"

With JDequeContains[] we can see if the stack contains a particular element:

"stack_42.gif"

"stack_43.gif"

"stack_44.gif"

"stack_45.gif"

"stack_46.gif"

"stack_47.gif"

"stack_48.gif"

"stack_49.gif"

"stack_50.gif"

"stack_51.gif"

Performance

Note the memory-footprint the JDeque[] has on the Mathematica system: Not a single byte of data is stored in the kernel:

"stack_52.gif"

"stack_53.gif"

"stack_54.gif"

"stack_55.gif"

"stack_56.gif"

"stack_57.gif"

"stack_58.gif"

Compare against storing eight million integers in the Mathematica kernel:

"stack_59.gif"

"stack_60.gif"

"stack_61.gif"

Spikey Created with Wolfram Mathematica 7.0