MCPcopy
hub / github.com/TheAlgorithms/Python / push

Method push

data_structures/stacks/stack.py:35–55  ·  view source on GitHub ↗

Push an element to the top of the stack. >>> S = Stack(2) # stack size = 2 >>> S.push(10) >>> S.push(20) >>> print(S) [10, 20] >>> S = Stack(1) # stack size = 1 >>> S.push(10) >>> S.push(20) Traceback (most recent cal

(self, data: T)

Source from the content-addressed store, hash-verified

33 return str(self.stack)
34
35 def push(self, data: T) -> None:
36 """
37 Push an element to the top of the stack.
38
39 >>> S = Stack(2) # stack size = 2
40 >>> S.push(10)
41 >>> S.push(20)
42 >>> print(S)
43 [10, 20]
44
45 >>> S = Stack(1) # stack size = 1
46 >>> S.push(10)
47 >>> S.push(20)
48 Traceback (most recent call last):
49 ...
50 data_structures.stacks.stack.StackOverflowError
51
52 """
53 if len(self.stack) >= self.limit:
54 raise StackOverflowError
55 self.stack.append(data)
56
57 def pop(self) -> T:
58 """

Callers 4

infix_to_postfixFunction · 0.95
test_stackFunction · 0.95
balanced_parenthesesFunction · 0.95

Calls 1

appendMethod · 0.45

Tested by 1

test_stackFunction · 0.76