-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
79 lines (74 loc) · 2.52 KB
/
Copy pathstack.py
File metadata and controls
79 lines (74 loc) · 2.52 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Stack:
def __init__(self):
'''
Objective: To initialize data memebers of the Stack
Input Parameter:
self(implicit parameter) -object of the type Stack
Return Value: None
'''
self.values=list()
def push(self,element):
'''
Objective: To put an elemenet on top the stack
Input Parameter:
self(implicit parameter) -object of the type Stack
element-value to be inserted
Return Value: None
'''
self.values.append(element)
def isempty(self):
'''
Objective: To determine if the stack is empty
Input Parameter:
self(implicit parameter) -object of the type Stack
element-value to be inserted
Return Value: true if the stack is empty.else false
'''
return len(self.values)==0
def pop(self):
'''
Objective: To remove an element from the top of a stack
Input Parameter:
self(implicit parameter) -object of the type Stack
element-value to be inserted
Return Value: top element of the stack, if stack is not empty,else None
'''
if(not(self.isempty())):
return self.values.pop()
else:
print('Stack Underflow')
return None
def top(self):
'''
Objective: To return the top of the stack
Input Parameter:
self(implicit parameter) -object of the type Stack
element-value to be inserted
Return Value: top element of the stack, if stack is not empty,else None
'''
if(not(self.isempty())):
return self.values[-1]
else:
print('Stack Empyty')
return None
def size(self):
'''
Objective: To return the number of elements in the stack.
Input Parameter:
self(implicit parameter) -object of the type Stack
element-value to be inserted
Return Value: Number of elements in stack - numeric
'''
return len(self.values)
def __str__(self):
'''
Objective: To return the string representation of the stack.
Input Parameter:
self(implicit parameter) -object of the type Stack
element-value to be inserted
Return Value: string
'''
stringRepr=''
for i in reversed(self.values):
stringRepr += str(i) +'\t'
return stringRepr