File tree 1 file changed +48
-0
lines changed
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Dynamic_Stack :
2
+ capacity = 0
3
+ stack = []
4
+
5
+ def __init__ (self , capacity ):
6
+ self .capacity = capacity
7
+ self .stack = []
8
+
9
+ def isFull (self ):
10
+ return len (self .stack ) == self .capacity
11
+
12
+ def isEmpty (self ):
13
+ return len (self .stack ) == 0
14
+
15
+ def push (self , element ):
16
+ if (self .isFull ()):
17
+ return
18
+ else :
19
+ self .stack .append (element )
20
+
21
+ def pop (self ):
22
+ if (self .isEmpty ()):
23
+ return - 1
24
+ else :
25
+ temp = self .stack [- 1 ]
26
+ self .stack [:- 2 ]
27
+ return temp
28
+
29
+ def peek (self ):
30
+ if (self .isEmpty ()):
31
+ return - 1
32
+ else :
33
+ return self .stack [- 1 ]
34
+
35
+ # Example
36
+ a = Dynamic_Stack (4 )
37
+
38
+ print (a .isEmpty ())
39
+ a .push (1 )
40
+ print (a .isEmpty ())
41
+ a .push (2 )
42
+ a .push (3 )
43
+ print (a .isFull ())
44
+ a .push (4 )
45
+ print (a .isFull ())
46
+ print (a .peek ())
47
+ print (a .pop ())
48
+ print (a .peek ())
You can’t perform that action at this time.
0 commit comments