stack4

py

School

Texas A&M University, Commerce *

*We aren’t endorsed by this school

Course

450

Subject

Computer Science

Date

Nov 24, 2024

Type

py

Pages

1

Uploaded by ElderRockAnteater16

Report
# Design a Data Structure SpecialStack that supports all the stack operations # like push(), pop(), isEmpty(), isFull() and an additional operation getMin() # which should return minimum element from the SpecialStack. class SpecialStack: def __init__(self): self.mainStack = [] self.minStack = [] def push(self, value): self.mainStack.append(value) if not self.minStack or value <= self.minStack[-1]: self.minStack.append(value) def pop(self): if not self.mainStack: raise IndexError("Pop from an empty stack") if self.mainStack[-1] == self.minStack[-1]: self.minStack.pop() return self.mainStack.pop() def isEmpty(self): return not self.mainStack def isFull(self): return False def getMinVal(self): if not self.minStack: raise ValueError("Special Stack is empty") return self.minStack[-1] stack = SpecialStack() stack.push(3) stack.push(5) stack.push(2) stack.push(1) print("Minimum Element: ", stack.getMinVal()) stack.pop() stack.pop() print("Minimum Element: ", stack.getMinVal())
Discover more documents: Sign up today!
Unlock a world of knowledge! Explore tailored content for a richer learning experience. Here's what you'll get:
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help