Perl | Implementing a Stack
Last Updated :
21 Feb, 2019
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.
-
Pushing using push():
Syntax: push(@stack, list);
Parameters:
- @stack - The stack on which push is to be performed.
- list - The elements to be pushed into a stack. These elements might be scalar, array, hash or any combination of these.
Example:
Perl
#!/usr/bin/perl
# Intitialising the Stack
@stack = (1..3);
# Original stack
print "Original Stack: @stack";
# Scalar to be pushed
$scalar = "scalar";
# Array to be pushed
@array = ("a", "r", "r", "a", "y");
# Hash to be pushed
%hash = ("Geeks" => 10,
"for Geeks" => 20);
# scalars, arrays and hashes can be
# inserted at the same time
push(@stack, ($scalar, @array, %hash));
# Updated Stack after
# Push operations
print("\nUpdated Stack: @stack");
Output:
Original Stack: 1 2 3
Updated Stack: 1 2 3 scalar a r r a y Geeks 10 for Geeks 20
-
Pushing using splice():
Syntax: splice(@stack, scalar(@stack), 0, list);
Parameters:
- splice() function appends the 'list' at the end of @stack.
- THe 'list' could be a scalar, an array or a hash.
Example:
perl
#!/usr/bin/perl
# Intitialising the Stack
@stack = (1..3);
# Original stack
print "Original Stack: @stack";
# Scalar to be pushed
$scalar = "scalar";
# Array to be pushed
@array = ("a", "r", "r", "a", "y");
# Hash to be pushed
%hash = ("Geeks" => 10,
"for Geeks" => 20);
# scalars, arrays and hashes can be
# inserted at the same time
splice(@stack, scalar(@stack), 0,
($scalar, @array, %hash));
# Updated Stack after
# Push operations
print("\nUpdated Stack: @stack");
Output:
Original Stack: 1 2 3
Updated Stack: 1 2 3 scalar a r r a y Geeks 10 for Geeks 20
Popping elements from a Stack
Popping can be done using either the pop() function or the splice() function.
-
Popping using pop():
Syntax: $popped_element = pop(@stack);
Parameters:
- pop() function returns the popped element.
- $popped_element contains the element popped from the stack.
Example:
perl
#!/usr/bin/perl
# Intitialising the Stack
@stack = (1..3);
# Original stack
print "Original Stack: @stack";
# Topmost element i.e. 3 is
# removed and returned
$popped_element = pop(@stack);
# Printing popped element
print "\nPopped element: $popped_element";
# Updated Stack after
# Pop operation
print("\nUpdated Stack: @stack");
Output:
Original Stack: 1 2 3
Popped element: 3
Updated Stack: 1 2
- If the stack is empty, undef is returned. undef is analogous to NULL in Java and None in Python. However, no error is raised.
Example:
perl
#!/usr/bin/perl
# Creating a Stack
@stack;
# undef is returned since the
# stack is empty.
# No error is raised.
$popped_element = pop(@stack);
# Printing popped element
# Since it contains no value,
# hence a blank space is returned
print "Popped element: $popped_element";
-
Popping using splice()::
Syntax: $popped_element=splice(@stack, -1);
Parameters:
- splice() function removes the last element of the stack and returns it.
- $popped_element stores the returned value.
Example:
perl
#!/usr/bin/perl
# Intitialising the Stack
@stack = (1..3);
# Original stack
print "Original Stack: @stack";
# popping using splice()
$popped_element = splice(@stack, -1);
# Printing popped element
print "\nPopped element: $popped_element";
# Updated Stack after
# Pop operation
print("\nUpdated Stack: @stack");
Output:
Original Stack: 1 2 3
Popped element: 3
Updated Stack: 1 2
- An error is raised, if the stack is empty. The following code raises an error:
perl
use warnings;
#!/usr/bin/perl
use warnings;
# Creating a Stack
@stack;
# popping using splice()
# An error is raised here
$popped_element = splice(@stack, -1);
# Printing popped element
print "\nPopped element: $popped_element";
# Updated Stack after
# Pop operation
print("\nStack: @stack");
Runtime Error:
Useless use of a variable in void context at /home/59c7c19979aa9e46564cd145d5fe5601.pl line 6.
Modification of non-creatable array value attempted, subscript -1 at /home/59c7c19979aa9e46564cd145d5fe5601.pl line 10.
Similar Reads
Perl | Implementing a Queue Prerequisite: StackQueue in Perl is a linear abstract data structure which follows the FIFO (First In First Out) order. It resembles the properties of the queue which we encounter in our daily life where the person who comes first will be served first. It is open at both ends. Unlike stack where the
10 min read
Stack implementation in C++ Stack is the fundamental data structures used in the computer science to the store collections of the objects. It can operates on the Last In, First Out (LIFO) principle where the most recently added the object is the first one to be removed. It can makes the stacks highly useful in the situations w
4 min read
Implement a Stack in C Programming Stack is the linear data structure that follows the Last in, First Out(LIFO) principle of data insertion and deletion. It means that the element that is inserted last will be the first one to be removed and the element that is inserted first will be removed at last. Think of it as the stack of plate
7 min read
Implement Stack using Array Stack is a linear data structure which follows LIFO principle. To implement a stack using an array, initialize an array and treat its end as the stackâs top. Implement push (add to end), pop (remove from end), and peek (check end) operations, handling cases for an empty or full stack.Step-by-step ap
10 min read
C++ Program to Implement Stack using array Stack is the fundamental data structure that can operates the under the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Implementing the stack using the array is one of the most straightforward methods in the terms of the both
4 min read
Implementation of Stack Using Array in C A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means that the most recently added element is the first one to be removed. In this article, we will learn how to implement a stack using an array in C. Implementation of Stack Using Arrays in CIn the array-b
5 min read
Implementation of stack using Doubly Linked List Stack and doubly linked lists are two important data structures with their own benefits. Stack is a data structure that follows the LIFO (Last In First Out) order and can be implemented using arrays or linked list data structures. Doubly linked list has the advantage that it can also traverse the pr
14 min read
Java Program to Implement Stack Data Structure Stack is the fundamental Data Structure that can follow the Last In, First Out(LIFO) principle. It can work that the last element added to the stack will be the first one to be removed. It can operate like a stack of plates: We can only add or remove the topmost plate at any given time. The simplici
5 min read
Stack Definition & Meaning in DSA A stack is defined as a linear data structure that is open at one end and the operations follow the Last-In-First-Out (LIFO) order. Example of StackCharacteristics of Stack:The stack follows the LIFO order, which means that the last element added to the stack will be the first element to be removed.
2 min read
Overload == operator for Stack Stack is a linear data structure that works in a model of LIFO ( last in first out ). In which, the element pushed at last will be deleted at first. Insertion and deletion will happen on the same end. A real-life example of a stack would be a stack of books, a stack of plates, etc. The Insertion del
14 min read