Stacks
Stacks are a special case of linked list structures where data can be added and removed from one end only, that is, the head, also known as the top. A stack is based on the Last In First Out (LIFO) principle, as the last element inserted is the first to be removed. The first element in the stack is called the top, and all operations are accessed through the top. The addition and removal of an element from the top of a stack is referred to as Push and Pop respectively, as shown in Figure 4.1:
Figure 4.1: Example of Push and Pop operation in stacks
A stack is a recursive data structure, as it consists of a top element, and the rest is either empty or another stack. The main ADT required to build a stack is shown in Table 4.1. This book will cover two approaches-array-based stack and linked stack-to implement the ADT mentioned in Table 4.1. The implementation is covered using reference classes in R:
Table 4.1 Abstract data type for stack
Array-based stacks
Array-based stack implementation...