forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75bc4de
commit 89698ef
Showing
1 changed file
with
44 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,44 @@ | ||
# This is written in Python 3.5.2 | ||
|
||
class Stack(object): | ||
|
||
# l is a list/tuple which can be used for initialization of the stack. | ||
# The value at the last index position is considered as the top of the stack. | ||
def __init__(self, l = []): | ||
self.stack = list(l) | ||
|
||
|
||
# Inserting a new value at the top of the stack. | ||
def push(self, x): | ||
self.stack.append(x) | ||
print(x, "pushed at the top of the stack.") | ||
|
||
|
||
# Deleting the value at the top of the stack. | ||
def pop(self): | ||
if len(self.stack) == 0: | ||
print("The stack is empty.") | ||
return | ||
temp = self.stack.pop() | ||
print("The value found at the top of the stack is", temp) | ||
return temp | ||
|
||
|
||
# Returning the value at the top of the stack without deleting it. | ||
def peek(self): | ||
if len(self.stack) == 0: | ||
print("The stack is empty.") | ||
return | ||
temp = self.stack[-1] | ||
print("The value found at the top of the stack is", temp) | ||
return temp | ||
|
||
|
||
# Clearing the entire stack | ||
def delete(self): | ||
self.stack = [] | ||
|
||
|
||
# String representation of the stack class | ||
def __str__(self): | ||
return self.stack.__str__() |