insert_at_index(self, index: int, value: object) -> None: This method inserts a new value at the specified index position in the linked list. Index 0 refers to the beginning of the list (right after the front sentinel). If the provided index is invalid, the method raises a custom "SLLException". Code for the exception is provided in the skeleton file. If the linked list contains N nodes (the sentinel node is not included in this count), valid indices for this method are [0, N] inclusive. Example #1: LinkedList () 1st test cases [(0, "A"), (0, "B"), (1, "c"), (3, "D"), (-1, "E"), (5, "F")) for index, value in test cases: print ("Inserted", value, "at index", index, ": ", end-"") try: 1st.insert_at_index (index, value) print (1st) except Exception as e: print (type (e)) Output: Inserted A at index 0: SLL (A) Inserted B at index 0: SLL (B -> A) SLL (B-> C -> A) Inserted C at index 1: Inserted D at index 3: SLL (B -> C -> A -> D) Inserted E at index -1 SLLException'>

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

class SLNode:
    """
    Singly Linked List Node class
    DO NOT CHANGE THIS CLASS IN ANY WAY
    """
    def __init__(self, value: object, next=None) -> None:
        self.value = value
        self.next = next

 

 

 

from SLNode import *


class SLLException(Exception):
    """
    Custom exception class to be used by Singly Linked List
    DO NOT CHANGE THIS CLASS IN ANY WAY
    """
    pass


class LinkedList:
    def __init__(self, start_list=None) -> None:
        """
        Initialize new linked list
        DO NOT CHANGE THIS METHOD IN ANY WAY
        """
        self._head = SLNode(None)

        # populate SLL with initial values (if provided)
        # before using this feature, implement insert_back() method
        if start_list is not None:
            for value in start_list:
                self.insert_back(value)

    def __str__(self) -> str:
        """
        Return content of singly linked list in human-readable form
        DO NOT CHANGE THIS METHOD IN ANY WAY
        """
        out = 'SLL ['
        node = self._head.next
        while node:
            out += str(node.value)
            if node.next:
                out += ' -> '
            node = node.next
        out += ']'
        return out

    def length(self) -> int:
        """
        Return the length of the linked list
        DO NOT CHANGE THIS METHOD IN ANY WAY
        """
        length = 0
        node = self._head.next
        while node:
            length += 1
            node = node.next
        return length

    def is_empty(self) -> bool:
        """
        Return True is list is empty, False otherwise
        DO NOT CHANGE THIS METHOD IN ANY WAY
        """
        return not self._head.next

 

 

 

def insert_at_index(self, index: int, value: object) -> None:
    """
     this implementation
    """
    pass

insert_at_index(self, index: int, value: object) -> None:
This method inserts a new value at the specified index position in the linked list. Index 0
refers to the beginning of the list (right after the front sentinel).
If the provided index is invalid, the method raises a custom "SLLException". Code for the
exception is provided in the skeleton file. If the linked list contains N nodes (the sentinel
node is not included in this count), valid indices for this method are [0, N] inclusive.
Example #1:
1st LinkedList ()
test cases
[(0, "A"), (0, "B"), (1, "C"), (3, "D"), (-1, "E"), (5, "F")]
for index, value in test_cases:
print("Inserted", value, "at index", index, ": ", end-"")
try:
1st.insert_at_index (index, value)
print (1st)
except Exception as e:
print (type (e))
Output:
Inserted A at index 0: SLL [A]
Inserted B at index
Inserted C at
index 1:
SLL [B -> C -> A)
Inserted D at index 3: SLL [B -> C -> A -> D]
Inserted E at index -1 : <class ' main
Inserted F at index 5: <class main
0: SLL [B -> A)
SLLException'>
.SLLException'>
Transcribed Image Text:insert_at_index(self, index: int, value: object) -> None: This method inserts a new value at the specified index position in the linked list. Index 0 refers to the beginning of the list (right after the front sentinel). If the provided index is invalid, the method raises a custom "SLLException". Code for the exception is provided in the skeleton file. If the linked list contains N nodes (the sentinel node is not included in this count), valid indices for this method are [0, N] inclusive. Example #1: 1st LinkedList () test cases [(0, "A"), (0, "B"), (1, "C"), (3, "D"), (-1, "E"), (5, "F")] for index, value in test_cases: print("Inserted", value, "at index", index, ": ", end-"") try: 1st.insert_at_index (index, value) print (1st) except Exception as e: print (type (e)) Output: Inserted A at index 0: SLL [A] Inserted B at index Inserted C at index 1: SLL [B -> C -> A) Inserted D at index 3: SLL [B -> C -> A -> D] Inserted E at index -1 : <class ' main Inserted F at index 5: <class main 0: SLL [B -> A) SLLException'> .SLLException'>
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Please show the output

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Map
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education