The following source code implements the Stack Data Structure in Perl.
#!/usr/bin/perl5
my $moreNos=undef;my @stack;my $elem=undef;
do{
print "\n1. Insert Element\n"; print "2. Delete Element\n"; print "3. Display\n"; print "4. Exit\n";
print "Enter choice: ";
chomp ($choice=<stdin>);
if ($choice eq "1")
{
&insertElem();
}
elsif ($choice eq "2")
{
&deleteElem();
}
elsif ($choice eq "3")
{
&display();
}
else
{
exit;
}
}while($choice ne "4");
sub insertElem
{
print "Enter the no: ";
chomp ( $elem =<stdin> );
unshift(@stack,$elem);
chomp @stack;
}
sub deleteElem
{
if($#stack >= 0)
{
print "\nDeleted $stack[0]\n";
shift(@stack);
}
else
{
print "\nStack Empty\n";
}
}
sub display
{
if($#stack >=0)
{
print "\n@stack\n";
}
else
{
print "\nStack Empty\n";
}
}
Paid for writing
In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks
Tuesday, January 20, 2009
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment