Question 2: Extend the UnorderedList class by adding the append(self, item)metho
ID: 3838511 • Letter: Q
Question
Question 2:
Extend the UnorderedList class by adding the append(self, item)method that takes an item as a parameter and inserts the item to the end of the unordered linked list.
The implementations of the Node and LinkedListIterator are provided to you as part of this exercise. You can simply use: Node(), get_next(), as well as get_data() as necessary in your function definition.
Note: include the entire class definition,including the __iter__(self), in your answer to this question.
===========================================================
For Example:
===========================================================
Test:
my_list = UnorderedList()
my_list.append(13)
for num in my_list:
print(num, end=" ")
print()
===========================================================
Result:
13
===========================================================
Test:
my_list = UnorderedList()
my_list.add(1)
my_list.append(13)
for num in my_list:
print(num, end=" ")
print()
===========================================================
Result:
1 13
===========================================================
Explanation / Answer
class UnorderedList:
def __init__(self, DataItem):
self.data = DataItem
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, newdata):
self.data = newdata
def setNext(self, newnext):
self.next = newnext
class Node:
def __init__(self):
self.head = None
self.lssize = 0
def isEmpty(self):
return self.head == None
def add(self, item):
self.lssize += 1
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def search(self, item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def append(self, item):
"""
Searches till the end of the
linked list so it can append
the given value or item
:param item:
:return:
"""
current = self.head
found = False
while current != None and not found:
if current.getNext() == None:
newitem = Node(item)
current.setNext(newitem)
self.lssize += 1
found = True
else:
current = current.getNext()
return found
def insert(self, pos, item):
# check if the list is empty
# if it is then just call the add
# method
newitem = Node(item)
done = False
if self.size() == 0:
self.add(newitem)
else:
current = self.head
curpos = 0
while current != None and not done:
if pos == 1:
self.add(item)
done = True
curpos += 1
if curpos == pos - 1:
n = current.getNext()
newitem.setNext(n)
current.setNext(newitem)
self.lssize += 1
done = True
else:
current = current.getNext()
return done
def index(self, item):
curpos = 0
found = False
current = self.head
if self.size() == 0:
print("Linked list is empty!")
else:
while current != None and not found:
curpos += 1
if current.getData() == item:
found = True
else:
current = current.getNext()
return curpos
mylist = UnorderedList()
mylist.add(10)
mylist.add(20)
mylist.add(30)
mylist.add(40)
# print(mylist.index(53))
# print(mylist.index(67))
# print(UnorderedList.lssize())
mylist.append(60)
# mylist.insert(1, 45)
//hope this code will helpful for you.
thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.