Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python - How can i call the linked list through my other methods in main, i get the error 'Linkedlist' object has no attribute 'method'

Python - How can i call the linked list through my other methods in main, i get the error 'Linkedlist' object has no attribute 'method' ? I am calling two text files with numbers into one on method single_list and then creating the linked list from that.

class Node(object): item = -1 next = None

def __init__(self, item, next=None): self.item = item self.next = next #required methods to make the list work def has_next(self): return self.next!=None def get_next(self): return self.next def set_next(self,node): self.next=node

class LinkedList(object): # required constructor def __init__(self): #initializing root and size self.root=None self.size=0

def get_size(self): return self.size

def add(self, item): new_node = Node(item, self.root); self.root = new_node; self.size += 1;

def add_node(self, next): next.set_next(self.root); self.root = next; self.size += 1;

def find(self, item): this_node = self.root while this_node: if this_node.get_data() == item: return item else: this_node = this_node.get_next() return None

def print_list(self): print("Print List = ", end= ""); if self.root is None: return; current = self.root; print(current.item, end=","); while current.has_next(): current = current.get_next(); print(current.item, end=",");

def duplicates_unsorted(list): #This method would find duplicates in a unsorted list k = list duplicate_elements = [] while k is not None: j = k.next while j is not None: if k.item == j.item: duplicate_elements.append(k.item) j = j.next k = k.next print("Duplicates: ", duplicate_elements)

def duplicates_sorted(list): k = list duplicate_elements = [] while k is not None: if k.next is None: break if k.item == k.next.item: duplicate_elements.append(k.item) k = k.next print("Duplicates: ", duplicate_elements) def bubble_sort(list): for i in range(list.length()): m = list n = m.next while n is not None: if n.item is None: break if m.item > n.item: swap(m, n) n = n.next m = m.next

def swap(node1, node2): temp = node1.item node1.item = node2.item node2.item = temp def mergeSort(list):

if list is None or list.next is None: return list else: middle = getMiddleNode(list) nextmiddle = middle.next middle.next = None

left = mergeSort(middle) right = mergeSort(nextmiddle)

sortedList = sortMerged(left, right)

return sortedList

def sortMerged(left, right):

if left is None: return left elif right is None: return right

if (int(right.item) <= int(left.item)): result = right result.next = sortMerged(right.next, left) else: result = left result.next = sortMerged(right, left.next) return result

def getMiddleNode(list):

if list is None or list.next is None: return list else: fast = list.next slow = list

while fast != None: fast = fast.next slow = slow.next

return slow def checkBoolean(list): seen = [False]*(list.length()+1) m = list n = m.next while n is not None: if m.item == n.item: seen[m.item] = True m = m.next if m == n: n = n.next m = list return seen def single_list(): merge_list = LinkedList() #create empty linked list with open('vivendi.txt', 'r') as myfile: #calls on vivendi.txt file for line in myfile: merge_list.add(int(line.strip())) myfile.close()

with open('activision.txt', 'r') as myfile: #calls on activision.txt file for line in myfile: merge_list.add(int(line)) myfile.close return merge_list

def main(): #creating a linked list linkedList=single_list() print("Displaying numbers: ") linkedList.print_list() linkedList.duplicates_unsorted() linkedList.bubble_sort() linkedList.mergeSort() linkedList.checkBoolean()

if __name__ =='__main__': main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions

Question

=+7 How has the COVID-19 pandemic impacted the operations of IHRM?

Answered: 1 week ago