Python 3 Linked List Program: 1. Create a class Fruit that has the following var
ID: 3831708 • Letter: P
Question
Python 3 Linked List Program:
1. Create a class Fruit that has the following variables:
Fruit next
Fruit previous
String name
2. Write a program that will:
Create a doubly linked list of Fruits with the following values stored in the variable space name of each node (note that Apple will be the first element in the linked List and Mango the last). Hint: using a list holding the names and a for loop would make this easier.
Apple
Banana
Cantaloupe
Date
Elderberry
Fig
Grape
Honeyberry
Ice Cream Bean
Jostaberry
Kiwi
Lime
Mango
Develop functions that will:
Iterate through (from front to back) the linked list elements printing the values stored in name.
then Iterate through (from back to front) the linked list elements printing the values stored in name.
Ask the user for the name of a new fruit and add a new element (with that fruit stored as name) to the end of the linked list.
Ask the user to enter the name of a fruit and delete the element in the linked list that has the same name (if there is such an element)
Present to the user a menu that will allow them to select one of the options (functions) above.
Explanation / Answer
PROGRAM CODE:
class DList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def insert(self, data):
self.size += 1
if self.head is None:
self.head = Fruit(data)
self.tail = self.head
else:
p = Fruit(data)
p.next = self.head
self.head.previous = p
self.head = p
def remove(self, name):
if self.head is None:
raise ValueError('Removing off an empty list')
current = self.head
if current.name != name:
for _ in range(self.size):
if current.next.name != name:
current = current.next
else:
break
p = current.next.next
if p is None:
current.next = None
else:
current.next = p
p.previous = current
else:
self.head = self.head.next
self.head.previous = None
if self.head == None:
self.tail = None
def __sizeof__(self):
return self.size
def __repr__(self):
res = '[ '
current = self.head
while current is not None:
res += str(current.name)
res += ' '
current = current.next
res += ']'
return res
def reverseList(self):
res = '[ '
current = self.tail
while current is not None:
res += str(current.name)
res += ' '
current = current.previous
res += ']'
return res
class Fruit:
def __init__(self, data):
if data is None:
raise ValueError('Node value cannot be None')
self.name = data
self.previous = None
self.next = None
fruits = ["Apple", "Banana", "Cantaloupe", "Date", "Elderberry", "Fig", "Grape", "Honeyberry", "Ice Cream Bean", "Jostaberry", "Kiwi", "Lime", "Mango"]
fruitList = DList()
for i in range(0, len(fruits)):
fruitList.insert(fruits[i])
print(fruitList)
fruit = input('Enter a fruit to remove: ')
fruitList.remove(fruit)
print(fruitList)
print('List in reverse')
print(fruitList.reverseList())
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.