Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extend the LList class below by implementing some of the methods that the built-in Python list supports: __min__,__max__,index, count and remove. class LList(): def __init__(self,

Extend the LList class below by implementing some of the methods that the built-in Python list supports: __min__,__max__,index, count and remove.

class LList(): def __init__(self, seq=()): """ seq should be a sequence of data. Can be empty. post: self will be a linked list comprised of all the elements in seq in order, each stored as the data in a ListNode object """ # initialize self.head # initialize self.size if len(seq) > 0: self.head = ListNode(seq[0]) self.size = 1 else: self.head = None self.size = 0 return if len(seq) == 1: return previous = self.head # prev = I for item in seq[1:]: current = ListNode(item) # current = J self.size += 1 previous.set_link(current) # set link for I previous = current # prep for next iteration

def __len__(self): """ Returns number of elements in self """ return self.size def insert(self, index, item): """ Inserts a new node containing item into self between nodes currently stored at self[index-1] and self[index] """ # Create new node for item inserted = ListNode(item) self.size += 1 # Find node BEFORE where inserted, call that node previous previous = self._find(index-1) # Connect inserted node to what comes after it inserted.set_link(previous.get_link()) # Connect what comes before inserted node to inserted node previous.set_link(inserted) def _find(self, index): """ returns node stored at self[index] assumes index is a valid index!""" # Questions: How do we deal with index <= 0 or index >= self.size? # Start at head current = self.head # Follow index number of links to get to the right node for l in range(index): current = current.get_link() # At end of loop, current is pointing at self[index] return current def __getitem__(self, index): """ Overloads the indexing operator so self[i] works Returns data stored in node at self[index] """ return self._find(index).get_item() def __setitem__(self, index, item): """ Overloads the indexing operator so item = self[index] works """ node = self._find(index) node.set_item(item)

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

Can a public key cipher be used as a symmetric key cipher? Explain

Answered: 1 week ago