forked from iampiyushjain/Text-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
47 lines (39 loc) · 1.23 KB
/
Stack.py
File metadata and controls
47 lines (39 loc) · 1.23 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
# Class Stack is used only for Stack management i.e. for undo and redo operations
class Stack:
def __init__(self, text):
self.stack = []
self.stack.append(text)
def add(self, dataval):
# Use list append method to add element
if dataval not in self.stack:
self.stack.append(dataval)
return True
else:
return False
# Use list pop method to remove element
def remove(self):
if len(self.stack) <= 1:
return "No element in the Stack"
else:
return self.stack.pop()
# It gives the top element of stack
def peek(self):
if len(self.stack) == 1:
return self.stack[0]
else:
return self.stack[-1]
# It prints all element of stack
def print_all(self):
length = len(self.stack) - 1
while self.stack:
print(self.stack[length])
length -= 1
# It return the size of the stack
def size(self):
return len(self.stack)
# It clears the stack
def clear_stack(self):
return self.stack.clear()
# It returns the particular element of the stack
def ele(self, index):
return self.stack[index]