Question: Consider that a Stack class has been implemented containing the push ( element ) , pop ( ) , peek ( ) and isEmpty (

Consider that a Stack class has been implemented containing the push(element), pop(), peek() and isEmpty() functions.
Complete the function conditional_reverse() which will take an object of Stack class as an input that contains some integer values. The function returns a new stack which will contain the values in reverse order from the given stack except the consecutive ones. You cannot use any other data structure except Stack.
Note: The Stack class implements a singly linked list-based Stack hence overflow is not possible. The pop() and peek() functions return None in case of the underflow.
In the following example, consider the rightmost element to be the topmost element of the stack.
Sample Input Stack
(Rightmost is the top)
Output Reversed Stack (Rightmost is the top)
Explanation
Stack: 10,10,20,20,30,10,50
Top =50
New Stack: 50,10,30,20,10
Top =10
Consecutive 20 and 10 are not present in the output reversed stack
Driver code:
def conditional_reverse(stack):
#To Do
print('Test 01')
st=Stack()
st.push(10)
st.push(10)
st.push(20)
st.push(20)
st.push(30)
st.push(10)
st.push(50)
print('Stack:')
print_stack(st)
print('------')
reversed_stack=conditional_reverse(st)
print('Conditional Reversed Stack:')
print_stack(reversed_stack) # This stack contains 50,10,30,20,10 in this order whereas top element should be 10
print('------')
Linked List based Stack is implemented in the following cell.
class Node:
def __init__(self,elem=None,next=None):
self.elem = elem
self.next = next
class Stack:
def __init__(self):
self.__top = None
def push(self,elem):
nn = Node(elem,self.__top)
self.__top = nn
def pop(self):
if self.__top == None:
#print('Stack Underflow')
return None
e = self.__top
self.__top = self.__top.next
e.next = None
return e.elem
def peek(self):
if self.__top == None:
#print('Stack Underflow')
return None
return self.__top.elem
def isEmpty(self):
return self.__top == None
#You can run this driver code cell to understand the methods of Stack class
st = Stack()
st.push(4)
st.push(3)
st.push(5)
st.push(1)
st.push(9)
print('Peeked Element: ',st.peek())
print('Popped Element: ',st.pop())
print('Popped Element: ',st.pop())
print('Popped Element: ',st.pop())
print('Peeked Element: ',st.peek())
print('Popped Element: ',st.pop())
print('Popped Element: ',st.pop())
print('Peeked Element: ',st.peek())
print('Popped Element: ',st.pop())
print(st.isEmpty())
You can print your stack using this code segment
def print_stack(st):
if st.isEmpty():
return
p = st.pop()
print('|',p,end='')
if p<10:
print('|')
else:
print('|')
#print('------')
print_stack(st)
st.push(p)
# st = Stack()
# st.push(4)
# st.push(3)
# st.push(5)
# st.push(1)
# st.push(9)
# print_stack(st)
# print('------')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!