Question

In: Computer Science

Using python. Produce a method for a linked list that is called FIND , which returns...

Using python.

Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list

Solutions

Expert Solution

Init method in a linked list:

{

class LinkedList(object):

def __init__(self, head=None):

self.head = head

}

Init method is used for the initialization of a class variable

Insert method:

{

}

def insert(self, data):

new_node = Node(data)

new_node.set_next(self.head)

self.head = new_node

This insert method takes data, initializes a new node with the given data, and adds it to the list.

search:

1

2

3

4

5

6

7

8

9

10

11

12

# Returns the node in list having nodeData, error occured if node not present

def search(self, nodeData):

current = self.head

isPresent = False

while current and isPresent is False:

if current.get_data() == nodeData:

isPresent = True

else:

current = current.get_next()

if current is None:

raise ValueError("Data not present in list")

return current

Let us produce a method for a linked list called FIND, which returns the index of a lookup value with in the linked list.

PROGRAM: class Node: # linked node def __init__(self, data=None): self.data = data self.next = None class linked_list: def __init__(self): # Createe an empty list self.tail = None self.head = None self.count = 0 def append_item(self, data): #Append items on the list node = Node(data) if self.head: self.head.next = node self.head = node else: self.tail = node self.head = node self.count += 1 def __finditem__(self, index): if index > self.count - 1: return "Index out of range" current_val = self.tail for n in range(index): current_val = current_val.next return current_val.data items =linked_list() items.append_item('PHP') items.append_item('Python') items.append_item('C#') items.append_item('C++') items.append_item('Java') print("Find items using index:") print(items[0]) print(items[1]) print(items[4]) print(items[5]) print(items[10])

sample output:

find items using index:

PHP Python Java Index out of range Index out of range

Related Solutions

Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question 2: Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list, return -1. Please use C++ language for both questions, I only need functions.
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
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__':
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT