-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds Stack data structure in Python Language. Includes basic functionality of stack (push, pop, top, display). Closes #37
- Loading branch information
1 parent
3ca5152
commit 9f57bb2
Showing
2 changed files
with
64 additions
and
1 deletion.
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
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,63 @@ | ||
class Stack: | ||
""" | ||
Class for operations related to stack. | ||
""" | ||
|
||
def __init__(self): | ||
|
||
self.stack = [] # Initialise empty stack | ||
|
||
def push(self, dataval): | ||
""" | ||
Method to push items in stack. | ||
:param dataval: Value that user needs to push. | ||
""" | ||
|
||
self.stack.append(dataval) | ||
|
||
def pop(self): | ||
""" | ||
Method for removing the last element from the stack. | ||
:return: Element that is popped. | ||
""" | ||
|
||
if len(self.stack) <= 0: | ||
# Raise an error if stack is empty. | ||
return "No element in the Stack" | ||
|
||
else: | ||
return self.stack.pop() | ||
|
||
def top(self): | ||
""" | ||
Method to show the last element of the stack. | ||
:return: last element of the stack. | ||
""" | ||
|
||
return self.stack[len(self.stack)-1] | ||
|
||
def display(self): | ||
""" | ||
Method to print all elements of the stack. | ||
""" | ||
|
||
print(*self.stack) | ||
|
||
|
||
def main(): | ||
AStack = Stack() | ||
AStack.push("Mon") | ||
AStack.push("Tue") | ||
AStack.pop() | ||
AStack.push("Wed") | ||
m = AStack.top() | ||
print(m) | ||
AStack.push("Thu") | ||
AStack.display() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |