Skip to content

Instantly share code, notes, and snippets.

@BhJaipal
Last active June 24, 2023 08:45
Show Gist options
  • Save BhJaipal/b4f9673d8933be9681176bfdee1997a8 to your computer and use it in GitHub Desktop.
Save BhJaipal/b4f9673d8933be9681176bfdee1997a8 to your computer and use it in GitHub Desktop.
Stack
class Stack:
def __init__(self):
self.list= list()
def push(self, element):
self.list.append(element)
def pushMany(self, *args):
self.list.extend(args)
def pop(self):
if (len(self.list) == 0):
print("Cannot pop element, stack is empty")
else:
return self.list.pop()
def popMany(self, n: int):
popedElemList= []
for i in range(n):
popedElemList.append(self.list.pop())
return popedElemList
def length(self)-> int:
return len(self.list)
def printList(self)-> None:
for i in self.list:
print(i, end=" ")
print()
stack= Stack()
stack.push(3)
stack.push(8)
print("Poped element is", stack.pop())
stack.pushMany(2, 7,3, 8)
print("Poped element list is", stack.popMany(3))
print(f"Remaining elements are :", end=" ")
stack.printList()
print("and its length is", stack.length())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment