Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions

Question

3. Is it a topic that your audience will find worthwhile?

Answered: 1 week ago

Question

2. Does the topic meet the criteria specified in the assignment?

Answered: 1 week ago