Question: In this one you'll need to: (Python code) Read a transaction file of U.S. presidents Separate the input lines into a transaction part and name
In this one you'll need to: (Python code)
Read a transaction file of U.S. presidents
Separate the input lines into a transaction part and name part
Create a Node class to be used to build a Linked List of presidents
Build a partial list one node at a time
Traverse the list using a 'while' loop
Build a complete list using a 'for' loop that includes all names
file.txt data is:
A,Washington A,Adams A,Jefferson A,Madison A,Monroe A,Quincy Adams P,Jackson A,Van Buren
# 1. Read the following input file provided with the assignment file = open("file.txt", "r") print(file.read())
# 2. Build a transaction file as a list of lists, with the # transaction code as the first sublit element, the name the second. # Use the string 'split' method to separate those. # # Code to build the transaction list of lists goes here -------- trans = [ ] # start by creating a blank list # trans should look like this: # [['A', Washington'], ['A', 'Adams'], ...]
# 3. Code to create the Node class goes here. # - used to build node objects # Attributes include a name (of a president) and "pointer" to # the next node in the list # # Code goes here ----------------------------------------------
class Node: # class definition line provided
# provide rest of code (hint: the constructor that includes # the 'element' or name as a parameter and 'next' to point # to the next link - set it to 'None' in the constructor). # You may directly access the node atributes using dot-notation. # getter/setter methods
# 4. Build linked list using the first four presidents - # - Washington through Madison # Ignore the transaction code, just use the presidents' names.
# You'll need to create a pointer to the head of the list ("head"). # Create the first node object, name = 'Washington', point 'head' # to this node, make the Washington node's next point "None". # # After you create the Washington Node object "n", print: # # 1. it's name attribute, n.name # 2. it's id, i.e. id(n) # 3. it's next pointer, id(n.next)
# Code goes here to create the list head and point to 'Washington': #----------------------------------------------------------------- head = Node(trans[0][1]) # Add first presidents name as head head.next = None # set next pointer, None = end of list
print(id(head), id(last)) # Print head and last to verify success
# Next add nodes for Adams, Jefferson, and Madison
# Create a Node object for Adams, assign the name "Adams" # to that Node object's name attribute, assign Washington's # 'next' attribute to point to this new node, assign this new # node's next attribute to be "None". # # Print the id, and name and next attributes for this new Node object # # Repeat the above process for Jefferson and Madison
# Code goes here:
# 5. Create a 'while' loop to travserse the linked list and # print the name, id, and next and pointers # # Start the traversal with the 'head' node, then set the next # node to the 'next' pointer inside the while loop...continue # until the node you're looking at is "None". # # Code goes here ------------------------------------------------
print('done with the search') # Search is done when this is printed
# 6. Build the a complete list using all the presidents in the # file, which you've read into the 'trans' list # - use a for loop to step through the list originally # read and built in 1 and 2, above (Washington through Van Buren) # # Code goes here -------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
