Question: Create a class DoubleStack to implement two stacks in one array A[1.n] in such a way that neither stack overflows unless the total number of

  1. Create a class DoubleStack to implement two stacks in one array A[1.n] in such a way that neither stack overflows unless the total number of elements in both stacks together is size n. The Push and Pop operations should run in O(1).

The class should implement following functions:

  1. Write a constructor that takes an integer size as argument and creates an array for size number of elements.
  2. Write a function Push that inserts an element for the stack s.
  3. Write a function Pop that removes an element for the stack s.
  4. Write a function Peek that returns an element for the stack s.
  5. Write a function IsEmpty that returns true stack s is empty.
  6. Write a function Count that returns the number of elements in stack s

class DoubleStack:

# size = number of elements

def __init__(self, size):

// your code goes here

# s = 1 or 2

def Push(self, s, value):

// your code goes here

# stack = 1 or 2

def Pop(self, s):

// your code goes here

# s = 1 or 2

def Peek(self, s):

// your code goes here

# s = 1 or 2

def IsEmpty(self, s):

// your code goes here

# s = 1 or 2

def Count(self, s):

// your code goes here

Example:

ds = DoubleStack(10)

ds.Push(1,10) #push 10 in stack 1

ds.Push(2,20) #push 20 in stack 2

ds.Push(1,30) #push 30 in stack 1

print(ds.Pop(1)) #pop from stack 1

print(ds.Peek(2)) # peek from stack 2

#the function should return

30

20

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!