Question: 3 . def merge ( stack 1 : ListStack, stack 2 : ListStack ) ListStack: merge ( ) function merges two sorted ListStacks into one.
def mergestack: ListStack, stack: ListStack ListStack: merge function merges two sorted ListStacks into one. Test case: stack: stack:
This function should return
By the way we have to use this way to solve the problem:
class LinkedStack:
def initself:
#underlying data structure is a linked list
self.stack LinkedList
return the number of elements in this stack"""
def lenself:
return self.stack.len
return True if empty, False otherwise"""
def isemptyself:
return self.stack.isempty
return top element"""
def topself:
return self.stack.first
add e to the top of the stack"""
def pushself e:
self.stack.addfirste
remove and return the top element"""
def popself:
return self.stack.removefirst
return a string representation of this stack"""
def strself:
return self.stack.str
class ListStack:
def initself:
#underlying data structure is a list
self.stack list
return the number of elements in this stack"""
def lenself:
return lenselfstack
return True if empty, False otherwise"""
def isemptyself:
return lenselfstack
return top element"""
def topself:
if self.isempty:
return None
return self.stack
add e to the top of the stack"""
def pushself e:
self.stack.appende
remove and return the top element"""
def popself:
if self.isempty:
return None
return self.stack.pop
return a string representation of this stack"""
def strself:
display self.stack
display.reverse
return strdisplay
if namemain:
# create a linkedstack
s LinkedStack
# insert
for e in :
spushe
prints
# then remove everything
while not sisempty:
spop
prints
s ListStack
for e in :
spushe
prints
while not sisempty:
spop
prints
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
