Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Remove Compartment Sheldon is a train maniac. He loves attaching each compartment of a train to the next with a linking chain as his hobby.

Remove Compartment
Sheldon is a train maniac. He loves attaching each compartment of a train to the next with a linking chain as his hobby. Now he is removing the nth compartment from the end of the train. Can you model the scenario by writing a method that takes the compartment sequence and a number; the method returns the changed (or unchanged) train compartment sequence.
Constraint:
You cannot create any new linked list. You have to change the given one.
Sample Input
10->15->34->41->56->72
2
10->15->34->41->56->72
7
10->15->34->41->56->72
6
Sample Output
10->15->34->41->72
10->15->34->41->56->72
15->34->41->56->72
You must Run this cell for your driver code to execute successfully
#Run this cell
class Node:
def __init__(self,elem,next = None):
self.elem,self.next = elem,next
def createList(arr):
head = Node(arr[0])
tail = head
for i in range(1,len(arr)):
newNode = Node(arr[i])
tail.next = newNode
tail = newNode
return head
def printLinkedList(head):
temp = head
while temp != None:
if temp.next != None:
print(temp.elem, end ='-->')
else:
print(temp.elem)
temp = temp.next
print()
Driver code:
def remove_compartment(head,n):
#TO DO
print('==============Test Case 1=============')
head = createList(np.array([10,15,34,41,56,72]))
print('Original Compartment Sequence: ', end ='')
printLinkedList(head)
head = remove_compartment(head,2)
print('Changed Compartment Sequence: ', end ='')
printLinkedList(head) #This should print 10-->15-->34-->41-->72
print()
print('==============Test Case 2=============')
head = createList(np.array([10,15,34,41,56,72]))
print('Original Compartment Sequence: ', end ='')
printLinkedList(head)
head = remove_compartment(head,7)
print('Changed Compartment Sequence: ', end ='')
printLinkedList(head) #This should print 10-->15-->34-->41-->56-->72
print()
print('==============Test Case 3=============')
head = createList(np.array([10,15,34,41,56,72]))
print('Original Compartment Sequence: ', end ='')
printLinkedList(head)
head = remove_compartment(head,6)
print('Changed Compartment Sequence: ', end ='')
printLinkedList(head) #This should print 15-->34-->41-->56-->72
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

Step: 3

blur-text-image

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

Database And Expert Systems Applications 23rd International Conference Dexa 2012 Vienna Austria September 2012 Proceedings Part 1 Lncs 7446

Authors: Stephen W. Liddle ,Klaus-Dieter Schewe ,A Min Tjoa ,Xiaofang Zhou

2012th Edition

3642325998, 978-3642325991

More Books

Students also viewed these Databases questions

Question

Define Management or What is Management?

Answered: 1 week ago

Question

What do you understand by MBO?

Answered: 1 week ago

Question

What is meant by planning or define planning?

Answered: 1 week ago