Monday, October 31, 2011

Stack Code

The main() method in the StackApp class creates a stack that can hold 10 items, pushes 4 items onto the stack, and then displays all the items by popping them off the stack, and then displays all the items by popping them off the stack until it's empty. Here's the output.,

80 60 40 20

Notice how the order of the data is reversed. Because the last item pushed is the first one popped, the 80 appears first in the output. the the 80 appears first in the output.

Efficiency of Stacks
Items can be both pushed and popped from the stack implemented in the StackX class in constant O(1) time. That is, the time is not dependent on how many items are in the stack and is therefore very quick. No comparisons or moves are necessary. .

No comments:

Post a Comment