📜  在一个数组中实现两个栈

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

在一个数组中实现两个栈

创建一个表示两个堆栈的数据结构twoStackstwoStacks的实现应该只使用一个数组,即两个堆栈应该使用同一个数组来存储元素。 twoStacks必须支持以下功能。
push1(int x) –> 将 x 推入第一个堆栈
push2(int x) -> 将 x 推入第二个堆栈
pop1() –> 从第一个堆栈中弹出一个元素并返回弹出的元素
pop2() –> 从第二个堆栈中弹出一个元素并返回弹出的元素
twoStack的实现应该是节省空间的。

方法1(将空间分成两半)
实现两个堆栈的一个简单方法是将数组分成两半,并将一半空间分配给两个堆栈,即stack1使用arr[0]到arr[n/2],arr[(n/2)+ 1] 到堆栈 2 的 arr[n-1] 其中 arr[] 是用于实现两个堆栈的数组,数组的大小为 n。
这种方法的问题是对数组空间的使用效率低下。即使 arr[] 中有可用空间,堆栈推送操作也可能导致堆栈溢出。例如,假设数组大小为 6,我们将 3 个元素推送到 stack1,并且不向第二个 stack2 推送任何内容。当我们将第 4 个元素压入 stack1 时,即使数组中还有 3 个元素的空间,也会发生溢出。

C++
#include 
#include 
 
using namespace std;
 
class twoStacks {
    int* arr;
    int size;
    int top1, top2;
 
public:
    // Constructor
    twoStacks(int n)
    {
        size = n;
        arr = new int[n];
        top1 = n / 2 + 1;
        top2 = n / 2;
    }
 
    // Method to push an element x to stack1
    void push1(int x)
    {
        // There is at least one empty
        // space for new element
        if (top1 > 0) {
            top1--;
            arr[top1] = x;
        }
        else {
            cout << "Stack Overflow"
                 << " By element :" << x << endl;
            return;
        }
    }
 
    // Method to push an element
    // x to stack2
    void push2(int x)
    {
 
        // There is at least one empty
        // space for new element
        if (top2 < size - 1) {
            top2++;
            arr[top2] = x;
        }
        else {
            cout << "Stack Overflow"
                 << " By element :" << x << endl;
            return;
        }
    }
 
    // Method to pop an element from first stack
    int pop1()
    {
        if (top1 <= size / 2) {
            int x = arr[top1];
            top1++;
            return x;
        }
        else {
            cout << "Stack UnderFlow";
            exit(1);
        }
    }
 
    // Method to pop an element
    // from second stack
    int pop2()
    {
        if (top2 >= size / 2 + 1) {
            int x = arr[top2];
            top2--;
            return x;
        }
        else {
            cout << "Stack UnderFlow";
            exit(1);
        }
    }
};
 
/* Driver program to test twoStacks class */
int main()
{
    twoStacks ts(5);
    ts.push1(5);
    ts.push2(10);
    ts.push2(15);
    ts.push1(11);
    ts.push2(7);
    cout << "Popped element from stack1 is "
         << " : " << ts.pop1()
         << endl;
    ts.push2(40);
    cout << "\nPopped element from stack2 is "
         << ": " << ts.pop2()
         << endl;
    return 0;
}


Java
import java.util.*;
class twoStacks
{
  int[] arr;
  int size;
  int top1, top2;
 
  // Constructor
  twoStacks(int n)
  {
    size = n;
    arr = new int[n];
    top1 = n / 2 + 1;
    top2 = n / 2;
  }
 
  // Method to push an element x to stack1
  void push1(int x)
  {
 
    // There is at least one empty
    // space for new element
    if (top1 > 0)
    {
      top1--;
      arr[top1] = x;
    }
    else
    {
      System.out.print("Stack Overflow"
                       + " By element :" +  x +"\n");
      return;
    }
  }
 
  // Method to push an element
  // x to stack2
  void push2(int x)
  {
 
    // There is at least one empty
    // space for new element
    if (top2 < size - 1)
    {
      top2++;
      arr[top2] = x;
    }
    else
    {
      System.out.print("Stack Overflow"
                       + " By element :" +  x +"\n");
      return;
    }
  }
 
  // Method to pop an element from first stack
  int pop1()
  {
    if (top1 <= size / 2)
    {
      int x = arr[top1];
      top1++;
      return x;
    }
    else
    {
      System.out.print("Stack UnderFlow");
      System.exit(1);
    }
    return 0;
  }
 
