In this chapter, we ll demonstrate how to implement a Stack using Test-Driven Development (TDD). We have found that the best way to understand TDD is to see it practiced and follow along step by step. The following are the steps we used to build a Stack using this method.
The task is to implement an unbounded Stack , which is a data structure in which access is restricted to the most recently inserted item.
An unbounded Stack doesn t have to be presized, and you can insert an unlimited number of elements onto it.
The operations include Push , Pop , Top, and IsEmpty . The Push function inserts an element onto the top of the Stack. The Pop function removes the topmost element and returns it; the Top operation returns the topmost element but does not remove it from the Stack . The IsEmpty function returns true when there are no elements on the Stack . Figure 2-1 shows the Push operation in action, Figure 2-2 shows the Pop operation, and Figure 2-3 shows the Top operation.