MCPcopy Create free account
hub / github.com/Ayush7614/Daily-Coding-DS-ALGO-Practice / Stack

Class Stack

Data Structures/Stack/Python/Stack.py:9–48  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8
9class Stack:
10 def __init__(self):
11 self.__head = None
12 self.__size = 0
13
14 def push(self, data):
15 # adds an element at the beginning
16 self.__size += 1
17 new_node = Node(data)
18 new_node.ref = self.__head
19 self.__head = new_node
20
21 def pop(self):
22 # deletes element from beginning
23 if self.isEmpty():
24 print("Stack is empty.")
25 return
26 self.__size -= 1
27 print(self.__head.data)
28 self.__head = self.__head.ref
29
30 def top(self):
31 # prints the topmost element, i.e, head in the case of LL
32 if self.isEmpty():
33 print("Stack is empty.")
34 return
35 print(self.__head.data)
36
37 def isEmpty(self):
38 return(self.__size == 0)
39
40 def display(self):
41 # displays the stack from top to bottom
42 n = self.__head
43 if n is None:
44 print("Empty!")
45 return
46 while n is not None:
47 print(n.data)
48 n = n.ref
49
50
51if __name__ == "__main__":

Callers 1

Stack.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected