Question

In: Computer Science

Python Add your own implementation of the function rearrange_list rearrange_list(linkedlist, number) returns the result of rearranging...

Python Add your own implementation of the function rearrange_list

rearrange_list(linkedlist, number) returns the result of rearranging the nodes in linkedlist so that:

  • all the nodes whose values are less than number are at the front
  • all the nodes whose values are greater than number are at the rear
  • any nodes whose values are equal to number are in the middle


For example, given the linked list represented by

linkedlist = -2 -> -3 -> -1 -> 2 -> 3 -> 0 -> 4 -> 1 -> 5

then

rearrange_list(linkedlist, 2) == -2 -> -3 -> -1 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5

You can see that all the numbers less than 2 now come before it, and all the numbers greater than 2 come after it. You can also see that the numbers maintain their relative position to each other, i.e. -2 and -3 and -1 don't change position relative to each other. This is an important characteristic, known as "stability". It also means that instead of requiring a full sort, you should be able to implement this algorithm in O(N) time.

You are given a simple Node class. Add your own implementation of the function rearrange_list (defined at the top level, not as part of the Node class)

Solutions

Expert Solution

ANSWER:

class Node:

def __init__(self, data):

self.data = data

self.next = None

class LinkedList:

def __init__(self):

self.head = None

def reverse(self):

prev = None

current = self.head

while(current is not None):

next = current.next

current.next = prev

prev = current

current = next

self.head = prev

def push(self, new_data):

new_node = Node(new_data)

new_node.next = self.head

self.head = new_node

def printList(self):

temp = self.head

while(temp):

if(temp.next!=None):

print(temp.data,end="->")

else:

print(temp.data,end="")

temp = temp.next

def populateList(self, number):

frontList=LinkedList()

rearList=LinkedList()

finalList=LinkedList()

found=False

temp = self.head

while(temp):

if temp.data==number :

found=True

else :

if temp.data<number :

frontList.push(temp.data)

else :

rearList.push(temp.data)

temp = temp.next

frontList.reverse()

rearList.reverse()

frontList.printList()

if found==True :

print("->"+str(number)+"->",end="")

rearList.printList()

num=input("Enter number of elements: ")

llist = LinkedList()

for x in range(int(num)):

val=input("Enter value: ")

llist.push(int(val))

llist.reverse()

print("\nGiven Linked List: ")

llist.printList()

print("\n\nEnter number to compare: ")

number=int(input())

print("\nFinal Result: ")

llist.populateList(number)

#SAMPLE OUTPUT

If you do not get anything in this solution,please put a comment and i will help you out.

Do not give a downvote instantly.It is a humble request.

If you like my answer,please give an upvote....Thank you.


Related Solutions

Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // slist.cpp #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy...
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy node”) that connects the end of the list with the beginning, transforming the list to a circular list Code in c++ The Josephus problem is named after the historian Flavius Josephus, who lived between the years 37 and 100 CE. Josephus was a reluctant leader of the Jewish revolt against the Roman Empire. When it appeared that Josephus and his band were to be...
Write python 3 code to define a function that uses three arguments, and returns a result....
Write python 3 code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False. Write a python 3 code for function main, that does the following: creates a variable and assign it the value True. uses a while loop which runs as long as the variable of the...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of...
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of edit operations needed to transform the start word into the goal word. There are three kinds of edit operations: Add a letter to start, Remove a letter from start, Substitute a letter in start for another. Each edit operation contributes 1 to the difference between two words. >>> big_limit = 10 >>> edit_diff("roses", "arose", big_limit) # roses -> aroses -> arose 2 >>> edit_diff("tesng",...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
Add the result envidented by the python compiler. use python language. 1(a) A Give the output...
Add the result envidented by the python compiler. use python language. 1(a) A Give the output for the array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) a. a[:6:2] = -1000 (ii) a[ : :-1] ) b. Display the values of 1D array using for loop?
 c. Write a code to print a 3D array?
 d. How to print the transpose of a 2D array?
 e Write a function to sort the array row and coloumn wise ?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT