Given main.py and a Node class in Node.py, complete the LinkedList class (a linked list of nodes) in LinkedList.py by writing the insert_in_ascending_order() method that inserts a new Node into the LinkedList in ascending order.

Click the orange triangle next to "Current file:" at the top of the editing window to view or edit the other files.

Note: Do not edit any existing code in the files. Type your code in the TODO sections of the files only. Modifying any existing code may result in failing the auto-graded tests.

Important Coding Guidelines:

Use comments, and whitespaces around operators and assignments.
Use line breaks and indent your code.
Use naming conventions for variables, functions, methods, and more. This makes it easier to understand the code.
Write simple code and do not over complicate the logic. Code exhibits simplicity when it’s well organized, logically minimal, and easily readable.
Ex: If the input is:

8 3 6 2 5 9 4 1 7
the output is:

1 2 3 4 5 6 7 8 9
CODE: from Node import Node
from LinkedList import LinkedList

if __name__ == "__main__":
int_list = LinkedList()

user_input = input()

# Convert the string tokens into integers and insert into intList
tokens = user_input.split()
for token in tokens:
num = int(token)
new_node = Node(num)
int_list.insert_in_ascending_order(new_node)

int_list.print_list()

Answers

Answer 1

Answer:

class LinkedList:

   def __init__(self):

       self.head = None

   def insert_in_ascending_order(self, new_node):

       """Inserts a new node into the linked list in ascending order.

       Args:

           new_node: The node to insert.

       Returns:

           None.

       """

       # If the linked list is empty, set the new node as the head.

       if self.head is None:

           self.head = new_node

           return

       # Find the insertion point.

       current = self.head

       while current.next is not None and current.next.data < new_node.data:

           current = current.next

       # Insert the new node.

       new_node.next = current.next

       current.next = new_node

   def print_list(self):

       """Prints the linked list in order.

       Returns:

           None.

       """

       current = self.head

       while current is not None:

           print(current.data)

           current = current.next


Related Questions

Other Questions
which of the following classifications is not used to categorize cash inflows and cash outflows on the statement of cash flows?multiple choice question.investingfinancingoperatingspending Which of the following is a possible response by a customer who is faced with a stockout is the most costly to the firm?a. Lose the sale and lose the customerb. Lose the salec. Lose the sale of one item, but the customer purchases another itemd. Customer waits for the item to become available The camshaft position sensor on an EI system typically provides information about the ____________________ cylinder. Deliberate indifference in a cruel and unusual punishment case considers the correctional staffO inflicting unnecessary painO ignoring court injunctionsOmaking a mistakeO being careless Online dictionaries are different from print dictionaries because they:. Which post office is open until midnight tonight 2022. Cereal boxes - A large box of corn flakes claims to contain 515 grams of cereal. Since cereal boxes must contain at least as much product as their packaging claims, the machine that fills the boxes is set to put 523 grams in each box. The machine has a known standard deviation of 3 grams and the distribution of fills is known to be normal. At random intervals throughout the day, workers sample 3 boxes and weigh the cereal in each box. If the average is less than 520 grams, the machine is shut down and adjusted. How often will the workers make a Type I error with this decision rule and the hypothesesProbability of a type I error = ... Identical twins=fraternal twins, Identical twins raised together =/= identical twins raised apart, Adoptive child= adoptive parents regarding real estate investments, risk that is associated with the type of property and its location, design, lease structure, and so on can be thought of as: in order to get more accurate ratings data, nielsen now uses to track their participants' radio listening habits. Energy from food must be transferred into bonds of ____ before it can be used by cellshe evidence which statement The NWBC found that 13% of women-owned businesses provided profit-sharing and/or stock options. What sample size could be 98% confident that the estimated (sample) proportion is within 5 percentage points of the true population proportion? What does reginald fuller believe is the current relationship between jesus and the father?. a manager who allocates $1 million to one product while reserving $250,000 for another project is using a ___ to prioritize the projects. Where in your body can you find blood vessels? choose the best answer. a(n) is a mental framework that can be maladaptive or adaptive, according to cognitive therapists. group of answer choices which intruction would the nurse provide to a client who has anxiety and depression and is prescribed venlafaxine er tablet A. For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables f, g, and h, are given and considered as 32-bit integers and stored into $s0, $s1, and $s2 registers respectively. Use a minimal number of MIPS assembly instructions.f = g + (h 5);B. For the following MIPS assembly instructions, if each of the variables f, g, h and i can be considered as a 32-bit integer, what is a corresponding C statement?add f, g, hadd f, i, f When a complaint is lodged, or when inappropriate activity is brought to the attention of management ___ When x-rays of wavelength of 0. 129 nm are incident on the surface of a crystal having a structure similar to that of nacl, a first-order maximum is observed at 8. 15. Calculate the interplanar spacing based on this information.