Question

In: Computer Science

Python 3 Function which takes the head Node of a linked list and sorts the list...

Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. '''

def sort(head_node):

#Code goes here

''' Test code goes here ''

' if __name__ == '__main__':

Solutions

Expert Solution

'''

Python version : 3.6

Python program to sort a linked list

'''

# class representing the node of the linked list

class Node:

               # initializes a node

               def __init__(self, value):

                              self.value = value

                              self.next = None

              

               # function to set the value of the node    

               def setValue(self,value):

                              self.value = value

              

               # function to set the next pointer of the node       

               def setNext(self,next):

                              self.next = next

              

               # function to return the value of the node

               def getValue(self):

                              return self.value

              

               # function to return the pointer of next node

               def getNext(self):

                              return self.next

              

               # function to return the string representation of the node

               def __str__(self):

                              return('%s' %(str(self.value)))

                             

# function to sort a linked list that starts with the head_node                        

def sort(head_node):

              

               swapped = True

               # loop that continues till the list is sorted

               while swapped:

                              swapped = False

                             

                              curr = head_node

                              # loop that continues till the end of the list

                              while curr.getNext() != None:

                                             # if value at curr node is greater than value at node next to curr then swap the values

                                             if curr.getValue() > curr.getNext().getValue():

                                                            temp = curr.getValue()

                                                            curr.setValue(curr.getNext().getValue())

                                                            curr.getNext().setValue(temp)

                                                            swapped = True # set swapped to True

                                             curr = curr.getNext()

               return head_node # return the head_node of the sorted linked list

# function to insert the value at the end of the linked list  

def insert(head_node, value):

               node = Node(value)

               if head_node == None:

                              head_node = node

               else:

                              temp = head_node

                              while temp.getNext() != None:

                                             temp= temp.getNext()

                                                           

                              temp.setNext(node)       

               return head_node

# function to display the linked list            

def display(head_node):

               node = head_node

                             

               if node == None:

                              print('Empty list')

               else:

                              while(node != None):

                                             print(str(node),end=' ')

                                             node = node.getNext()

                              print('')                

# Test the functions                       

if __name__ == "__main__":

              

               head_node = None

              

               head_node = insert(head_node,23)

               head_node = insert(head_node,12)

               head_node = insert(head_node,20)

               head_node = insert(head_node,15)

               head_node = insert(head_node,19)

              

               display(head_node)

               head_node = sort(head_node)

               display(head_node)

              

#end of program             

Code Screenshot:

Output:


Related Solutions

In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You're given the pointer to the head node of a ordered linked list, an integer to...
You're given the pointer to the head node of a ordered linked list, an integer to add to the list. Write a function that inserts a number in the the list preserving its order. If the head pointer contains a null pointer that indicates an empty list. Function insertNode has the following parameters: head: a SinglyLinkedListNode pointer to the head of the list data: an integer value to insert as data in your new node Function prototype: SinglyLinkedListNode* insertNode(SinglyLinkedListNode* head,...
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
1- Function 1: to delete a node in the head of the list. 2- Function 2:...
1- Function 1: to delete a node in the head of the list. 2- Function 2: to delete a node in the end of the list. 3- Function 3: to delete a node in the middle of the list. Ask the user the value of the node to delete. 4- Test the three functions in the main() and display the new list after each delete. #include <iostream> using namespace std; struct node { int num; node * nextptr; }*head,*curnode; node...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT