linkedlist_sort

py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Apr 3, 2024

Type

py

Pages

2

Uploaded by MateClover22302

Report
""" File: linked_list.py Author: Nick Brobeck Course: CSC 120, Spring 2024 Purpose: This program involves writing a method for the LinkedList class that sorts a linked list in descending order of the _value attibutes of the nodes. """ class LinkedList: def __init__(self): self._head = None # sort the nodes in the list def sort(self): # If the list is empty or has only one node, it \ # is already sorted, so return. if self._head is None or self._head.next() is None: return # Initialize a new linked list to store the sorted nodes. sorted_list = None # Start with the current node being the head of the original \ # linked list. current = self._head # Traverse through the original linked list. while current is not None: # Store the next node in the original \ # list before modifying current. next_node = current.next() # If the sorted list is empty or the current \ # node's value is greater than \ # the head of the sorted list, \ # insert the current node at the beginning of the sorted list. if sorted_list is None or current.value()\ > sorted_list.value(): current._next = sorted_list sorted_list = current else: # Otherwise, find the correct position to insert the current \ # node in the sorted list. temp = sorted_list while temp.next() is not None and temp.next().value()\ > current.value(): temp = temp.next() # Insert the current node between temp and temp.next(). current._next = temp.next() temp._next = current # Move to the next node in the original linked list. current = next_node # Update the head of the original linked list to point to the \ # head of the sorted list. self._head = sorted_list # add a node to the head of the list def add(self, node):
node._next = self._head self._head = node # remove a node from the head of the list and return the node def remove(self): assert self._head != None _node = self._head self._head = _node._next _node._next = None return _node # insert node2 after node1 def insert(self, node1, node2): assert node1 != None node2._next = node1._next node1._next = node2 def __str__(self): string = 'List[ ' curr_node = self._head while curr_node != None: string += str(curr_node) curr_node = curr_node.next() string += ']' return string class Node: def __init__(self, value): self._value = value self._next = None def __str__(self): return str(self._value) + "; " def value(self): return self._value def next(self): return self._next def main(): file_name = input().strip() file = open(file_name, 'r') numbers = file.readline().strip().split() file.close() linked_list = LinkedList() for num in numbers: node = Node(int(num)) linked_list.add(node) linked_list.sort() print(linked_list) main()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help