Perl | Implementing a Stack
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.
The stack could be empty, as follows:
@stack;Or it could be initialized:
@stack = (1, 2, 3);
-
Pushing using push():
Syntax: push(@stack, list); Parameters:
Example:- @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.
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:
Example:- splice() function appends the 'list' at the end of @stack.
- THe 'list' could be a scalar, an array or a hash.
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 using pop():
Syntax: $popped_element = pop(@stack); Parameters:
Example:- pop() function returns the popped element.
- $popped_element contains the element popped from the stack.
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";
Output:Popped element:
-
Popping using splice()::
Syntax: $popped_element=splice(@stack, -1); Parameters:
Example:- splice() function removes the last element of the stack and returns it.
- $popped_element stores the returned value.
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");
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.