Question: Add a print function to the Stack class. Your print function should print the elements in the stack in the order they will be popped

Add a print function to the Stack class. Your print function should print the elements in the stack in the order they will be popped off the stack. Make sure that the stack has the same elements before and after calling the print function. For example execute a peek() before popping items off the stack, and a peek() afterwards.

Write the print function here:

class Stack:

def __init__(self):

self.items = []

def isEmpty(self):

return self.items == []

def push(self, item):

self.items.append(item)

def pop(self):

return self.items.pop()

def peek(self):

return self.items[len(self.items)-1]

def size(self):

return len(self.items)

def main():

s=Stack()

print(s.isEmpty())

s.push(4)

s.push('dog')

print(s.peek())

s.push(True)

print(s.size())

print(s.isEmpty())

s.push(8.4)

print(s.pop())

print(s.pop())

print(s.size())

s.pop()

if __name__ == "__main__":

main()

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!