Repository navigation
Expand file tree
/
Copy pathstack.py
More file actions
98 lines (78 loc) · 3.38 KB
/
Copy pathstack.py
File metadata and controls
98 lines (78 loc) · 3.38 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#===============================================================================
# implementation of Stack using Array
#===============================================================================
import numpy
import random
class Stack:
#===============================================================================
# Arrays, linked lists, trees, etc. are best for
# data that represents real objects.
#
# Stacks & Queues are instead used to complete a
# task and are soon after discarded.
#
# Stacks & Queues
# 1. Allow only a single item to be added or removed at a time
# 2. Stacks allow access to the last item inserted (LIFO)
#===============================================================================
#===========================================================================
# initializing stack with specific size
#===========================================================================
def __init__(self, stack_size):
self.stack_array = numpy.array([None] * stack_size)
self.stack_size = stack_size
#Sets stack as empty
self.top_of_stack = -1
print "Stack with size %d initialized" %(stack_size)
self.display_stack()
def display_stack(self):
print "[ " ,
for element in self.stack_array:
if element != None:
print element , " ," ,
else:
print " ," ,
print "]"
def push(self, input):
if self.top_of_stack + 1 < self.stack_size:
self.top_of_stack += 1
self.stack_array[self.top_of_stack] = input
print "PUSH : value %s added to stack" %(input)
else:
print "Sorry but stack is already full."
self.display_stack()
def push_many(self, multiple_values):
for value in multiple_values:
self.push(value)
def pop(self):
if( self.top_of_stack >= 0):
print "POP : value %s removed from stack" %(self.stack_array[self.top_of_stack])
self.stack_array[self.top_of_stack] = None
self.top_of_stack -= 1
self.display_stack()
else:
print "Stack is already empty"
def pop_all(self):
for i in len(self.stack_array):
self.pop()
def peek(self):
if ( self.top_of_stack >= 0):
print "Top of the stack is %s" %(self.stack_array[self.top_of_stack])
else:
print "Stack is already empty"
stack_size = int(raw_input("Enter the size of the stack : "))
new_stack = Stack(stack_size)
#===============================================================================
# #Add many to the stack
new_stack.push_many(random.randint(0, 100) for _ in range(stack_size))
#===============================================================================
#===============================================================================
# # Look at the top value on the stack
new_stack.peek()
#===============================================================================
#===============================================================================
# # Remove values from the stack (LIFO)
new_stack.pop()
#===============================================================================
#Remove all from the stack
#new_stack.pop_all()