珀尔 |实现堆栈
Perl 中的堆栈是遵循LIFO (后进先出)或FILO (先进后出)顺序的线性数据结构。
简单来说,堆栈是一个数组,其中插入和删除仅发生在称为堆栈顶部的一端。
推送是将元素插入堆栈的过程。
弹出是删除堆栈最顶层元素的过程。
在 Perl 中创建堆栈相当简单。我们需要做的就是声明一个数组。
堆栈可能为空,如下所示:
@stack;
或者它可以被初始化:
@stack = (1, 2, 3);
可以使用push()函数或splice()函数来完成推送。
- 使用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.
例子:
#!/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");
输出:Original Stack: 1 2 3 Updated Stack: 1 2 3 scalar a r r a y Geeks 10 for Geeks 20
- 使用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.
例子:
#!/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");
输出:Original Stack: 1 2 3 Updated Stack: 1 2 3 scalar a r r a y Geeks 10 for Geeks 20
可以使用 pop()函数或 splice()函数来完成弹出。
- 使用pop() 弹出:
Syntax: $popped_element = pop(@stack);
Parameters:- pop() function returns the popped element.
- $popped_element contains the element popped from the stack.
例子:
#!/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");
输出:Original Stack: 1 2 3 Popped element: 3 Updated Stack: 1 2
- 如果堆栈为空,则返回undef 。 undef 类似于Java中的 NULL 和Python中的 None 。但是,不会引发错误。
例子:
#!/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";
输出:Popped element:
- 使用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.
例子:
#!/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");
输出:Original Stack: 1 2 3 Popped element: 3 Updated Stack: 1 2
- 如果堆栈为空,则会引发错误。以下代码引发错误:
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.