forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
44 lines (32 loc) · 1.03 KB
/
stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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__()