-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path(1)_stack-using-arrays.py
53 lines (45 loc) · 1.79 KB
/
(1)_stack-using-arrays.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
45
46
47
48
49
50
51
52
53
# Stack Abstract Data Type (ADT)
# ==============
# A stack is an Abstract Data Type (ADT), commonly used in most programming languages.
# It is named stack as it behaves like a real-world stack,
# for example – a deck of cards or a pile of plates, etc.
# A real-world stack allows operations at one end only.
# For example, we can place or remove a card or plate from the top of the stack only.
# Likewise, Stack ADT allows all data operations at one end only.
# At any given time, we can only access the top element of a stack.
# This feature makes it LIFO data structure.
# LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last,
# is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation.
#
# In Python, a stack can be implemented using either an Array or a Linked List.
# This tutorial will consider an Array implementation of a stack
# StackArray Class
class ArrayStack:
def __init__(self):
self._data = []
def __len__(self):
return len(self._data)
def is_empty(self):
return len(self._data) == 0
def push(self, element):
self._data.append(element)
def pop(self):
if self.is_empty():
print('Stack is Empty')
return
return self._data.pop()
def top(self):
if self.is_empty():
print('Stack is Empty')
return
return self._data[ - 1]
array_stack = ArrayStack()
array_stack.push(5)
array_stack.push(7)
array_stack.push(9)
print('Push Element:', array_stack._data)
print('Stack Length:', len(array_stack))
print('Pop Element:', array_stack.pop())
print('Push Element:',array_stack._data)
print('Stack Length:', len(array_stack))
print('Top Element:', array_stack.top())