File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change 1
1
class Stack :
2
- class EmptyStackError (Exception ):
2
+ class __EmptyStackError (Exception ):
3
3
def __str__ (self ) -> str :
4
4
return "Stack is empty."
5
5
@@ -21,7 +21,7 @@ def is_empty(self) -> bool:
21
21
22
22
def top (self ) -> object :
23
23
if self .is_empty ():
24
- raise Stack .EmptyStackError
24
+ raise Stack .__EmptyStackError
25
25
return self ._data [- 1 ]
26
26
27
27
def push (self , element : object ) -> None :
@@ -30,21 +30,25 @@ def push(self, element: object) -> None:
30
30
31
31
def pop (self ) -> object :
32
32
if self .is_empty ():
33
- raise Stack .EmptyStackError
33
+ raise Stack .__EmptyStackError
34
34
self ._size -= 1
35
35
return self ._data .pop ()
36
36
37
+ def empty_stack (self ) -> None :
38
+ while not self .is_empty ():
39
+ self .pop ()
40
+
37
41
38
42
if __name__ == '__main__' :
39
43
stack = Stack ()
40
44
print (stack .is_empty ())
41
45
for i in range (10 ):
42
46
stack .push (i )
43
47
print (stack .top ())
48
+ print (stack .pop ())
44
49
print (stack )
45
50
print (len (stack ))
46
51
print (stack .length ())
47
52
print (stack .is_empty ())
48
- for i in range (10 ):
49
- print (stack .pop ())
53
+ stack .empty_stack ()
50
54
print (stack .is_empty ())
You can’t perform that action at this time.
0 commit comments