The Stack Abstract Data Type
Explore the stack abstract data type by learning its core operations such as Push, Pop, Top, isEmpty, and Size. Understand how to implement stacks in Go using arrays with fixed capacity or linked lists without limits. This lesson helps you build fundamental skills in managing stacks following the LIFO principle within Go applications.
We'll cover the following...
Stack ADT
The stack ADT is defined as a class that follows the LIFO principle for its elements. The stack should support the following operations:
Push(): Adds a single element at the top of the stackPop(): Removes a single element from the top of the stack.Top(): Reads the value of the top element of the stack (does not remove it).isEmpty(): Returns1if the stack is empty.Size(): Returns the number of elements in the stack.
Let’s see how the Push()and Pop() operations work.
Stack implementation
The stack can be implemented using an array or a linked list.When a stack is implemented using arrays, its capacity is fixed. In the case of a linked list, there is no such limit on the number of elements it can contain.
Top of the array
When a stack is implemented using an array, the top of the stack is managed using a variable called top as shown in the illustration above.
Head of the linked list
When a stack is implemented using a linked list, Push() and Pop() operations are implemented using insert at the head of the linked list and remove from the head of the linked list.