Last active
June 24, 2023 08:45
-
-
Save BhJaipal/b4f9673d8933be9681176bfdee1997a8 to your computer and use it in GitHub Desktop.
Stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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