  // Method to pop an element
  // from second stack
  int pop2()
  {
    if (top2 >= size / 2 + 1)
    {
      int x = arr[top2];
      top2--;
      return x;
    }
    else
    {
      System.out.print("Stack UnderFlow");
      System.exit(1);
    }
    return 1;
  }
};
class GFG
{
 
  /* Driver program to test twStacks class */
  public static void main(String[] args)
  {
    twoStacks ts = new twoStacks(5);
    ts.push1(5);
    ts.push2(10);
    ts.push2(15);
    ts.push1(11);
    ts.push2(7);
    System.out.print("Popped element from stack1 is "
                     + " : " +  ts.pop1() +"\n");
    ts.push2(40);
    System.out.print("Popped element from stack2 is "
                     + ": " +  ts.pop2()
                     +"\n");
  }
}
 
// This code is contributed by aashish1995


C#
using System;
using System.Collections.Generic;
 
public  class twoStacks {
    public int[] arr;
    public int size;
    public int top1, top2;
 
    // Constructor
    public twoStacks(int n)
    {
        size = n;
        arr = new int[n];
        top1 = n / 2 + 1;
        top2 = n / 2;
    }
 
    // Method to push an element x to stack1
    public void push1(int x)
    {
 
        // There is at least one empty
        // space for new element
        if (top1 > 0) {
            top1--;
            arr[top1] = x;
        }
        else {
            Console.Write("Stack Overflow"
                          + " By element :" + x + "\n");
            return;
        }
    }
 
    // Method to push an element
    // x to stack2
    public void push2(int x)
    {
 
        // There is at least one empty
        // space for new element
        if (top2 < size - 1) {
            top2++;
            arr[top2] = x;
        }
        else {
            Console.Write("Stack Overflow"
                          + " By element :" + x + "\n");
            return;
        }
    }
 
    // Method to pop an element from first stack
    public int pop1()
    {
        if (top1 <= size / 2) {
            int x = arr[top1];
            top1++;
            return x;
        }
        else {
            Console.Write("Stack UnderFlow");
        }
        return 0;
    }
 
    // Method to pop an element
    // from second stack
    public int pop2()
    {
        if (top2 >= size / 2 + 1) {
            int x = arr[top2];
            top2--;
            return x;
        }
        else {
            Console.Write("Stack UnderFlow");
        }
        return 1;
    }
};
 
public class GFG {
 
    /* Driver program to test twStacks class */
    public static void Main(String[] args)
    {
        twoStacks ts = new twoStacks(5);
        ts.push1(5);
        ts.push2(10);
        ts.push2(15);
        ts.push1(11);
        ts.push2(7);
        Console.Write("Popped element from stack1 is "
                      + " : " + ts.pop1() + "\n");
        ts.push2(40);
        Console.Write("Popped element from stack2 is "
                      + ": " + ts.pop2() + "\n");
    }
}
 
// This code is contributed by umadevi9616


Javascript


C++
#include 
#include 
 
using namespace std;
 
class twoStacks {
    int* arr;
    int size;
    int top1, top2;
 
public:
    twoStacks(int n) // constructor
    {
        size = n;
        arr = new int[n];
        top1 = -1;
        top2 = size;
    }
 
    // Method to push an element x to stack1
    void push1(int x)
    {
        // There is at least one empty space for new element
        if (top1 < top2 - 1) {
            top1++;
            arr[top1] = x;
        }
        else {
            cout << "Stack Overflow";
            exit(1);
        }
    }
 
    // Method to push an element x to stack2
    void push2(int x)
    {
        // There is at least one empty
        // space for new element
        if (top1 < top2 - 1) {
            top2--;
            arr[top2] = x;
        }
        else {
            cout << "Stack Overflow";
            exit(1);
        }
    }
 
    // Method to pop an element from first stack
    int pop1()
    {
        if (top1 >= 0) {
            int x = arr[top1];
            top1--;
            return x;
        }
        else {
            cout << "Stack UnderFlow";
            exit(1);
        }
    }
 
    // Method to pop an element from second stack
    int pop2()
    {
        if (top2 < size) {
            int x = arr[top2];
            top2++;
            return x;
        }
        else {
            cout << "Stack UnderFlow";
            exit(1);
        }
    }
};
 
