Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

python

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions