Skip to content

Commit

Permalink
Merge pull request #48 from narayancse/main
Browse files Browse the repository at this point in the history
Stack Implementation in Python
  • Loading branch information
narayancseian authored Oct 6, 2022
2 parents 5189dc7 + 6aa98ee commit 4d1bfa5
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Stack/Stack Implementation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Custom stack implementation in Python
class Stack:

# Constructor to initialize the stack
def __init__(self, size):
self.arr = [None] * size
self.capacity = size
self.top = -1

# Function to add an element `val` to the stack
def push(self, val):
if self.isFull():
print('Stack Overflow!! Calling exit()…')
exit(-1)

print(f'Inserting {val} into the stack…')
self.top = self.top + 1
self.arr[self.top] = val

# Function to pop a top element from the stack
def pop(self):
# check for stack underflow
if self.isEmpty():
print('Stack Underflow!! Calling exit()…')
exit(-1)

print(f'Removing {self.peek()} from the stack')

# decrease stack size by 1 and (optionally) return the popped element
top = self.arr[self.top]
self.top = self.top - 1
return top

# Function to return the top element of the stack
def peek(self):
if self.isEmpty():
exit(-1)
return self.arr[self.top]

# Function to return the size of the stack
def size(self):
return self.top + 1

# Function to check if the stack is empty or not
def isEmpty(self):
return self.size() == 0

# Function to check if the stack is full or not
def isFull(self):
return self.size() == self.capacity


if __name__ == '__main__':

stack = Stack(3)

stack.push(1) # Inserting 1 in the stack
stack.push(2) # Inserting 2 in the stack

stack.pop() # removing the top element (2)
stack.pop() # removing the top element (1)

stack.push(3) # Inserting 3 in the stack

print('Top element is', stack.peek())
print('The stack size is', stack.size())

stack.pop() # removing the top element (3)

# check if the stack is empty
if stack.isEmpty():
print('The stack is empty')
else:
print('The stack is not empty')

0 comments on commit 4d1bfa5

Please sign in to comment.