Python Issue 1.Extend the UnorderedList class by adding the pop(self, index) met
ID: 3752019 • Letter: P
Question
Python Issue
1.Extend the UnorderedList class by adding the pop(self, index) method that that takes an integer index as a parameter and removes the item at the index position in the unordered linked list. The index should be in the range [0..length-1]. If the method is called without any parameter, the last element will be removed from the unordered linked list.
The implementations of the Node is provided to you as part of this exercise. You can simply use: Node(), get_next(), set_next(), as well as get_data() and set_data() as necessary in your function definition.
For example:
2.**Extend the UnorderedList class by adding the insert(self, index, item) method that that takes an integer index as a parameter and inserts the new item at the index position in the unordered linked list. The index should be in the range [0..length-1].
The implementations of the Node is provided to you as part of this exercise. You can simply use: Node(), get_next(), set_next(), as well as get_data() and set_data() as necessary in your function definition.
For example:
Test Resultmy_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) print(my_list.pop())
3
my_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) print(my_list.pop(0))
8
Explanation / Answer
class UnorderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
#adds new node at the begning list.
def add(self,item):
temp = Node(item)
temp.set_next(self.head)
self.head = temp
#returns the size of the list.
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.get_next()
return count
def pop(self, index = None):
current = self.head
previous = None
count = 0
#if no position is provided pop the last item.
if index == None:
index = self.size() - 1
while not count == index:
count = count + 1
previous = current
current = current.get_next()
popped_value = current.get_data()
#if the index doesn't exists.
if(current == NULL):
return None
#if the given index is 0:
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
return popped_value
def insert(self, index, item):
current = self.head
previous = None
count = 0
#if index is out of range.
if index > self.size():
print("Invalid index.")
return
while not count == index:
count = count + 1
previous = current
current = current.get_next()
#if end of the list has been reached.
#Add a new node.
if(current == None):
temp = Node(item)
previous.set_next(temp)
return
if previous == None:
self.add(item)
else:
temp = Node(item)
previous.set_next(temp)
temp.set_next(current)
def __str__(self):
data = []
current = self.head
while not current == None:
data.append(current.get_data())
current = current.get_next()
return str(data)
--------------------------------------------------------------------------------------------------------------------------------
The question didn't ask that. But if want pop function to pop a specific item present in the list then,
here is the sample code for that:
def pop(self, item):
current = self.head
previous = None
while not item == current.get_data():
previous = current
current = current.get_next()
#if end of list is met.
if(curent == None):
return
print(item, " popped out.")
previous.set_next(current.get_next())
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.