/* Driver program to test twoStacks class */
int main()
{
    twoStacks ts(5);
    ts.push1(5);
    ts.push2(10);
    ts.push2(15);
    ts.push1(11);
    ts.push2(7);
    cout << "Popped element from stack1 is "
         << ts.pop1();
    ts.push2(40);
    cout << "\nPopped element from stack2 is "
         << ts.pop2();
    return 0;
}


Java
// Java program to implement two stacks in a
// single array
class TwoStacks {
    int size;
    int top1, top2;
    int arr[];
 
    // Constructor
    TwoStacks(int n)
    {
        arr = new int[n];
        size = n;
        top1 = -1;
        top2 = size;
    }
 
    // Method to push an element x to stack1
    void push1(int x)
    {
        // There is at least one empty space for
        // new element
        if (top1 < top2 - 1) {
            top1++;
            arr[top1] = x;
        }
        else {
            System.out.println("Stack Overflow");
            System.exit(1);
        }
    }
 
    // Method to push an element x to stack2
    void push2(int x)
    {
        // There is at least one empty space for
        // new element
        if (top1 < top2 - 1) {
            top2--;
            arr[top2] = x;
        }
        else {
            System.out.println("Stack Overflow");
            System.exit(1);
        }
    }
 
    // Method to pop an element from first stack
    int pop1()
    {
        if (top1 >= 0) {
            int x = arr[top1];
            top1--;
            return x;
        }
        else {
            System.out.println("Stack Underflow");
            System.exit(1);
        }
        return 0;
    }
 
    // Method to pop an element from second stack
    int pop2()
    {
        if (top2 < size) {
            int x = arr[top2];
            top2++;
            return x;
        }
        else {
            System.out.println("Stack Underflow");
            System.exit(1);
        }
        return 0;
    }
 
    // Driver program to test twoStack class
    public static void main(String args[])
    {
        TwoStacks ts = new TwoStacks(5);
        ts.push1(5);
        ts.push2(10);
        ts.push2(15);
        ts.push1(11);
        ts.push2(7);
        System.out.println("Popped element from"
                           + " stack1 is " + ts.pop1());
        ts.push2(40);
        System.out.println("Popped element from"
                           + " stack2 is " + ts.pop2());
    }
}
// This code has been contributed by
// Amit Khandelwal(Amit Khandelwal 1).


Python
# Python Script to Implement two stacks in a list
class twoStacks:
     
    def __init__(self, n):     # constructor
        self.size = n
        self.arr = [None] * n
        self.top1 = -1
        self.top2 = self.size
         
    # Method to push an element x to stack1
    def push1(self, x):
         
        # There is at least one empty space for new element
        if self.top1 < self.top2 - 1 :
            self.top1 = self.top1 + 1
            self.arr[self.top1] = x
 
        else:
            print("Stack Overflow ")
            exit(1)
 
    # Method to push an element x to stack2
    def push2(self, x):
 
        # There is at least one empty space for new element
        if self.top1 < self.top2 - 1:
            self.top2 = self.top2 - 1
            self.arr[self.top2] = x
 
        else :
           print("Stack Overflow ")
           exit(1)
 
    # Method to pop an element from first stack
    def pop1(self):
        if self.top1 >= 0:
            x = self.arr[self.top1]
            self.top1 = self.top1 -1
            return x
        else:
            print("Stack Underflow ")
            exit(1)
 
    # Method to pop an element from second stack
    def pop2(self):
        if self.top2 < self.size:
            x = self.arr[self.top2]
            self.top2 = self.top2 + 1
            return x
        else:
            print("Stack Underflow ")
            exit()
 
# Driver program to test twoStacks class
ts = twoStacks(5)
ts.push1(5)
ts.push2(10)
ts.push2(15)
ts.push1(11)
ts.push2(7)
 
print("Popped element from stack1 is " + str(ts.pop1()))
ts.push2(40)
print("Popped element from stack2 is " + str(ts.pop2()))
 
# This code is contributed by Sunny Karira


C#
// C# program to implement two
// stacks in a single array
using System;
 
public class TwoStacks {
    public int size;
    public int top1, top2;
    public int[] arr;
 
    // Constructor
    public TwoStacks(int n)
    {
        arr = new int[n];
        size = n;
        top1 = -1;
        top2 = size;
    }
 
    // Method to push an element x to stack1
    public virtual void push1(int x)
    {
        // There is at least one empty
        // space for new element
        if (top1 < top2 - 1) {
            top1++;
            arr[top1] = x;
        }
        else {
            Console.WriteLine("Stack Overflow");
            Environment.Exit(1);
        }
    }
 
