Question

""" Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python...

"""
Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python class. Name of list is LList
class node(object):

    def __init__(self, data, next=None):

        self.data = data
        self.next = next

    # Note: use the attributes directly; no setters or getters!

 
class LList(object):
    def __init__(self):

        self._size = 0     
        self._head = None  
        self._tail = None
"""
def set_data_at_index(self, idx, val):
    """
        The value stored at index idx changes to val
        return True if the index was valid otherwise return False
    """



def insert_value_at_index(self, val, idx):
    """
        The list increases in size.
        The new value is at index idx.
        The values previously in the list at idx or later appear after the new value.
        return If the index is valid, insert_value_at_index returns True.
        return If the index is not valid, insert_value_at_index returns False.
    """


def delete_item_at_index(self, idx):
    """
    
        The list decreases in size if the index is valid
        The value at idx is no longer in the list.
        return True if index was valid otherwise return False
    """

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

class node(object):
    def __init__(self, data, next=None):
        self.data = data
        self.next = next
    # Note: use the attributes directly; no setters or getters!
class LList(object):
    def __init__(self):
        self._size = 0 
        self._head = None 
        self._tail = None 
    def set_data_at_index(self, idx, val):
    """
        The value stored at index idx changes to val
        return True if the index was valid otherwise return False
    """
        if not 0<=id<self._size:
            return False
        itr = self._head
        for i in range(idx):
            itr = itr.next
        itr.data = val
        return True
    def insert_value_at_index(self, val, idx):
    """
        The list increases in size.
        The new value is at index idx.
        The values previously in the list at idx or later appear after the new value.
        return If the index is valid, insert_value_at_index returns True.
        return If the index is not valid, insert_value_at_index returns False.
    """
        if not 0<=idx<self._size:
            return False
        if idx==0:
            tmp = self._head
            self._head = node(val, tmp)
            self._size += 1
            return True
        itr = self._head
        for i in range(idx-1):
            itr = itr.next
        tmp = node(val, itr.next)
        itr.next = tmp
        self._size += 1
        return True
    def delete_item_at_index(self, idx):
    """
    
        The list decreases in size if the index is valid
        The value at idx is no longer in the list.
        return True if index was valid otherwise return False
    """
        if not 0<=idx<self._size:
            return False
        if idx==0:
            tmp = self._head
            self._head = tmp.next
            del tmp
            self._size -= 1
            return True
        itr = self._head
        for i in range(idx-1):
            itr = itr.next
        tmp = itr.next
        itr.next = tmp.next
        del tmp
        if idx == self._size - 1:
            self._last = itr
        self._size -= 1
        return True

Code Screenshot:

2 3 4 5 6 Eclass node (object): def init (self, data, next=None): self. data = data self.next = next # Note: use the attribut

def delete_item_at_index (self, idx): The list decreases in size if the index is valid The value at idx is no longer in the l

Since you have asked only for the code, written it.

Thank you! Hit like if you like my work.

Add a comment
Know the answer?
Add Answer to:
""" Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Finish each function python 3 LList.py class node(object): """ A version of the Node class with...

    Finish each function python 3 LList.py class node(object): """ A version of the Node class with public attributes. This makes the use of node objects a bit more convenient for implementing LList class.    Since there are no setters and getters, we use the attributes directly.    This is safe because the node class is defined in this module. No one else will use this version of the class. ''' def __init__(self, data, next=None): """ Create a new node for...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object):    def...

    class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object):    def __init__(self): self.head = None self.tail = None Implement the following functions for Linked List in Python and use the constructors above : Copy(lList) Builds and returns a copy of list ItemAt(List,i) Returns the data item at position i in list Pop(List,i=0) Remove item at position i in list. If i is not specified, it removes the first item in list Count(List,x) Returns the number...

  • i need to create methods that: append_element(self, val) This method should increase the size of the...

    i need to create methods that: append_element(self, val) This method should increase the size of the list by one, adding the specified value in the new tail position. This is the only way to add a value as the tail. insert_element_at(self, val, index) If the provided index identifies a valid zero-based position within the list, then insert the specified value at that position, increasing the length by one. This method can be used to insert at the head of a...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display,...

    Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...

  • PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None   

    PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None        self.__size = 0    # Return the head element in the list    def getFirst(self):        if self.__size == 0:            return None        else:            return self.__head.element        # Return the last element in the list    def getLast(self):        if self.__size == 0:            return None        else:            return self.__tail.element    # Add an element to the beginning of the list    def addFirst(self, e):        newNode = Node(e) # Create a new node        newNode.next = self.__head # link...

  • In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game...

    In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game Hangman. In this game, a word is first randomly chosen. Initially, the letters in the word are displayed represented by "_”.   For example, if the random word is "yellow”, the game initially displays "_ _ _ _ _ _”. Then, each turn, the player guesses a single letter that has yet to be guessed. If the letter is in the secret word, then the...

  • Please code in Python Revise the AbstractBag class so that it behaves as a subclass of...

    Please code in Python Revise the AbstractBag class so that it behaves as a subclass of AbstractCollection provided in the file abstractcollection.py. Abstractcollection.py code: class AbstractCollection(object):     """An abstract collection implementation."""     # Constructor     def __init__(self, sourceCollection = None):         """Sets the initial state of self, which includes the         contents of sourceCollection, if it's present."""         self.size = 0         if sourceCollection:             for item in sourceCollection:                 self.add(item)     # Accessor methods     def isEmpty(self):         """Returns True if len(self) == 0, or False otherwise."""         return len(self) == 0...

  • PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree,...

    PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree, the BinaryTreeclass, he wrote test code and is having problems understanding the error. Locate his problem and explain how you would fix it. class Node(object): def __init__(self, data=None): self.data = data def __str__(self): return "NODE: " + str(self.data)    class Tree(object): def __init__(self): self.root_node = None self.size = 0    def __len__(self): return self.size    def add(self, data): raise NotImplementedError("Add method not implemented.")    def inorder_traversal(self): raise NotImplementedError("inorder_traversal...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT