We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9f898b2 commit f1ccef8Copy full SHA for f1ccef8
1 file changed
DSA/stack.py
@@ -0,0 +1,35 @@
1
+# Stack implementation in python
2
+
3
+print("_______________________________________________________")
4
+# Creating a stack
5
+def create_stack():
6
+ stack = []
7
+ return stack
8
9
10
+# Creating an empty stack
11
+def check_empty(stack):
12
+ return len(stack) == 0
13
14
15
+# Adding items into the stack
16
+def push(stack, item):
17
+ stack.append(item)
18
+ print("pushed item: " + item)
19
20
21
+# Removing an element from the stack
22
+def pop(stack):
23
+ if (check_empty(stack)):
24
+ return "stack is empty"
25
26
+ return stack.pop()
27
28
29
+stack = create_stack()
30
+push(stack, str(1))
31
+push(stack, str(2))
32
+push(stack, str(3))
33
+push(stack, str(4))
34
+print("popped item: " + pop(stack))
35
+print("stack after popping an element: " + str(stack))
0 commit comments