codeintuition-logo

Determining the size of the stack


The size operation tells the caller about the current size of the stack. Unlike in the array implementation, where we have the topIndex which we use to get the size of the stack, the linked list implementation does not have a topIndex. The only way to calculate the size of a linked list is to traverse it, which is very expensive. For this reason, we store a currentSize variable in the stack class, which keeps track of the current size of the stack.

Loading Image

Size of the stack is stored in a member variable of the stack class

Algorithm

Liking the course? Check our discounted plans to continue learning.