-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackImplUsingArray.py
More file actions
44 lines (36 loc) · 954 Bytes
/
StackImplUsingArray.py
File metadata and controls
44 lines (36 loc) · 954 Bytes
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
class Stack:
def __init__(self,size):
self.list = [i for i in range(size)]
self.top = -1
def push(self, data):
if self.top == len(self.list) - 1:
return "Stack full"
self.top = self.top + 1
self.list[self.top] = data
return "success"
# returns top element from ther stack, returns None if stack is empty
def topElement(self):
if self.isEmpty():
return "Stack empty"
return self.list[self.top]
# removes the element from top of the stack and returns it, returns None if stack is empty
def pop(self):
if self.isEmpty():
return "Stack empty"
element = self.list[self.top]
self.top = self.top - 1
return element
# if stack is empty, returns 0 otherwise returns 1
def isEmpty(self):
if self.top == -1:
return True
else:
return False
def print(self):
if self.isEmpty():
print("Stack empty, nothing to print")
else:
i=0
while i <= self.top:
print(self.list[i])
i = i + 1