📜  珀尔 |实现堆栈

📅  最后修改于: 2022-05-13 01:55:38.112000             🧑  作者: Mango

珀尔 |实现堆栈

Perl 中的堆栈是遵循LIFO (后进先出)或FILO (先进后出)顺序的线性数据结构。
简单来说,堆栈是一个数组,其中插入和删除仅发生在称为堆栈顶部的一端。
推送是将元素插入堆栈的过程。
弹出是删除堆栈最顶层元素的过程。

制作堆栈


在 Perl 中创建堆栈相当简单。我们需要做的就是声明一个数组。
堆栈可能为空,如下所示:

@stack;

或者它可以被初始化:

@stack = (1, 2, 3);
将项目推入堆栈


可以使用push()函数或splice()函数来完成推送。

  • 使用push() 推送

    例子:

    #!/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()推送:

    例子:

    #!/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() 弹出

    例子:

    #!/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():弹出

    例子:

    #!/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");
      

      运行时错误: