-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from narayancse/main
Stack Implementation in Python
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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') |