In: Computer Science
Please include comments on what you are doing.
Using linked lists, write a Python program that performs the following tasks:
If you use the Node and Linked List class definitions from the zyBook, please make sure you cite this reference in your comments.(dont use if not needed but if you need to use this I will reference them myself)
college.csv file sample
| Alabama A & M University | AL |
| University of Alabama at Birmingham | AL |
| Amridge University | AL |
| University of Alabama in Huntsville | AL |
| Alabama State University | AL |
| The University of Alabama | AL |
| Central Alabama Community College | AL |
| Athens State University | AL |
| Auburn University at Montgomery | AL |
| Auburn University | AL |
| Birmingham Southern College | AL |
| Chattahoochee Valley Community College | AL |
| Concordia College Alabama | AL |
| South University-Montgomery | AL |
| Enterprise State Community College | AL |
| Coastal Alabama Community College | AL |
| Faulkner University | AL |
| Gadsden State Community College | AL |
| New Beginning College of Cosmetology | AL |
| George C Wallace Community College-Dothan | AL |
| George C Wallace State Community College-Hanceville | AL |
| George C Wallace State Community College-Selma | AL |
| Herzing University-Birmingham | AL |
| Huntingdon College | AL |
| Heritage Christian University | AL |
| J. F. Drake State Community and Technical College | AL |
| J F Ingram State Technical College | AL |
| Jacksonville State University | AL |
| Jefferson Davis Community College | AL |
| Jefferson State Community College | AL |
| John C Calhoun State Community College | AL |
| Judson College | AL |
| Lawson State Community College-Birmingham Campus | AL |
| University of West Alabama | AL |
| Lurleen B Wallace Community College | AL |
| Marion Military Institute | AL |
| Miles College | AL |
| University of Mobile | AL |
| University of Montevallo | AL |
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.Below is the output of the program:
Below is the code to copy: #CODE STARTS HERE----------------
class Node: #Node class
def __init__(self, college,state):
self.college = college #college of the node
self.state = state #state of the node
self.next = None #Next node in the linked list
def insert(head, college, state):
new = Node(college,state) #Create a new node with given values
if head is None: #if head is none, new node becomes the head
new.next = head
return new
#Traverse the list until last node is reached
current_node = head
while current_node.next is not None:
current_node = current_node.next
#Add new nodes to the end of the list
new.next = current_node.next #Add 'next' of new node
current_node.next = new #Update 'next' of current node
return head
def search_college(head, name): #Search if a college is in the linked list or not
pointer = head #Pointer is used to hold current position while traversing
while pointer is not None: #Traverse linked list from the head
#Check if entered name is in the node's college value
if name.strip().lower() == pointer.college.strip().lower():
return True #Return true if present
pointer = pointer.next #Update pointer
return False
def search_in_state(head, state_name): #Return the number of colleges in the state
counter =0
pointer = head #Pointer is used to hold current position while traversing
while pointer is not None: #Traverse linked list from the head
#Check if state entered is equal to node's state
if state_name.strip() == pointer.state.strip():
counter+=1 #update counter value
pointer = pointer.next
return counter #return count
def main():
#Inserting values into linked list from .csv
head = None #Initialize node to None
with open("colleges.csv") as f: #open file
for line in f.readlines():#traverse .csv line by line
#call insert function with college name and state name
head = insert(head , " ".join(line.split()[0:-1]),line.split()[-1])
#Searching college name
name = input("Please enter a college name to search: ")#Take input
ret_college = search_college(head, name) #Call function
if ret_college is True: #print result
print("The college is in the database!")
else:
print("The college is not in the database!")
#searching number of colleges in a state
state_name = input("\nPlease enter state: ")#get state input
count = search_in_state(head, state_name) #Function call
print("Number of colleges in",state_name,"=",count) #print result
main()
#CODE ENDS HERE-----------------