Question

In: Computer Science

a python function that reads two text files and merges in to one Linked List, be...

a python function that reads two text files and merges in to one Linked List, be able to print each Item in the new single Linked List

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

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

================================

textfile!

979
2744
5409
1364
4948
4994
5089
703
1994
4637
2228
4004
1088
2812
170
5179
2614
238
4523
4849
3592
3258
1951
3440
3977
1247
4076
1824
4759
4855
5430
347
974
5180
5159
3074
4462
3600
5824
1490
1941
4744
855
4940
1442
792
609
254
2203
2343
2550
1955
5513
4725
2935
4024
4979
5765
625
2888
924
5989
4119
1572
176
2773
3800
200
3389
4245
3849
3170
4773
2344
1413
292
2603
4694
5579
4012
5584
5352
3293
5516
1008
5395
545
3340
4967
2050
870
2462
2606
4850
4731
450
4052
1279
2506
3937
396
222
3928
2557
3070
444
2599
3462
2418
1180
3290
2046
493
2708
3650
973
57
1846
3300
265
3572
3543
4439
1807

========

textfile2

4964
157
2374
3617
2833
2413
388
3642
3468
3331
2537
1830
4923
1718

Solutions

Expert Solution

class Node(object):
data = -1
next_node = None

def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node

def get_data(self):
return self.data

def get_next(self):
return self.next_node

def set_next(self, new_next):
self.next_node = new_next

#define a different class for linkedlist
class LinkedList(object):
def __init__(self, head=None):
self.head = head
  
#function to insert
def insert(self, data):
new_node = Node(data)
new_node.set_next(self.head)
self.head = new_node
  
#function to print the elements of the linked list
def print_nodes(self):
current=self.head
while current:
print(current.get_data())
current=current.get_next()

file1=open("textfile!.txt","r")
file2=open("textfile2.txt","r")

ll=LinkedList()

for i in file1:
ll.insert(i)

for i in file2:
ll.insert(i)

file1.close()
file2.close()

ll.print_nodes()
# prints data in reverse fashion

sample output:

1718
4923

1830

2537

3331

3468

3642

388

2413

2833

3617

2374

157

4964

1807
4439

3543

3572

265

3300

1846

57

973

3650

2708

493

2046

3290

1180

2418

3462

2599

444

3070

2557

3928

222

396

3937

2506

1279

4052

450

4731

4850

2606

2462

870

2050

4967

3340

545

5395

1008

5516

3293

5352

5584

4012

5579

4694

2603

292

1413

2344

4773

3170

3849

4245

3389

200

3800

2773

176

1572

4119

5989

924

2888

625

5765

4979

4024

2935

4725

5513

1955

2550

2343

2203

254

609

792

1442

4940

855

4744

1941

1490

5824

3600

4462

3074

5159

5180

974

347

5430

4855

4759

1824

4076

1247

3977

3440

1951

3258

3592

4849

4523

238

2614

5179

170

2812

1088

4004

2228

4637

1994

703

5089

4994

4948

1364

5409

2744

979


Related Solutions

Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Edit question Write a program that merges two files as follows. The two files are in...
Edit question Write a program that merges two files as follows. The two files are in the docsharing which you can download it. One file will contain usernames(usernames.txt):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
Write a python program function to check the frequency of the words in text files. Make...
Write a python program function to check the frequency of the words in text files. Make sure to remove any punctuation and convert all words to lower case. If my text file is like this: Hello, This is Python Program? thAt chEcks% THE freQuency of the words! When is printed it should look like this: hello 1 this 1 is 1 python 1 program 1 that 1 checks 1 the 2 frequency 1 of 1 words 1
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__':
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT