Question: Python - Create a single linked list from two text files and using the duplicates_unsorted method to print any duplicates found in the linked list.

Python - Create a single linked list from two text files and using the duplicates_unsorted method to print any duplicates found in the linked list. What i have done is create a method called single_list that reads both text files and merges them, but i need the list to be a linked list. I tried to do a method that creates a linked list but it dosent work. How else can i do this?

Python - Create a single linked list from two text files and

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

def __init__ (self, item, next = None): self.item = item self.next = next

class LinkedList (object): def __init__(self): self.head = None def append(self, item): item = self while item.next != None: item = item.next item.next(new_node) def size(self): current = self.head count = 0 if current != None and current.next == None: return 1 while current != None: current = current.next count +=1 return count

def single_list(): with open('vivendi.txt', 'r') as myfile: #calls on vivendi.txt file vivendi = [] for line in myfile: vivendi.append(int(line.strip())) #print(vivendi)

with open('activision.txt', 'r') as myfile: #calls on activision.txt file activision = [] for line in myfile: activision.append(int(line)) #print(activision) new_list = activision + vivendi return new_list def create_linkedlist(n): assert len(1st) > 0 if len(1st) == 1: return LinkedList(1st[0]) else: return LinkedList(1st[0], create_linkedlist(1st[1:]))

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)

if __name__ =='__main__': users = single_list() user_list = create_linkedlist(users) user_list.print() duplicates_unsorted(user_list)

vivendi.txt-... activision.txt...- File Edit Format File Edit Format View Help 586 481 12 25 85 1000 32 65 123 784 4 78 39 698 10

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!