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

Create your own function in C that accepts one input number and returns a double number....
Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following: Divides the number by 3 and returns the result. For example, if 6 was input then 2.0 should be returned. provide both your C code and an example call to the C code function. Be sure to provide an overview of what your function is doing. Include header documentation in the code as...
Create your own function in C that accepts one input number and returns a double number....
Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following: Calculates 3.14 times of the square of input number. For example, if 2 is input then 12.56 should be returned. (e.g. 3.14*2*2 = 12.56)
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...
Create your own function in Java that accepts one input parameter and returns a float number....
Create your own function in Java that accepts one input parameter and returns a float number. You decide the theme.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT