How to implement stack in Perl?

Questions by ms_2007

Showing Answers 1 - 6 of 6 Answers

Sangeeta Arora

  • Nov 20th, 2007
 

Stack is LIFO (Last in First out), In perl that could be inplemented using the push() and shift() functions. push() adds the element at the last of array and shift() removes from the beginning of an array.

  Was this answer useful?  Yes

maruthr

  • Apr 16th, 2008
 

Stacks and queus in Perl

IN
unshift =>     -------------------------------------------     <= push 
                     |         |          |          |          |        |
shift    <=      --------------------------------------------    =>pop
OUT
                     Figure 1.1: Demonstrate stacks and queues in Perl

Stack: Stack is a important data structure In Perl programming, and can be implemented using LIFO technique. i.e., the Last thing In is the First thing Out.
So from the above demonstration(refer Figure 1.1), it's learnt that Stacks can be implemented using PUSH and POP commands.

Queue: The queue is another data structure. A physical analogy for a queue is a line at a bank. When you go to the bank, customers go to the rear (end) of the line and customers come off of the line (i.e., are serviced) from the front of the line.
So from the above demonstation(refer Figure 1.1), we can say that Queues can be implemented using PUSH and SHIFT.

  Was this answer useful?  Yes

wow_suraj

  • May 2nd, 2008
 

In perl stacks are easy to be implemented. Stacks which follows LIFO (Last In First Out) data structure can be handled in perl with 'POP' and 'PUSH' keywords. 

POP Example:
my $strTest = POP(@arrTest);
print "$strTest";

In this example, the Last element present in @arrTest will be poped and will be assigned to variable $strTest (LIFO). Suppose the array contains value "STAR" at it's last position thus the next line of the code will populate STAR.

PUSH Example:

PUSH(@arrTest, $strTest) if ($strTest);

In this example, we are pushing $strTest variable to the array @arrTest. After the PUSH statement the array size will be increased by one and the last element of the array will be the value contained in variable $strTest. 
 

  Was this answer useful?  Yes

#! /usr/bin/perl -w

$str1="Pushpendra Singh";
@str=split("",
$str1);

@stack_array=();
foreach $i (@str){
print "nt
Item being pushed is : $i";
push(@stack_array, $i);

sleep 1;
}

print "nt Popping out the stack";

$len=@stack_array;
for($i=$len; $i>=0; $i--,$len--){
if($len==0){

exit 0;
}
print "nt Popped item is : ".pop(@stack_array);

sleep 1;
}
print "nt";

  Was this answer useful?  Yes

shah

  • May 16th, 2018
 

Its wrong sangeeta. To implement a stack in perl, we have to use push and pop function, push will add an element at the end of array and pop will remove the element from the end of array

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions