Today we are going to share Python program to delete middle of a stack without using additional data structure. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share a Python program to delete middle of a stack without using additional data structure.
To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.
Copy the below python program and execute it with the help of python compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items) def deleteMid(st, n, curr) : if (st.isEmpty() or curr == n) : return x = st.peek() st.pop() deleteMid(st, n, curr+1) if (curr != int(n/2)) : st.push(x) st = Stack() st.push('1') st.push('2') st.push('3') st.push('4') st.push('5') st.push('6') st.push('7') deleteMid(st, st.size(), 0) while (st.isEmpty() == False) : p = st.peek() st.pop() print (str(p) + " ", end="") |
7 6 5 3 2 1
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.