Skip to content

Commit

Permalink
Added stack using Python 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
parthjdoshi committed Oct 2, 2018
1 parent 75bc4de commit 89698ef
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions stack/stack.py
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__()

0 comments on commit 89698ef

Please sign in to comment.