    // Method to push an element x to stack2
    public virtual void push2(int x)
    {
        // There is at least one empty
        // space for new element
        if (top1 < top2 - 1) {
            top2--;
            arr[top2] = x;
        }
        else {
            Console.WriteLine("Stack Overflow");
            Environment.Exit(1);
        }
    }
 
    // Method to pop an element
    // from first stack
    public virtual int pop1()
    {
        if (top1 >= 0) {
            int x = arr[top1];
            top1--;
            return x;
        }
        else {
            Console.WriteLine("Stack Underflow");
            Environment.Exit(1);
        }
        return 0;
    }
 
    // Method to pop an element
    // from second stack
    public virtual int pop2()
    {
        if (top2 < size) {
            int x = arr[top2];
            top2++;
            return x;
        }
        else {
            Console.WriteLine("Stack Underflow");
            Environment.Exit(1);
        }
        return 0;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        TwoStacks ts = new TwoStacks(5);
        ts.push1(5);
        ts.push2(10);
        ts.push2(15);
        ts.push1(11);
        ts.push2(7);
        Console.WriteLine("Popped element from"
                          + " stack1 is " + ts.pop1());
        ts.push2(40);
        Console.WriteLine("Popped element from"
                          + " stack2 is " + ts.pop2());
    }
}
 
// This code is contributed by Shrikant13


PHP
size = $n;
        $this->arr = array();
        $this->top1 = -1;
        $this->top2 = $this->size;
    }
 
// Method to push an element x to stack1
function push1($x)
{
    // There is at least one empty
    // space for new element
    if ($this->top1 < $this->top2 - 1)
    {
        $this->top1++;
        $this->arr[$this->top1] = $x;
    }
    else
    {
        echo "Stack Overflow";
        exit();
    }
}
 
// Method to push an element x to stack2
function push2($x)
{
    // There is at least one empty space
    // for new element
    if ($this->top1 < $this->top2 - 1)
    {
        $this->top2--;
        $this->arr[$this->top2] = $x;
    }
    else
    {
        echo "Stack Overflow";
        exit();
    }
}
 
// Method to pop an element
// from first stack
function pop1()
{
    if ($this->top1 >= 0 )
    {
        $x = $this->arr[$this->top1];
        $this->top1--;
        return $x;
    }
    else
    {
        echo "Stack UnderFlow";
        exit();
    }
}
 
// Method to pop an element from
// second stack
function pop2()
{
    if ($this->top2 < $this->size)
    {
        $x = $this->arr[$this->top2];
        $this->top2++;
        return $x;
    }
    else
    {
        echo "Stack UnderFlow";
        exit();
    }
}
};
 
 
// Driver Code
$ts = new twoStacks(5);
$ts->push1(5);
$ts->push2(10);
$ts->push2(15);
$ts->push1(11);
$ts->push2(7);
echo "Popped element from stack1 is " .
                           $ts->pop1();
$ts->push2(40);
echo "\nPopped element from stack2 is " .
                             $ts->pop2();
 
// This code is contributed by
// rathbhupendra
?>


Javascript


输出:

Stack Overflow By element :7
Popped element from stack1 is  : 11
Stack Overflow By element :40
Popped element from stack2 is : 15

复杂性分析:

  • 时间复杂度:
    • 推送操作: O(1)
    • 弹出操作: O(1)
  • 辅助空间: O(N)。
    使用数组来实现堆栈。它不是上面解释的空间优化方法。

方法 2(节省空间的实现)
这种方法有效地利用了可用空间。如果 arr[] 中有可用空间,则不会导致溢出。这个想法是从 arr[] 的两个极端角开始两个堆栈。 stack1 从最左边的元素开始,stack1 中的第一个元素被压入索引 0。stack2 从最右边的角开始,stack2 中的第一个元素被压入索引 (n-1)。两个堆栈都以相反的方向增长(或缩小)。要检查溢出,我们只需要检查两个堆栈顶部元素之间的空间。此检查在以下代码中突出显示。

C++

#include 
#include 
 
using namespace std;
 
class twoStacks {
    int* arr;
    int size;
    int top1, top2;
 
public:
    twoStacks(int n) // constructor
    {
        size = n;
        arr = new int[n];
        top1 = -1;
        top2 = size;
    }
 
    // Method to push an element x to stack1
    void push1(int x)
    {
        // There is at least one empty space for new element
        if (top1 < top2 - 1) {
            top1++;
            arr[top1] = x;
        }
        else {
            cout << "Stack Overflow";
            exit(1);
        }
    }
 
