Open In App

Perl | Implementing a Stack

Stack in Perl is a linear data structure that follows the LIFO (Last In First Out) or FILO (First In Last Out) order.
In simpler terms, a stack is an array in which insertion and deletion takes place at only one end called the top of the stack.
Pushing is the process of insertion of elements into a stack.
Popping is the process of removal of topmost element of a stack.

Making a stack

 
Creating a stack in Perl is rather simple. All we need to do is declare an array.
The stack could be empty, as follows:



@stack;

Or it could be initialized:

@stack = (1, 2, 3);
Pushing items to a stack

 
Pushing can be done using either the push() function or the splice() function.



Popping elements from a Stack

 
Popping can be done using either the pop() function or the splice() function.

Article Tags :