Python
I'm creating two linked lists from two txt files, merging them, then printing any duplicates found. My program right now only reads numbers one after another following a comma. For example 10,20,15,2. How do i change it so it reads numbers one below another. Like this,

class Node(object):
value = -1
next = None
def __init__(self,item):
self.val = item
self.next = None
def merge_lists(vivendi, activision):
head = tail = Node(0)
while vivendi and activision:
if vivendi.val <
activision.val:
tail.next = vivendi
vivendi = vivendi.next
else:
tail.next = activision
activision = activision.next
tail = tail.next
tail.next = vivendi or activision
return head.next
def printRepeating(mergeLists,size):
print("Repeating ID's are ", end = '')
for i in range(0, size):
for j in range(i+1,
size):
if mergeLists[i]==mergeLists[j]:
print(mergeLists[i],"", end = '')
if __name__ =='__main__':
vivendi = []
with open('vivendi.txt', 'r') as myfile:
for line in
myfile:
vivendi.extend(map(int, line.split( ',')))
#print(vivendi)
head1=tall=None;
for i in vivendi:
if(head1==None):
head1=tall=Node(i)
else:
tall.next=Node(i)
tall=tall.next
activision = []
with open('activision.txt', 'r') as
myfile:
for line in
myfile:
activision.extend(map(int, line.split(',')))
#print(activision)
head2=tall2=None;
for i in activision:
if(head2==None):
head2=tall2=Node(i)
else:
tall2.next=Node(i)
tall2=tall2.next
new_head=merge_lists(head1,head2)
mergeLists = []
count = 0
while new_head:
mergeLists.append(new_head.val)
new_head=new_head.next
count=count+1
printRepeating(mergeLists,count)
You just need to modify one line like i did. Let me know if you have any doubt.
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 12 00:54:37 2018
@author:
"""
class Node(object):
value = -1
next = None
def __init__(self,item):
self.val = item
self.next = None
def merge_lists(vivendi, activision):
head = tail = Node(0)
while vivendi and activision:
if vivendi.val < activision.val:
tail.next = vivendi
vivendi = vivendi.next
else:
tail.next = activision
activision = activision.next
tail = tail.next
tail.next = vivendi or activision
return head.next
def printRepeating(mergeLists,size):
print("Repeating ID's are ", end = '')
for i in range(0, size):
for j in range(i+1, size):
if mergeLists[i]==mergeLists[j]:
print(mergeLists[i],"", end = '')
if __name__ =='__main__':
vivendi = []
with open('vivendi.txt', 'r') as myfile:
for line in myfile:
#first remove next line character then convert it in int.
vivendi.append(int(line.strip()))
#vivendi.extend(map(int, line.split(',')))
#print(vivendi)
head1=tall=None;
for i in vivendi:
if(head1==None):
head1=tall=Node(i)
else:
tall.next=Node(i)
tall=tall.next
activision = []
with open('activision.txt', 'r') as myfile:
for line in myfile:
#first remove next line character then convert it in int.
activision.append(int(line.strip()))
#activision.extend(map(int, line.split(',')))
#print(activision)
head2=tall2=None;
for i in activision:
if(head2==None):
head2=tall2=Node(i)
else:
tall2.next=Node(i)
tall2=tall2.next
new_head=merge_lists(head1,head2)
mergeLists = []
count = 0
while new_head:
mergeLists.append(new_head.val)
new_head=new_head.next
count=count+1
printRepeating(mergeLists,count)
Python I'm creating two linked lists from two txt files, merging them, then printing any duplicates...
Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...
PYTHON
--------------------------------------------------------
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size ==
0:
return
None
else:
return
self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size ==
0:
return
None
else:
return
self.__tail.element
# Add an element to the beginning of the
list
def addFirst(self, e):
newNode = Node(e) #
Create a new node
newNode.next =
self.__head # link...