    // Method to push an element x to stack2
    void push2(int x)
    {
        // There is at least one empty
        // space for new element
        if (top1 < top2 - 1) {
            top2--;
            arr[top2] = x;
        }
        else {
            cout << "Stack Overflow";
            exit(1);
        }
    }
 
    // Method to pop an element from first stack
    int pop1()
    {
        if (top1 >= 0) {
            int x = arr[top1];
            top1--;
            return x;
        }
        else {
            cout << "Stack UnderFlow";
            exit(1);
        }
    }
 
    // Method to pop an element from second stack
    int pop2()
    {
        if (top2 < size) {
            int x = arr[top2];
            top2++;
            return x;
        }
        else {
            cout << "Stack UnderFlow";
            exit(1);
        }
    }
};
 
/* Driver program to test twoStacks class */
int main()
{
    twoStacks ts(5);
    ts.push1(5);
    ts.push2(10);
    ts.push2(15);
    ts.push1(11);
    ts.push2(7);
    cout << "Popped element from stack1 is "
         << ts.pop1();
    ts.push2(40);
    cout << "\nPopped element from stack2 is "
         << ts.pop2();
    return 0;
}

Java

// Java program to implement two stacks in a
// single array
class TwoStacks {
    int size;
    int top1, top2;
    int arr[];
 
    // Constructor
    TwoStacks(int n)
    {
        arr = new int[n];
        size = n;
        top1 = -1;
        top2 = size;
    }
 
    // Method to push an element x to stack1
    void push1(int x)
    {
        // There is at least one empty space for
        // new element
        if (top1 < top2 - 1) {
            top1++;
            arr[top1] = x;
        }
        else {
            System.out.println("Stack Overflow");
            System.exit(1);
        }
    }
 
    // Method to push an element x to stack2
    void push2(int x)
    {
        // There is at least one empty space for
        // new element
        if (top1 < top2 - 1) {
            top2--;
            arr[top2] = x;
        }
        else {
            System.out.println("Stack Overflow");
            System.exit(1);
        }
    }
 
    // Method to pop an element from first stack
    int pop1()
    {
        if (top1 >= 0) {
            int x = arr[top1];
            top1--;
            return x;
        }
        else {
            System.out.println("Stack Underflow");
            System.exit(1);
        }
        return 0;
    }
 
    // Method to pop an element from second stack
    int pop2()
    {
        if (top2 < size) {
            int x = arr[top2];
            top2++;
            return x;
        }
        else {
            System.out.println("Stack Underflow");
            System.exit(1);
        }
        return 0;
    }
 
    // Driver program to test twoStack class
    public static void main(String args[])
    {
        TwoStacks ts = new TwoStacks(5);
        ts.push1(5);
        ts.push2(10);
        ts.push2(15);
        ts.push1(11);
        ts.push2(7);
        System.out.println("Popped element from"
                           + " stack1 is " + ts.pop1());
        ts.push2(40);
        System.out.println("Popped element from"
                           + " stack2 is " + ts.pop2());
    }
}
// This code has been contributed by
// Amit Khandelwal(Amit Khandelwal 1).

Python

# Python Script to Implement two stacks in a list
class twoStacks:
     
    def __init__(self, n):     # constructor
        self.size = n
        self.arr = [None] * n
        self.top1 = -1
        self.top2 = self.size
         
    # Method to push an element x to stack1
    def push1(self, x):
         
        # There is at least one empty space for new element
        if self.top1 < self.top2 - 1 :
            self.top1 = self.top1 + 1
            self.arr[self.top1] = x
 
        else:
            print("Stack Overflow ")
            exit(1)
 
    # Method to push an element x to stack2
    def push2(self, x):
 
        # There is at least one empty space for new element
        if self.top1 < self.top2 - 1:
            self.top2 = self.top2 - 1
            self.arr[self.top2] = x
 
        else :
           print("Stack Overflow ")
           exit(1)
 
    # Method to pop an element from first stack
    def pop1(self):
        if self.top1 >= 0:
            x = self.arr[self.top1]
            self.top1 = self.top1 -1
            return x
        else:
            print("Stack Underflow ")
            exit(1)
 
    # Method to pop an element from second stack
    def pop2(self):
        if self.top2 < self.size:
            x = self.arr[self.top2]
            self.top2 = self.top2 + 1
            return x
        else:
            print("Stack Underflow ")
            exit()
 
