4.4. The Stack Abstract Data Type

The stack abstract data type is defined by the following structure and operations. A stack is structured, as described above, as an ordered collection of items where items are added to and removed from the end called the “top.” Stacks are ordered LIFO. The stack operations are given below.

For example, if s is a stack that has been created and starts out empty, then Table 1 shows the results of a sequence of stack operations. Under stack contents, the top item is listed at the far right.

Table 1: Sample Stack Operations

Stack Operation

Stack Contents

Return Value

s.isEmpty()

[]

True

s.push(4)

[4]

s.push('dog')

[4,'dog']

s.peek()

[4,'dog']

'dog'

s.push(True)

[4,'dog',True]

s.size()

[4,'dog',True]

3

s.isEmpty()

[4,'dog',True]

False

s.push(8.4)

[4,'dog',True,8.4]

s.pop()

[4,'dog',True]

8.4

s.pop()

[4,'dog']

True

s.size()

[4,'dog']

2

You have attempted of activities on this page