# Driver program to test twoStacks class
ts = twoStacks(5)
ts.push1(5)
ts.push2(10)
ts.push2(15)
ts.push1(11)
ts.push2(7)
 
print("Popped element from stack1 is " + str(ts.pop1()))
ts.push2(40)
print("Popped element from stack2 is " + str(ts.pop2()))
 
# This code is contributed by Sunny Karira

C#

// C# program to implement two
// stacks in a single array
using System;
 
public class TwoStacks {
    public int size;
    public int top1, top2;
    public int[] arr;
 
    // Constructor
    public TwoStacks(int n)
    {
        arr = new int[n];
        size = n;
        top1 = -1;
        top2 = size;
    }
 
    // Method to push an element x to stack1
    public virtual void push1(int x)
    {
        // There is at least one empty
        // space for new element
        if (top1 < top2 - 1) {
            top1++;
            arr[top1] = x;
        }
        else {
            Console.WriteLine("Stack Overflow");
            Environment.Exit(1);
        }
    }
 
    // Method to push an element x to stack2
    public virtual void push2(int x)
    {
        // There is at least one empty
        // space for new element
        if (top1 < top2 - 1) {
            top2--;
            arr[top2] = x;
        }
        else {
            Console.WriteLine("Stack Overflow");
            Environment.Exit(1);
        }
    }
 
    // Method to pop an element
    // from first stack
    public virtual int pop1()
    {
        if (top1 >= 0) {
            int x = arr[top1];
            top1--;
            return x;
        }
        else {
            Console.WriteLine("Stack Underflow");
            Environment.Exit(1);
        }
        return 0;
    }
 
    // Method to pop an element
    // from second stack
    public virtual int pop2()
    {
        if (top2 < size) {
            int x = arr[top2];
            top2++;
            return x;
        }
        else {
            Console.WriteLine("Stack Underflow");
            Environment.Exit(1);
        }
        return 0;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        TwoStacks ts = new TwoStacks(5);
        ts.push1(5);
        ts.push2(10);
        ts.push2(15);
        ts.push1(11);
        ts.push2(7);
        Console.WriteLine("Popped element from"
                          + " stack1 is " + ts.pop1());
        ts.push2(40);
        Console.WriteLine("Popped element from"
                          + " stack2 is " + ts.pop2());
    }
}
 
// This code is contributed by Shrikant13

PHP

size = $n;
        $this->arr = array();
        $this->top1 = -1;
        $this->top2 = $this->size;
    }
 
// Method to push an element x to stack1
function push1($x)
{
    // There is at least one empty
    // space for new element
    if ($this->top1 < $this->top2 - 1)
    {
        $this->top1++;
        $this->arr[$this->top1] = $x;
    }
    else
    {
        echo "Stack Overflow";
        exit();
    }
}
 
// Method to push an element x to stack2
function push2($x)
{
    // There is at least one empty space
    // for new element
    if ($this->top1 < $this->top2 - 1)
    {
        $this->top2--;
        $this->arr[$this->top2] = $x;
    }
    else
    {
        echo "Stack Overflow";
        exit();
    }
}
 
// Method to pop an element
// from first stack
function pop1()
{
    if ($this->top1 >= 0 )
    {
        $x = $this->arr[$this->top1];
        $this->top1--;
        return $x;
    }
    else
    {
        echo "Stack UnderFlow";
        exit();
    }
}
 
// Method to pop an element from
// second stack
function pop2()
{
    if ($this->top2 < $this->size)
    {
        $x = $this->arr[$this->top2];
        $this->top2++;
        return $x;
    }
    else
    {
        echo "Stack UnderFlow";
        exit();
    }
}
};
 
 
// Driver Code
$ts = new twoStacks(5);
$ts->push1(5);
$ts->push2(10);
$ts->push2(15);
$ts->push1(11);
$ts->push2(7);
echo "Popped element from stack1 is " .
                           $ts->pop1();
$ts->push2(40);
echo "\nPopped element from stack2 is " .
                             $ts->pop2();
 
// This code is contributed by
// rathbhupendra
?>

Javascript


输出:

Popped element from stack1 is 11
Popped element from stack2 is 40

复杂性分析:

  • 时间复杂度:
    • 推送操作: O(1)
    • 弹出操作: O(1)
  • 辅助空间: O(N)。
    使用数组实现栈,是一种空间优化的方法。